一、前言
在框架学习中,我们学到了很多东西。spring 2.5 的一大增强就是引入了很多注释类,现在您已经可以使用注释配置完成大部分 XML 配置的功能。在这篇文章里,我们将向您讲述使用注释进行 Bean 定义和依赖注入的内容。
二、非注解怎么做?
在spring 2.5以前,没有注解。不使用注解,我们就需要在spring的applicationContext.xml文件中,添加上要注入的类的实例。原理是当我们需要实例化这个实例,用这个类下面的方法的时候,spring会自动帮我们实例化,从而减少了我们的操作。
通过下面的代码来表示一下:
首先由三个类:
DepartmentDaoImpl :
public class DepartmentDaoImpl extends HibernateDaoSupport implements DepartmentDao {
public void saveDepartment(Department department) {
this.getHibernateTemplate().save(department);
}
}
DepartmentServiceImpl :
public class DepartmentServiceImpl implements DepartmentService {
private DepartmentDao departmentDao;
public DepartmentDao getDepartmentDao() {
return departmentDao;
}
public void setDepartmentDao(DepartmentDao departmentDao)
{
this.departmentDao = departmentDao;
}
public void saveDepartment(Department department) {
this.departmentDao.saveDepartment(department);
}
}
DepartmentAction :
public class DepartmentAction extends BaseAction implements ModelDriven<Department> {
private Department department = new Department();
public Department getModel() {
return department;
}
private DepartmentService departmentService;
public DepartmentService getDepartmentService() {
return departmentService;
}
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
/**
* 添加操作-2017年5月10日10:48:11
* @return string
*/
public String add() {
this.departmentService.saveDepartment(department);
return action2action;
}
}
在上面的三个类中,DepartmentServiceImpl 中注入了DepartmentDao,在DepartmentAction 中注入了DepartmentService。
在spring中,我们需要注入这些:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="departmentDao" class="cn.itcast.oa.dao.impl.DepartmentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="departmentService" class="cn.itcast.oa.service.impl.DepartmentServiceImpl">
<property name="departmentDao" ref="departmentDao"/>
</bean>
<bean id="departmentAction" class="cn.itcast.oa.struts2.action.DepartmentAction" scope="prototype">
<property name="departmentService" ref="departmentService"/>
</bean>
</beans>
当我们注入的时候,就可以直接使用了。
三、注解怎么做?
同样的我们需要这三个类,但是需要有一些变动,那就是添加了注解,取消了get和set方法:
DepartmentDaoImpl :
@Repository("departmentDao")
public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements
DepartmentDao<Department> {
}
DepartmentServiceImpl :
@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {
@Resource(name="departmentDao")
private DepartmentDao departmentDao;
public void saveDepartment(Department department) {
this.departmentDao.saveEntry (department);
}
}
DepartmentAction :
@Controller("departmentAction")
@Scope("prototype")
public class DepartmentAction extends BaseAction implements ModelDriven<Department> {
private Department department = new Department();
public Department getModel() {
return department;
}
@Resource(name="departmentService")
private DepartmentService departmentService;
/**
* 添加操作-2017年5月10日10:48:11
* @return string
*/
public String add() {
this.departmentService.saveDepartment(department);
return action2action;
}
}
在上面的三个类中我们通过注解实现了注入,我们分别说明一下注解:
@Repository(“departmentDao”)
可以理解为要标志是D层。
@Service(“departmentService”)
可以理解为要标志是B层的。
@Resource(name=”departmentDao”)
这个是DepartmentServiceImpl为了注入DepartmentDao而引入的。可以理解为为了注入D层而写的,可以得到D层的示例。
@Controller(“departmentAction”)和@Scope(“prototype”)
这个可以理解为Action的标志
@Resource(name=”departmentService”)
同 @Resource(name=”departmentDao”),表示注入了DepartmentService。
当然,通过写了注入,我们需要在spring的ApplicationContext.xml中进行配置。这里要声明的是spring2.5 以上 ,如下:
在导入类扫描的注解解析器和事务的注解解析器,会自动扫描我们填写了注解的类。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 导入类扫描的注解解析器和事务的注解解析器 -->
<context:component-scan base-package="cn.itcast.oa"></context:component-scan>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
四、小结
通过不同的方法,可以达到相同的目的,这个在我们使用的时候就有不同的效果。第一种不使用注释的,我们每次添加一条线,就需要我们在配置文件中手动添加,而第二种,就需要我们在添加的线上,加入注解就可以了。