依赖注入(Dependency Injection,DI)——依赖 : 指Bean对象的创建依赖于容器,Bean对象的依赖资源;注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配。
具体可以查看官方文档
1. 有参构造函数注入
1.1 搭建环境
public class User {
private String name;
public User(String name) {
this.name = name;
System.out.println("User类的有参构造函数被调用");
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.comprehensive.pojo.User">
<constructor-arg name="name" value="comprehensive"/>
<!--<constructor-arg index="0" value="comprehensive"/>-->
</bean>
</beans>
public class Test_User {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
}
}
1.2 测试结果
2. set方法注入
2.1 搭建环境
//Adress实体类
public class Address {
private String address;
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
//Student实体类
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setWife(String wife) {
this.wife = wife;
}
public void setInfo(Properties info) {
this.info = info;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
public String[] getBooks() {
return books;
}
public List<String> getHobbies() {
return hobbies;
}
public Map<String, String> getCard() {
return card;
}
public Set<String> getGames() {
return games;
}
public String getWife() {
return wife;
}
public Properties getInfo() {
return info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
2.2 常量注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.comprehensive.pojo.Address">
<property name="address" value="572456720@qq.com"/>
</bean>
</beans>
public class Test_DI {
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Address address = context.getBean("address", Address.class);
System.out.println(address);
}
}
2.3 bean ref注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 常量注入 -->
<bean id="address" class="com.comprehensive.pojo.Address">
<property name="address" value="572456720@qq.com"/>
</bean>
<!-- bean ref注入 -->
<bean id="student1" class="com.comprehensive.pojo.Student">
<property name="address" ref="address"/>
</bean>
</beans>
public class Test_DI {
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = context.getBean("student1", Student.class);
System.out.println(student.getAddress());
}
}
2.4 array注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- array注入 -->
<bean id="student2" class="com.comprehensive.pojo.Student">
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
</array>
</property>
</bean>
</beans>
public class Test_DI {
@Test
public void test3() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = context.getBean("student2", Student.class);
System.out.println(Arrays.toString(student.getBooks()));
}
}
2.5 list注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- list注入 -->
<bean id="student3" class="com.comprehensive.pojo.Student">
<property name="hobbies">
<list>
<value>听歌</value>
<value>看电影</value>
<value>爬山</value>
</list>
</property>
</bean>
</beans>
public class Test_DI {
@Test
public void test4() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = context.getBean("student3", Student.class);
System.out.println(student.getHobbies());
}
}
2.6 map注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- map注入 -->
<bean id="student4" class="com.comprehensive.pojo.Student">
<property name="card">
<map>
<entry key="中国邮政" value="456456456465456"/>
<entry key="建设" value="1456682255511"/>
</map>
</property>
</bean>
</beans>
public class Test_DI {
@Test
public void test5() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = context.getBean("student4", Student.class);
System.out.println(student.getCard());
}
}
2.7 set注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- set注入 -->
<bean id="student5" class="com.comprehensive.pojo.Student">
<property name="games">
<set>
<value>LOL</value>
<value>BOB</value>
<value>COC</value>
</set>
</property>
</bean>
</beans>
public class Test_DI {
@Test
public void test6() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = context.getBean("student5", Student.class);
System.out.println(student.getGames());
}
}
2.8 null注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- null注入 -->
<bean id="student6" class="com.comprehensive.pojo.Student">
<property name="wife"><null/></property>
</bean>
</beans>
public class Test_DI {
@Test
public void test7() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = context.getBean("student6", Student.class);
System.out.println(student.getWife());
}
}
2.9 properties注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- properties注入 -->
<bean id="student7" class="com.comprehensive.pojo.Student">
<property name="info">
<props>
<prop key="学号">20190604</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>
</props>
</property>
</bean>
</beans>
public class Test_DI {
@Test
public void test8() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = context.getBean("student7", Student.class);
System.out.println(student.getInfo());
}
}
3. 拓展方式注入
3.1 p命名空间
public class User {
private String name;
private int age;
public void setName(String name) {
this.name = name;
System.out.println("setName方法被调用[set方法注入]");
}
public void setAge(int age) {
this.age = age;
System.out.println("setAge方法被调用[set方法注入]");
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- p命名空间 注入 -->
<bean id="user1" class="com.comprehensive.pojo.User" p:name="comprehensive" p:age="22"/>
</beans>
public class Test_DI {
@Test
public void test9() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans_p.xml");
}
}
3.2 c命名空间
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
System.out.println("有参构造函数被调用[有参构造函数注入]");
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- c命名空间 注入 -->
<bean id="user2" class="com.comprehensive.pojo.User" c:name="comprehensive" c:age="22"/>
</beans>
public class Test_DI {
@Test
public void test10() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans_c.xml");
}
}