spring依赖注入
1、构造器注入
2、set方式注入(重点)
2.1 依赖注入:Set注入!
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,有容器注入
2.2 环境搭建
- 复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
- 测试对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private String wife;
private Set<String> games;
private Properties info;
}
- bean.xml
<?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="student" class="com.cherry.pojo.Student">
<!--第一种:普通注入-->
<property name="name" value="cherry"/>
</bean>
</beans>
- 测试类
public class MyTest {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getAddress());
2.3 完善注入信息
<?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.cherry.pojo.Address"/>
<bean id="student" class="com.cherry.pojo.Student">
<!--第一种:普通注入-->
<property name="name" value="nyy"/>
<!--第二种:bean注入-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>三国演义</value>
<value>西游记</value>
</array>
</property>
<!--List注入-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>跑步</value>
<value>游泳</value>
</list>
</property>
<!--Map注入-->
<property name="card">
<map>
<entry key="身份证" value="1324444"/>
<entry key="银行卡" value="123456774"/>
</map>
</property>
<!--Set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>BOB</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null></null>
</property>
<!--Properties注入-->
<property name="info">
<props>
<prop key="studentname">2017124008</prop>
<prop key="url">男</prop>
<prop key="username">小明</prop>
</props>
</property>
</bean>
</beans>
3、拓展方式注入
使用P命名空间或c命名空间注入
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-c-namespace
- 使用
<?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"
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">
<!--p命名空间注入,可以直接注入属性的值(无参构造):property-->
<bean id="user" class="com.cherry.pojo.User" p:name="cherry" p:age="18"/>
<!--c命名空间注入,通过构造器注入,(有参构造+无参构造),construct-arg-->
<bean id="user2" class="com.cherry.pojo.User" c:name="cherry" c:age="19"/>
</beans>
- 测试
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
User user = context.getBean("user2",User.class);
System.out.println(user);
}
注:p命名空间和c命名空间都必须导入xml约束!!!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"