---Bean依赖注入的两种方式:
1。设置注入:
例如:
<!-- 配置chinese实例,其实现类是Chinese --> <bean id="chinese" class="org.crazyit.app.service.impl.Chinese"> <!-- 将stoneAxe注入给axe属性 --> <property name="axe" ref="stoneAxe"/> </bean> <!-- 配置stoneAxe实例,其实现类是StoneAxe --> <bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/>
public class Chinese implements Person { private Axe axe; //设值注入所需的setter方法 public void setAxe(Axe axe) { this.axe = axe; } //实现Person接口的useAxe方法 public void useAxe() { //调用axe的chop()方法, //表明Person对象依赖于axe对象 System.out.println(axe.chop()); } }
2。构造注入:
例如:
<bean id="chinese" class="org.crazyit.app.service.impl.Chinese"> <!-- 使用构造注入,为chinese实例注入steelAxe实例 --> <constructor-arg ref="steelAxe"/> </bean> <!-- 配置stoneAxe实例,其实现类是StoneAxe --> <bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/>
public class Chinese implements Person { private Axe axe; //默认的构造器 public Chinese() { } //构造注入所需的带参数的构造器 public Chinese(Axe axe) { this.axe = axe; } //实现Person接口的useAxe方法 public void useAxe() { //调用axe的chop()方法 //表明Person对象依赖于axe对象 System.out.println(axe.chop()); } }
---Bean的注入装配
value
ref
autowire:byName,byType,autodetect,constructor
list,set,map,props
---spring注入
如果你在字段上有注解,就不用提供get,set方法。
如果在字段没有注解,就要写set方法就行,get方法可以不写
http://zhidao.baidu.com/question/454632653362469245.html?qbl=relate_question_2&word=spring%D7%A2%C8%EB%20%20set%B7%BD%B7%A8