`
- 浏览:
110567 次
- 性别:
- 来自:
杭州
-
spring对其管理的bean的属性注入方式有两种,构造器注入和setter注入。
1 构造器注入
构造器参数解析根据参数类型进行匹配,如果bean的构造器参数类型定义非常明确,那么在bean被实例化的时候,bean定义中构造器参数的定义顺序就是这些参数的顺序,依次进行匹配,比如下面的代码
package x.y;
public class Foo {
public Foo(Bar bar, Baz baz) {
// ...
}
}
上述例子中由于构造参数非常明确(这里我们假定 Bar和 Baz之间不存在继承关系)。因此下面的配置即使没有明确指定构造参数顺序(和类型),也会工作的很好。
<beans>
<bean name="foo" class="x.y.Foo">
<constructor-arg>
<bean class="x.y.Bar"/>
</constructor-arg>
<constructor-arg>
<bean class="x.y.Baz"/>
</constructor-arg>
</bean>
</beans>
下面是基本数据类型的注入
public class ExampleBean {
private int years;
private String ultimateAnswer;
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>
我们还可以通过index属性来显式指定构造参数的索引,比如下面的例子:
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
</bean>
通过使用索引属性不但可以解决多个简单属性的混淆问题,还可以解决有可能有相同类型的2个构造参数的混淆问题了,注意index是从0开始
2 setter注入
通过调用无参构造器或无参static工厂方法实例化bean之后,调用该bean的setter方法,即可实现基于setter的DI。
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
<bean id="exampleBean" class="examples.ExampleBean">
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
</bean>
由于大量的构造器参数可能使程序变得笨拙,特别是当某些属性是可选的时候。因此通常情况下,Spring开发团队提倡使用setter注入。而且setter DI在以后的某个时候还可将实例重新配置(或重新注入)
尽管如此,构造器注入还是得到很多纯化论者(也有很好的理由)的青睐。一次性将所有依赖注入的做法意味着,在未完全初始化的状态下,此对象不会返回给客户代码(或被调用),此外对象也不需要再次被重新配置 对于注入类型的选择并没硬性的规定。只要能适合你的应用,无论使用何种类型的DI都可以。对于那些没有源代码的第三方类,或者没有提供setter方法的遗留代码,我们则别无选择--构造器注入将是你唯一的选择。
bean的属性有简单类型(基本数据类型和String)和复杂类型(其他类的实例)两种。分别对应不同的方式。
简单类型的xml写法如下
<!--第一种方式 推荐使用-->
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<!--第二种方式-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!--第三种方式-->
<property name="properties">
<value>
jdbc.driver.className=com.mysql.jdbc.Driver
jdbcjdbcjdbc.url=jdbc:mysql://localhost:3306/mydb
</value>
</property>
复杂类型有两种方式,一种是直接引入别的bean,另一种是用内置bean
<bean id="userDao" class="dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="showUser" class="action.ShowUserAction">
<!--引用其他的bean-->
<property name="userDao" ref="userDao"/>
</bean>
<bean id="outer" class="...">
<property name="target">
<!--这个就是内置bean,不需要指定name的属性-->
<bean class="com.example.Person">
<property name="name" value="Fiona Apple"/>
<property name="age" value="25"/>
</bean>
</property>
</bean>
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
NULL 博文链接:https://gary0416.iteye.com/blog/890689
原创的哦,希望能对spring学习者有所帮助
spring注入方式 set注入 构造注入 自动注入
NULL 博文链接:https://gary0416.iteye.com/blog/889228
NULL 博文链接:https://weiliuhong1.iteye.com/blog/1070875
你知道Spring的注入方式有哪几种吗?这篇文章主要为大家详细介绍了Spring的注入方式,感兴趣的小伙伴们可以参考一下
要用注解注入方式,还需要在applicationContext.xml文件加入一行代码: <context:component-scan base-package="Mode"></context:component-scan> //表示在包mode下面的类将扫描带有@Component,@Controller,@Service...
Spring 三种依赖注入方式.doc Spring 三种依赖注入方式.doc
bean加入spring容器管理的方式,bean加入applicationcontext容器的方式
详细介绍spring的三种注入方式,带有实例和图片
spring注入配置文件,使用注解注入的
我们在开发过程中经常会发生spring注入错误或失败的情况,其实Web服务器端在Action、dao、service包之内是很容易实现的,在这些包之外实现spring注入就不容易了,总是出错,其实根本原因是无法获得相应的spring上...
主要给大家介绍了关于spring boot中几种注入方法的一些个人看法,文中通过示例代码介绍的非常详细,对大家学习或者使用spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
spring 注入 spring 注入 spring 注入 spring 注入
Spring依赖注入——java项目中使用spring注解方式进行注入.rar
spring mvc spring4.x hibernate4.注解方式注入 简单的例子.spring mvc spring4.x hibernate4.注解方式注入 简单的例子.
使用spring的依赖注入,来实现servlet中注入dao层
spring注入文件写法(包括hibernate注入、dao注入等等)
自己实现spring注入和实现一个自定义的注入
详细阐明spring依赖注入工作原理,基于注解的一个完整例子,当然数据库需要自己去创建