【SSH】註解和非註解的形式配置Spring
一、前言
在框架學習中,我們學到了很多東西。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>
四、小結
通過不同的方法,可以達到相同的目的,這個在我們使用的時候就有不同的效果。第一種不使用註釋的,我們每次新增一條線,就需要我們在配置檔案中手動新增,而第二種,就需要我們在新增的線上,加入註解就可以了。