作者:琳宝灬幸福 | 来源:互联网 | 2023-05-26 06:45
一、应用场景:Spring会帮创建实现类的实例,但是有时候我们还需要在类中设置一些属性用于传入设置值,这些跟类紧密关联的属性就叫依赖,通过spring帮忙设置的过程叫依赖注入。二、
一、应用场景:Spring会帮创建实现类的实例,但是有时候我们还需要在类中设置一些属性用于传入设置值,这些跟类紧密关联的属性就叫依赖,通过spring帮忙设置的过程叫依赖注入。
二、依赖注入的实现
A.构造方法的注入。
//这样配置bean都是通过无参构造生成bean实例
"userDao" class="cn.xxx.dao.impl.UserDaoImpl"/>
通过上面无参构造我们想到是否有有参构造创建实例了,这样不就达到了注入属性的目的了吗?确实,看下面:
//java代码:
private String name;
public UserDaoImpl(String name) {
this.name = name;
}
@Override
public void insert() {
System.out.println(name+"用户信息插入");
}
//配置文件
class="cn.xxx.dao.impl.UserDaoImpl">
只提供了有参构造而没有提供无参,所以现在Spring只能通过有参构造来创建实例,并注入属性。
B.set方式注入。
这种方式就比较常见了,在需要实例化的类里面提供属性及其set方法。
//java类里面:
private String name;
@Override
public void insert() {
System.out.println(name+"用户信息插入");
}
public void setName(String name) {
this.name = name;
}
配置文件里:
"userDao" class="cn.xxx.dao.impl.UserDaoImpl">
"name" value="张三">
运行结果:
18:05:18,439 INFO ClassPathXmlApplicationContext:578 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61443d8f: startup date [Sun May 05 18:05:18 CST 2019]; root of context hierarchy
18:05:18,461 INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [config/applicationContext.xml]
张三用户信息插入
C.构造或者set设置非普通属性(对象或者数组等引用类型)
//在需要Spring代理的实体类中用set或者构造
private Car car;
/*public UserDaoImpl(Car car) {
this.car = car;
}*/
public void setCar(Car car) {
this.car = car;
}
@Override
public void insert() {
System.out.println(car);
}
主要看配置文件:
class="cn.xxx.bean.Car">
class="cn.xxx.dao.impl.UserDaoImpl">
也就是说只靠在实体类里设置property不够,我们需要对象里面每个属性的值,所以直接把bean类托管给Spring。在我们需要的类里面用ref属性引入这个bean类
运行结果:
18:43:10,444 INFO ClassPathXmlApplicationContext:578 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61443d8f: startup date [Sun May 05 18:43:10 CST 2019]; root of context hierarchy
18:43:10,476 INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [config/applicationContext.xml]
Car [carName=奇瑞QQ, mOney=50000.0]
D.P名称空间的属性注入(Spring2.5版本以后)
有点类似与表达式那种,可以替换一些标签,用处不大,了解一下。首先得引入p名称空间(名称空间是xml中的概念)
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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="car" class="cn.xxx.bean.Car" p:carName="劳斯莱斯" p:money="12000000">
bean>
<bean id="userDao" class="cn.xxx.dao.impl.UserDaoImpl" p:car-ref="car" p:name="王校长">
bean>
就是将一些在property等标签里面写的东西拿到了bean标签上面写,简化了代码量
E:SPEL的方式属性注入(Spring3.x版本后使用)
SpEL:Spring Expression Language(Spring表达式语言),语法:#{SpEL}
<bean id="car" class="cn.xxx.bean.Car" >
<property name="carName" value="#{‘奔驰‘}" >property>
<property name="money" value="#{600000}">property>
bean>
<bean id="userDao" class="cn.xxx.dao.impl.UserDaoImpl" >
<property name="car" value="#{car}">property>
运行:
20:43:14,694 INFO ClassPathXmlApplicationContext:578 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61443d8f: startup date [Sun May 05 20:43:14 CST 2019]; root of context hierarchy
20:43:14,709 INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [config/applicationContext.xml]
Car [carName=奔驰, mOney=600000.0]
小周用户信息插入
F:复杂数据类型注入(集合,数组)
配置文件:
<bean id="userDao" class="cn.xxx.dao.impl.UserDaoImpl" >
<property name="arrs">
<array>
<value>你value>
<value>我value>
<value>它value>
array>
property>
<property name="list">
<list>
<value>芙蓉value>
<value>如花value>
<value>凤姐value>
list>
property>
<property name="map">
<map>
<entry key="aaa" value="111">entry>
<entry key="bbb" value="222">entry>
<entry key="ccc" value="333">entry>
map>
property>
bean>
//bean里面
private String[] arrs;
private List list;
private Map map;public void setArrs(String[] arrs) {
this.arrs = arrs;
}
public void setList(List list) {
this.list = list;
}
public void setMap(Map map) {
this.map = map;
}
运行结果:
21:27:48,113 INFO ClassPathXmlApplicationContext:578 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61443d8f: startup date [Sun May 05 21:27:48 CST 2019]; root of context hierarchy
21:27:48,128 INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [config/applicationContext.xml]
你
我
它
芙蓉
如花
凤姐
aaa...111
bbb...222
ccc...333
如果是set集合那么就用set标签。上面就是属性注入全方位讲解了。