< context:component- scan base-package= "cn.itheima03.spring.aop.annotation" ></context :component-scan> |
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >
<!--
1、把所有的bean放入到spring容器中,启动类扫描机制的注解
2、启动AOP的注解
-->
<context:component-scan base-package="cn.itheima03.spring.aop.annotation" ></context:component-scan>
<aop:aspectj-autoproxy></ aop:aspectj-autoproxy>
</beans>
|
//配置注解,需要产生实例
@Repository("classesDao" )
public class ClassesDaoImpl extends HibernateUtils implements ClassesDao{
public String
saveClasses(Classes classes) {
sessionFactory.getCurrentSession().save(classes);
return "aaaa" ;
}
public List<Classes>
getClasses() {
return sessionFactory .getCurrentSession().createQuery("from
Classes").list();
}
public void updateClasses(Classes
classes) {
sessionFactory.getCurrentSession().update(classes);
}
}
=============================
/**
* @Aspect该注解说明该类是一个切面类 ,等效于:
* <aop:aspect ref="myTransaction">
* <aop:pointcut expression="execution(* cn.itheima03.spring.aop.annotation.ClassesDaoImpl.*(..))"
* id=" aa()"/>
* </aop:aspect>
*/
@Component("myTransaction" )
@Aspect
public class MyTransaction extends HibernateUtils{
private Transaction transaction;
@Pointcut("execution(*
cn.itheima03.spring.aop.annotation.ClassesDaoImpl.*(..))")
private void aa(){} //方法签名
方法的修饰符最好为private,返回值必须是void
@Before("aa()")
public void beginTransaction(){
this.transaction = sessionFactory.getCurrentSession().beginTransaction();
}
@AfterReturning( "aa()")
public void commit(){
this.transaction .commit();
}
}
|
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/damogu_arthur/article/details/46988405