1.构造器注入
2.set方式注入【主流】
依赖注入:set注入!(依赖是指:bean对象的创建依赖于容器
注入是指:bean对象中的所有属性,由容器来注入!)
测试:
复杂类型
pojo类文件
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;//通过ref赋值
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> game;
private String wife;
private Properties info;
}
beans.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="pojo.Student">
<!--普通值注入-->
<property name="name" value="hzc"/>
</bean>
</beans>
完善注入信息:
<?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="pojo.Address">
</bean>
<bean id="student" class="pojo.Student">
<!--第一种,常量注入-->
<property name="name" value="hzc"/>
<!--第二种,引用注入,ref-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>《红楼梦》</value>
<value>《西游记》</value>
<value>《水浒传》</value>
<value>《三国演义》</value>
</array>
</property>
<!--链表注入-->
<property name="hobbies">
<list>
<value>打游戏</value>
<value>打代码</value>
<value>学习</value>
</list>
</property>
<!--set注入-->
<property name="game">
<set>
<value>王者荣耀</value>
<value>英雄联盟</value>
</set>
</property>
<!--null值注入-->
<property name="wife">
<null></null>
</property>
<!--图注入-->
<property name="card">
<map>
<entry key="身份证" value="123456123456781234"/>
<entry key="银河卡" value="123123123123"/>
</map>
</property>
<!--Properties注入
key=value
-->
<property name="info">
<props>
<prop key="driver">2</prop>
<prop key="url"></prop>
<prop key="username"></prop>
<prop key="password"></prop>
</props>
</property>
</bean>
</beans>
3.拓展方式注入
可以使用p命名空间和c命名空间注入
p-namespace对应set注入
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"
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命名空间注入,可以直接注入属性的值,p=property-->
<bean id="user" class="pojo.User" p:name="cz" p:age="18"/>
</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: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命名空间注入,通过构造器注入:c=construct-args-->
<bean id="user2" class="pojo.User" c:age="21" c:name="zc"/>
</beans>
注意:p命名空间和c命名空间不能直接使用,需要导入xml约束