首页 > 代码库 > Spring的依赖注入

Spring的依赖注入

1.添加spring的Jar包,还包括spring额外依赖的Jar包


指定创建类的方法(默认使用对应的构造函数创建),在创建类之前调用某个方法。在销毁类之后创建的方法

package com.lubby.test;

public class Single {
	private String id;
	private String something;
	private static Single single;

	private Single() {
		super();
	}
	private Single(String id, String something) {
		super();
		this.id = id;
		this.something = something;
	}
	public static Single getInstance(String id, String something) {
		if (single == null) {
			single = new Single(id, something);
		}
		return single;
	}
	public void init() {
		System.out.println("doSomethingBefor........");
	}
	public void destroy() {
		System.out.println("doSomethingAfter........");
	}
	@Override
	public String toString() {
		return "Single [id=" + id + ", something=" + something + "]";
	}

}



spring中的配置

<bean id="single" class="com.lubby.test.Single" factory-method="getInstance"  
		init-method="init" destroy-method="destroy"><pre name="code" class="html"><pre name="code" class="html">factory-method="getInstance" 
指定创建类的方法(默认使用对应的构造函数创建),
init-method="init"
在创建类之前调用某个方法。在销毁类之后创建的方法

<pre name="code" class="html"> destroy-method="destroy"
<constructor-arg value=http://www.mamicode.com/"0100102">"shadow-none p-3 mb-5 bg-light rounded">


依赖注入

Course类

package com.lubby.test;

public class Course {
	private String courseName;
	private String teacherName;
	public Course() {
		super();
	}
	public Course(String courseName, String teacherName) {
		super();
		this.courseName = courseName;
		this.teacherName = teacherName;
	}
	public String getCourseName() {
		return courseName;
	}
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	public String getTeacherName() {
		return teacherName;
	}
	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}
	@Override
	public String toString() {
		return "Course [courseName=" + courseName + ", teacherName=" + teacherName + "]";
	}

}


Student类

package com.lubby.test;

public class Student {
	private String name;
	private String school;
	private int age;
	private Course course;
	private String address;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String name, String school, int age, Course course) {
		super();
		this.name = name;
		this.school = school;
		this.age = age;
		this.course = course;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

	public Course getCourse() {
		return course;
	}
	public void setCourse(Course course) {
		this.course = course;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", school=" + school + ", age=" + age + ", course=" + course + ", address="
				+ address + "]";
	}
}
1.通过构造函数注入参数

<bean id="course" class="com.lubby.test.Course">
	<constructor-arg value=http://www.mamicode.com/"lilI Wang" name="teacherName">>

3.通过setting方法注入参数

	<bean id="stu" class="com.lubby.test.Student">
		<property name="name" value=http://www.mamicode.com/"Lubby">>执行

package com.lubby.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
              //加载配置文件,获取上下文
		ApplicationContext ctx = new ClassPathXmlApplicationContext("com/lubby/test/test.xml");
//		Single single = (Single) ctx.getBean("single");
//		System.out.println(single);
//		single.destroy();
               //  通过配置文件中bean 的id去创建和取得bean
		 Course course = (Course) ctx.getBean("course");
		 System.out.println(course);

		 Student student = (Student) ctx.getBean("stu");
		 System.out.println(student);
	}
}