DI 依赖注入(Dependency Injection)
1、构造器注入
<bean id="user" class="com.wang.pojo.User">
<!--方式一-->
<!-- <constructor-arg index="0" value="王野"/>-->
<!--方式二,不建议使用,如果有多个参数,-->
<!-- <constructor-arg type="java.lang.String" value="邢敏"/>-->
<constructor-arg name="name" value="hfsdjhflkads"/>
</bean>
2、Set方式注入(重点)
-
依赖注入:Set注入
-
依赖: bean对象的创建依赖于容器
-
注入:bean对象中的所有属性,由容器来注入
【环境搭建】
-
复杂类型
-
真实测试对象
-
测试pojo类 :
Address.java
public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Student.java
package com.wang.pojo; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Student { private String name; private Address address; private String[] books; private List<String> hobbys; 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 setHobbys(List<String> hobbys) { this.hobbys = hobbys; } 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 void show(){ System.out.println("name="+ name + ",address="+ address.getAddress() + ",books=" ); for (String book:books){ System.out.print("<<"+book+">> "); } System.out.println(" 爱好:"+hobbys); System.out.println("card:"+card); System.out.println("games:"+games); System.out.println("wife:"+wife); System.out.println("info:"+info); } }
1、常量注入
<bean id="student" class="com.wang.pojo.Student"> <property name="name" value="小明"/> </bean>
测试:
@Test public void test01(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.getName()); }
2、Bean注入
注意点:这里的值是一个引用,ref
<bean id="addr" class="com.kuang.pojo.Address"> <property name="address" value="重庆"/> </bean> <bean id="student" class="com.kuang.pojo.Student"> <property name="name" value="小明"/> <property name="address" ref="addr"/> </bean>
3、数组注入
<bean id="student" class="com.wang.pojo.Student"> <property name="name" value="小明"/> <property name="address" ref="addr"/> <property name="books"> <array> <value>西游记</value> <value>红楼梦</value> <value>水浒传</value> </array> </property> </bean>
4、List注入
<property name="hobbys"> <list> <value>听歌</value> <value>看电影</value> <value>爬山</value> </list> </property>
5、Map注入
<property name="card"> <map> <entry key="中国邮政" value="456456456465456"/> <entry key="建设" value="1456682255511"/> </map> </property>
6、set注入
<property name="games"> <set> <value>LOL</value> <value>BOB</value> <value>COC</value> </set> </property>
7、Null注入
<property name="wife"><null/></property>
8、Properties注入
<property name="info"> <props> <prop key="学号">20234343</prop> <prop key="性别">男</prop> <prop key="姓名">小fd</prop> </props> </property>
3、其他方式
记得有参构造和无参构造器写上
1、P命名空间注入 : 需要在头文件中加入约束文件
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.kuang.pojo.User" p:name="hh" p:age="18"/>
2、c 命名空间注入 : 需要在头文件中加入约束文件
导入约束 : xmlns:c="http://www.springframework.org/schema/c"
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.kuang.pojo.User" c:name="oo" c:age="18"/>
Idea可以自动导入约束
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
}
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user2", User.class);
System.out.println(user);
}
DI 依赖注入(Dependency Injection)【spring基础】
原文地址:https://www.cnblogs.com/IanIan/p/14325263.html