1.使用注解的方式实现IOC和AOP
(a)注解式的IOC
(1)注解文件applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <!-- 包扫描:扫描哪些包下的注解可以被识别 --> <context:component-scan base-package="com.svse"></context:component-scan> <!-- 开启IOC注解配置 --> <context:annotation-config></context:annotation-config> |
(2)编写代码
Dao层
@Repository("usersDAO") public class UsersDAOImpl implements UsersDAO { ....... } |
Service层
@Service("usersService") public class UsersServiceImpl implements UsersService { @Autowired private UsersDAO usersDAO; ....... } |
注意:使用注解IOC的时候可以不用setter方法,进行注入的准备
Action层
@Controller("usersAction") public class UsersAction { @Autowired private UsersService usersService; .....
} |
(b)注解式的AOP
(1)配置文件applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> ,<context:component-scan base-package="com.svse"></context:component-scan> <!-- 开启AOP注解配置 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 事务管理bean --> <bean id="transactionManager" class=" org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"> </property> </bean> <!-- 使用事务注解 --> <tx:annotation-driven transaction-manager="transactionManager"/>
</beans> |
(2)代码编写:
(a)切面类
//切面 @Aspect public class SecurityHandlerAspect {
//该方法只是一个标记,没有任何含义,只是在这方法上写切入表达式,该方法的调用就是这个切入表达式的id @Pointcut("execution(* com.svse.dao.impl.*.*(..))") private void aspect() {}
@Before("aspect()") public void checkSecurity() { System.out.println("安全检查......"); }
@AfterReturning("aspect()") public void logcat() { System.out.println("日志记录..................."); } } |
(b)需要事物控制的类
@Service("usersService") public class UsersServiceImpl implements UsersService { @Autowired private UsersDAO usersDAO;
@Override @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class) public void add() { // TODO Auto-generated method stub usersDAO.add(); }
@Override @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class) public void update() { // TODO Auto-generated method stub usersDAO.update(); }
@Override @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class) public void delete() { // TODO Auto-generated method stub usersDAO.delete(); }
@Override @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true) public void find() { // TODO Auto-generated method stub usersDAO.find(); }
}
|