1、构造器注入
请参考前面一篇博客IOC创建对象的方式及spring配置
2、set方式注入(重点)
依赖注入:Set注入
依赖:bean对象的创建依赖于容器!
注入:bean对象中的所有属性,由容器来注入!
环境搭建
1、复杂类型:
public class Address {
//引用类型
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
2、测试对象:
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;
}
3、beans.xml(重点)
<bean id="address" class="com.like.pojo.Address">
<property name="address" value="重庆"/>
</bean>
<bean id="student" class="com.like.pojo.Student">
<!--普通值注入,value-->
<property name="name" value="玛卡巴卡"/>
<!--bean注入、ref-->
<property name="address" ref="address"/>
<!--数组-->
<property name="books">
<array>
<value>西游记</value>
<value>三国演义</value>
<value>水浒传</value>
<value>红楼梦</value>
</array>
</property>
<!--List-->
<property name="hobbys">
<list>
<value>唱歌</value>
<value>跳舞</value>
<value>rap</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="身份证" value="200001010001"/>
<entry key="银行卡" value="230274747023"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>打篮球</value>
<value>LOL</value>
<value>NBA</value>
</set>
</property>
<!--Null-->
<property name="wife">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="driver">696969669</prop>
<prop key="url">232.3543.2343</prop>
<prop key="user">root</prop>
<prop key="password">991123</prop>
</props>
</property>
</bean>
Result:
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
/*
Student{name='玛卡巴卡',
address=Address{address='重庆'},
books=[西游记, 三国演义, 水浒传, 红楼梦],
hobbys=[唱歌, 跳舞, rap], card={身份证=200001010001, 银行卡=230274747023},
games=[打篮球, LOL, NBA],
wife='null',
info={user=root, password=991123, url=232.3543.2343, driver=696969669}}
*/
}
}
3、拓展方式注入
可以使用p命名空间和c命名空间进行注入
使用,去官网导入xml约束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
报红的话可以这样解决:
打开File------->Settings------>Languages & Frameworks
使用:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性的值-->
<bean id="user" class="com.like.pojo.User" p:name="武松" p:age="3"/>
<!--c命名空间注入,通过构造器注入:constructor-args-->
<bean id="user2" class="com.like.pojo.User" c:age="5" c:name="李逵"/>
</beans>
测试:
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user2", User.class);
System.out.println(user);
}