6、DI依赖注入
6.1、构造器注入
前面已经讲过
6.2、set方式注入【重点】
- 依赖注入:set注入!
- 依赖:bean对象的创建依赖于容器!
- 注入:bean对象中的所有属性,由容器来注入!
【环境搭建】
-
复杂类型
package com.ysl.pojo; 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 + '\'' + '}'; } }
-
真实测试对象
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 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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.ysl.pojo.Address" > <property name="address" value="洛阳" /> </bean> <bean id="stu" class="com.ysl.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> <!--Map--> <property name="card"> <map> <entry key="身份证" value="410482"/> <entry key="学号" value="201451081528"/> </map> </property> <!--Set--> <property name="games"> <set> <value>lol</value> <value>cs</value> <value>csgo</value> </set> </property> <!--List--> <property name="hobbies"> <list> <value>学习</value> <value>篮球</value> <value>电影</value> </list> </property> <!--null--> <property name="wife"> <null/> </property> <!--Properties--> <property name="info"> <props> <prop key="driver">com.mysql.cj.jdbc.Driver</prop> <prop key="url">mysql:jdbc://localhost:3306/spring</prop> <prop key="user">root</prop> <prop key="password">123456</prop> </props> </property> </bean> </beans>
-
测试类
public class MyTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student stu = (Student) context.getBean("stu"); System.out.println(stu); }
6.3、扩展方式注入
我们可以使用p命名空间和c命名空间进行注入
官方解释:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rYjbXr3P-1648381985888)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20220327183136765.png)]
使用:
<?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命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.ysl.pojo.User" p:age="18" p:name="于世梁"/>
<!--c命名空间注入,通过构造器注入:constructs-args-->
<bean id="user1" class="com.ysl.pojo.User" c:age="19" c:name="zhangsan" scope=""/>
</beans>
测试;
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user",User.class);
System.out.println(user);
User user2 = context.getBean("user2",User.class);
System.out.println(user2);
}
注意点:p命名空间和c命名空间不能直接使用,需要导入xml约束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
6.4、bean的作用域
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9VhkIanX-1648381985889)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20220327182914978.png)]
-
单例模式
<bean id="user1" class="com.ysl.pojo.User" c:age="19" c:name="zhangsan" scope="singleton"/>
-
原型模式:每次从容器中get的时候,都会new一个新对象!
<bean id="user1" class="com.ysl.pojo.User" c:age="19" c:name="zhangsan" scope="prototype"/>
-
其余的request、session、application、,这些只能在web开发中使用到!