标签:
1 package com.bless.springdemo.action; 2 public class SpringAction { 3 //注入对象springDao 4 private SpringDao springDao; 5 //一定要写被注入对象的set方法 6 public void setSpringDao(SpringDao springDao) { 7 this.springDao = springDao; 8 } 9 10 public void ok(){ 11 springDao.ok(); 12 } 13 }
随后编写spring的xml文件,<bean>中的name属性是class属性的一个别名,class属性指类的全名,因为在SpringAction中有一个公共属性Springdao,所以要在<bean>标签中创建一个<property>标签指定SpringDao。
<property>标签中的name就是SpringAction类中的SpringDao属性名,ref指下面<bean name="springDao"...>,
这样其实是spring将SpringDaoImpl对象实例化并且调用SpringAction的setSpringDao方法将SpringDao注入:
1 <!--配置bean,配置后该类由spring管理--> 2 <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 3 <!--(1)依赖注入,配置当前类中相应的属性--> 4 <property name="springDao" ref="springDao"></property> 5 </bean> 6 <bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>
1 public class SpringAction { 2 //注入对象springDao 3 private SpringDao springDao; 4 private User user; 5 6 public SpringAction(SpringDao springDao,User user){ 7 this.springDao = springDao; 8 this.user = user; 9 System.out.println("构造方法调用springDao和user"); 10 } 11 12 public void save(){ 13 user.setName("卡卡"); 14 springDao.save(user); 15 } 16 }
在XML文件中同样不用<property>的形式,而是使用<constructor-arg>标签,ref属性同样指向其它<bean>标签的name属性:
1 <!--配置bean,配置后该类由spring管理--> 2 <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 3 <!--(2)创建构造器注入,如果主类有带参的构造方法则需添加此配置--> 4 <constructor-arg ref="springDao"></constructor-arg> 5 <constructor-arg ref="user"></constructor-arg> 6 </bean> 7 <bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean> 8 <bean name="user" class="com.bless.springdemo.vo.User"></bean>
解决构造方法参数的不确定性,你可能会遇到构造方法传入的两参数都是同类型的,为了分清哪个该赋对应值,则需要进行一些小处理:
1 <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 2 <constructor-arg index="0" ref="springDao"></constructor-arg> 3 <constructor-arg index="1" ref="user"></constructor-arg> 4 </bean>
另一种是设置参数类型:
1 <constructor-arg type="java.lang.String" ref=""/>
1 package com.bless.springdemo.factory; 2 3 import com.bless.springdemo.dao.FactoryDao; 4 import com.bless.springdemo.dao.impl.FactoryDaoImpl; 5 import com.bless.springdemo.dao.impl.StaticFacotryDaoImpl; 6 7 public class DaoFactory { 8 //静态工厂 9 public static final FactoryDao getStaticFactoryDaoImpl(){ 10 return new StaticFacotryDaoImpl(); 11 } 12 }
同样看关键类,这里我需要注入一个FactoryDao对象,这里看起来跟第一种注入一模一样,但是看随后的xml会发现有很大差别:
1 public class SpringAction { 2 //注入对象 3 private FactoryDao staticFactoryDao; 4 5 public void staticFactoryOk(){ 6 staticFactoryDao.saveFactory(); 7 } 8 //注入对象的set方法 9 public void setStaticFactoryDao(FactoryDao staticFactoryDao) { 10 this.staticFactoryDao = staticFactoryDao; 11 } 12 }
Spring的IOC配置文件,注意看<bean name="staticFactoryDao">指向的class并不是FactoryDao的实现类,而是指向静态工厂DaoFactory,并且配置 factory-method="getStaticFactoryDaoImpl"指定调用哪个工厂方法:
1 <!--配置bean,配置后该类由spring管理--> 2 <bean name="springAction" class="com.bless.springdemo.action.SpringAction" > 3 <!--(3)使用静态工厂的方法注入对象,对应下面的配置文件(3)--> 4 <property name="staticFactoryDao" ref="staticFactoryDao"></property> 5 </property> 6 </bean> 7 <!--(3)此处获取对象的方式是从工厂类中获取静态方法--> 8 <bean name="staticFactoryDao" class="com.bless.springdemo.factory.DaoFactory" factory-method="getStaticFactoryDaoImpl"></bean>
1 public class DaoFactory { 2 //实例工厂 3 public FactoryDao getFactoryDaoImpl(){ 4 return new FactoryDaoImpl(); 5 } 6 }
那么下面这个类没什么说的,跟前面也很相似,但是我们需要通过实例工厂类创建FactoryDao对象:
1 public class SpringAction { 2 //注入对象 3 private FactoryDao factoryDao; 4 5 public void factoryOk(){ 6 factoryDao.saveFactory(); 7 } 8 9 public void setFactoryDao(FactoryDao factoryDao) { 10 this.factoryDao = factoryDao; 11 } 12 }
最后看spring配置文件:
1 <!--配置bean,配置后该类由spring管理--> 2 <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 3 <!--(4)使用实例工厂的方法注入对象,对应下面的配置文件(4)--> 4 <property name="factoryDao" ref="factoryDao"></property> 5 </bean> 6 7 <!--(4)此处获取对象的方式是从工厂类中获取实例方法--> 8 <bean name="daoFactory" class="com.bless.springdemo.factory.DaoFactory"></bean> 9 <bean name="factoryDao" factory-bean="daoFactory" factory-method="getFactoryDaoImpl"></bean>
1 <bean name="..." class="..." scope="prototype">
标签:
原文地址:http://www.cnblogs.com/AnXinliang/p/5463598.html