六、依赖注入
1.构造器注入
<bean id="hello" class="com.lzt.pojo.Hello">
<constructor-arg name="str" value="lzt"/>
</bean>
2.Set方法注入【重点】
- 依赖注入:set注入!
- 依赖:bean对象创建依赖于容器!
- 注入:bean对象中的所有属性,由容器来注入!
【环境搭建】
-
复杂类型
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> hobbies; private Map<String,String> card; private Set<String> games; private Properties info; private String wife;
-
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="com.lzt.pojo.Student"> <!--第一种,普通值注入,直接使用value--> <property name="name" value="lzt"/> </bean> </beans>
-
测试类
public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); String name = student.getName(); System.out.println(name); } }
完善注入信息:
<?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.lzt.pojo.Address">
<property name="address" value="南极"/>
</bean>
<bean id="student" class="com.lzt.pojo.Student">
<!--普通值注入,直接使用value-->
<property name="name" value="lzt"/>
<!--bean 注入,使用ref-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!--list注入-->
<property name="hobbies">
<list>
<value>听歌</value>
<value>打游戏</value>
<value>敲代码</value>
</list>
</property>
<!--Map注入-->
<property name="card">
<map>
<entry key="身份证" value="22222111123"/>
<entry key="学生号" value="11111"/>
<entry key="电话号码" value="333333"/>
</map>
</property>
<!--set注入-->
<property name="games">
<set>
<value>1</value>
<value>2</value>
<value>4</value>
<value>3</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null/>
</property>
<!--properties注入-->
<property name="info">
<props>
<prop key="学号">222</prop>
<prop key="年龄">12</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>
</beans>
执行结果:
/*Student{name='lzt'
address=Address{address='南极'}
books=[红楼梦, 西游记, 水浒传, 三国演义]
hobbies=[听歌, 打游戏, 敲代码]
card={身份证=22222111123,
学生号=11111,
电话号码=333333}
games=[1, 2, 4, 3]
info={学号=222,
性别=男,
年龄=12}
wife='null'}*/
3.拓展方式植入注入
我们可以通过p命名空间和从命名空间进行注入
-
p 命名空间注入
<?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="com.lzt.pojo.Student" P:name="sss"/> </beans>
-
c 命名空间注入
<?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="user1" class="com.lzt.pojo.Student" c:name="2222" c:books="sss"/> </beans>
注意点:p命名和c命名空间不能直接使用,需要导入xml约束