Spring注解的方式有很多中,下面仅介绍Spring通过xml进行IOC配置的方式:
一、Set注入
这是最简单的注入方式,假设有一个SpringAction,类中需要实例化一个SpringDao对象,那么就可以定义一个private的SpringDao成员变量,然后创建SpringDao的Set方法(这是IOC的注入入口):
- package com.bless.springdemo.action;
- public class SpringAction {
- //注入对象springDao
- private SpringDao springDao;
- //一定要写被注入对象的set方法
- public void setSpringDao(SpringDao springDao) {
- this.springDao = springDao;
- }
- public void ok(){
- springDao.ok();
- }
- }
随后编写spring的xml文件,<bean>中的name属性是class属性的一个别名,class属性指类的全名,因为在SpringAction中有一个公共属性SpringDao,所以要在<bean>标签中创建一个<property>标签指定SpringDao.<property>标签中的name就是SpringAction类中的SpringDao属性名,ref指下面<bean name="SpringDao" class=".....>,这样其实Spring将SpringDaoImpl对象实例化并且调用SpringAction的SetSpringDao方法将SpringDao注入:
- <!--配置bean,配置后该类由spring管理-->
- <bean name="springAction" class="com.bless.springdemo.action.SpringAction">
- <!--(1)依赖注入,配置当前类中相应的属性-->
- <property name="springDao" ref="springDao"></property>
- </bean>
- <bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>
这种方式的注入是指带有参数的构造函数注入,看下面的例子,创建了两个成员变量SpringDao和User,但是并未设置对象那个的set方法,所以就不能支持第一种注入方式,这里的注入方式是在SpringAction的构造函数中注入,也就是说在创建SpringAction对象时要将SpringDao和User两个参数值传进来:
- public class SpringAction {
- //注入对象springDao
- private SpringDao springDao;
- private User user;
- public SpringAction(SpringDao springDao,User user){
- this.springDao = springDao;
- this.user = user;
- System.out.println("构造方法调用springDao和user");
- }
- public void save(){
- user.setName("卡卡");
- springDao.save(user);
- }
- }
- <!--配置bean,配置后该类由spring管理-->
- <bean name="springAction" class="com.bless.springdemo.action.SpringAction">
- <!--(2)创建构造器注入,如果主类有带参的构造方法则需添加此配置-->
- <constructor-arg ref="springDao"></constructor-arg>
- <constructor-arg ref="user"></constructor-arg>
- </bean>
- <bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>
- <bean name="user" class="com.bless.springdemo.vo.User"></bean>
- <bean name="springAction" class="com.bless.springdemo.action.SpringAction">
- <constructor-arg index="0" ref="springDao"></constructor-arg>
- <constructor-arg index="1" ref="user"></constructor-arg>
- </bean>
- <constructor-arg type="java.lang.String" ref=""/>
- package com.bless.springdemo.factory;
- import com.bless.springdemo.dao.FactoryDao;
- import com.bless.springdemo.dao.impl.FactoryDaoImpl;
- import com.bless.springdemo.dao.impl.StaticFacotryDaoImpl;
- public class DaoFactory {
- //静态工厂
- public static final FactoryDao getStaticFactoryDaoImpl(){
- return new StaticFacotryDaoImpl();
- }
- }
- public class SpringAction {
- //注入对象
- private FactoryDao staticFactoryDao;
- public void staticFactoryOk(){
- staticFactoryDao.saveFactory();
- }
- //注入对象的set方法
- public void setStaticFactoryDao(FactoryDao staticFactoryDao) {
- this.staticFactoryDao = staticFactoryDao;
- }
- }
- <!--配置bean,配置后该类由spring管理-->
- <bean name="springAction" class="com.bless.springdemo.action.SpringAction" >
- <!--(3)使用静态工厂的方法注入对象,对应下面的配置文件(3)-->
- <property name="staticFactoryDao" ref="staticFactoryDao"></property>
- </property>
- </bean>
- <!--(3)此处获取对象的方式是从工厂类中获取静态方法-->
- <bean name="staticFactoryDao" class="com.bless.springdemo.factory.DaoFactory" factory-method="getStaticFactoryDaoImpl"></bean>
同样看关键类,这里我需要注入一个FactoryDao对象,这里看起来跟第一种注入一模一样,但是随后的xml会发现有很大差别:
- public class SpringAction {
- //注入对象
- private FactoryDao staticFactoryDao;
- public void staticFactoryOk(){
- staticFactoryDao.saveFactory();
- }
- //注入对象的set方法
- public void setStaticFactoryDao(FactoryDao staticFactoryDao) {
- this.staticFactoryDao = staticFactoryDao;
- }
- }
- <!--配置bean,配置后该类由spring管理-->
- <bean name="springAction" class="com.bless.springdemo.action.SpringAction" >
- <!--(3)使用静态工厂的方法注入对象,对应下面的配置文件(3)-->
- <property name="staticFactoryDao" ref="staticFactoryDao"></property>
- </property>
- </bean>
- <!--(3)此处获取对象的方式是从工厂类中获取静态方法-->
- <bean name="staticFactoryDao" class="com.bless.springdemo.factory.DaoFactory" factory-method="getStaticFactoryDaoImpl"></bean>
- public class DaoFactory {
- //实例工厂
- public FactoryDao getFactoryDaoImpl(){
- return new FactoryDaoImpl();
- }
- }
- public class SpringAction {
- //注入对象
- private FactoryDao factoryDao;
- public void factoryOk(){
- factoryDao.saveFactory();
- }
- public void setFactoryDao(FactoryDao factoryDao) {
- this.factoryDao = factoryDao;
- }
- }
- <!--配置bean,配置后该类由spring管理-->
- <bean name="springAction" class="com.bless.springdemo.action.SpringAction">
- <!--(4)使用实例工厂的方法注入对象,对应下面的配置文件(4)-->
- <property name="factoryDao" ref="factoryDao"></property>
- </bean>
- <!--(4)此处获取对象的方式是从工厂类中获取实例方法-->
- <bean name="daoFactory" class="com.bless.springdemo.factory.DaoFactory"></bean>
- <bean name="factoryDao" factory-bean="daoFactory" factory-method="getFactoryDaoImpl"></bean>
- 总结
- <bean name="..." class="..." scope="prototype">