依赖注入
构造器注入
前面有一篇文章写过,还有大神的依赖注入理解 https://blog.youkuaiyun.com/weixin_44424328/article/details/116656937
Set方式注入
- 依赖注入:使用set注入!
- 依赖 :bean对象的创建依赖于容器!
- 注入 :bean对象中的属性由容器来注入!
- 搭建环境
- 复杂类型(省略get set 构造 tostring 下同)
public class Address {
private String address;
}
- 整合类
public class Person {
private String name;
private Address address;
private String[] books;
private List<String> list;
private Map<String,String> map;
private Set<String> set;
private Properties info;
private String wife;
private boolean flag;
private List<Map<String,String>> mapList;
}
- sping配置文件
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.yxx.pojo.Address">
<property name="address" value="四川"/>
</bean>
<bean id="person" class="com.yxx.pojo.Person">
<!--简单类型使用value -->
<property name="name" value="小明"/>
<!-- 复杂类型使用引用 ref-->
<property name="address" ref="address"/>
<!-- 数组类型使用 array-->
<property name="books">
<array>
<value>三国</value>
<value>春秋</value>
<value>论语</value>
</array>
</property>
<!--list类型 -->
<property name="list">
<list>
<value>c</value>
<value>c++</value>
<value>java</value>
</list>
</property>
<!--map类型 -->
<property name="map">
<map>
<entry key="川A" value="成都"/>
<entry key="川B" value="绵阳"/>
<entry key="川C" value="自贡"/>
</map>
</property>
<!-- set类型-->
<property name="set">
<set>
<value>LOL</value>
<value>CF</value>
<value>CS</value>
</set>
</property>
<!-- Properties类型-->
<property name="info">
<props>
<prop key="学9">603</prop>
<prop key="学8">603</prop>
<prop key="学7">603</prop>
</props>
</property>
<!--属性赋null -->
<property name="wife">
<null/>
</property>
<property name="flag" value="true"/>
<!--list map混合类型 标签套用即可 -->
<property name="mapList">
<list>
<map>
<entry key="川D" value="攀枝花"/>
<entry key="川E" value="泸州"/>
<entry key="川F" value="德阳"/>
</map>
</list>
</property>
</bean>
</beans>
- 测试
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
Person person = (Person) context.getBean("person");
System.out.println(person);
/*
Person{name='小明',
address=Address{address='四川'},
books=[三国, 春秋, 论语],
list=[c, c++, java],
map={川A=成都, 川B=绵阳, 川C=自贡},
set=[LOL, CF, CS],
info={学8=603, 学7=603, 学9=603},
wife='null',
flag=true,
mapList=[{川D=攀枝花, 川E=泸州, 川F=德阳}]}
*/
}
拓展
采用c命名空间和p命名空间
- c命名空间就是使用的构造方法 与原来在bean中配置的constructor-arg类似
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanTwo" class="x.y.ThingTwo"/>
<bean id="beanThree" class="x.y.ThingThree"/>
<!-- traditional declaration with optional argument names -->
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg name="thingTwo" ref="beanTwo"/>
<constructor-arg name="thingThree" ref="beanThree"/>
<constructor-arg name="email" value="something@somewhere.com"/>
</bean>
<!-- c-namespace declaration with argument names -->
<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>
</beans>
- p命名空间就是属性配置
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="classic" class="com.example.ExampleBean">
<property name="email" value="someone@somewhere.com"/>
</bean>
<bean name="p-namespace" class="com.example.ExampleBean"
p:email="someone@somewhere.com"/>
</beans>
注意:使用这两种方法需要注意的点,使用时需要导入他们各自的命名空间
c: xmlns:c=“http://www.springframework.org/schema/c”
p: xmlns:p=“http://www.springframework.org/schema/p”