【Spring五】AOP之使用注解配置
来源:互联网 发布:威客网络兼职 编辑:程序博客网 时间:2024/05/22 07:52
AOP使用注解配置流程:
1、当spring容器启动时候,
< context:component- scan base-package= "cn.itheima03.spring.aop.annotation" ></context :component-scan>
2、在上面的包及子包中查询所有的类,按照类扫描注解的机制把类放入到spring容器中
3、 检查是否配置:<aop:aspectj-autoproxy> </aop: aspectj-autoproxy>
4、查找哪些类上加@Aspect注解,会查找加了该注解的所有的方法,看方法上是否有 @Pointcut注解
5、把 @Pointcut的切入点表达式解析出来和spring中的bean进行匹配,如果匹配成功,则创建代理对象
6、 生成的代理对象的方法=通知+ 目标方法
完整代码:
1.配置文件:appliactionContext.xml
<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>
2.给相关类配置注解:
//配置注解,需要产生实例
@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();
}
}
0 0
- 【Spring五】AOP之使用注解配置
- Spring学习之使用注解配置AOP
- 使用注解配置spring aop
- 使用注解配置Spring AOP
- Spring实现AOP方式之二:使用注解配置 Spring AOP
- Spring Aop实例之AspectJ注解配置
- Spring Aop实例之AspectJ注解配置
- 使用注解配置spring中的aop
- Spring AOP注解配置
- Spring AOP 注解配置
- Spring---AOP(注解配置)
- spring aop注解配置
- Spring AOP 注解配置
- spring-aop注解配置
- spring之基于aspectj注解aop使用
- Spring 之AOP 使用@AspectJ注解方式
- spring(五)AOP注解学习
- Spring框架(五)AOP之注解方式
- 互联网创业者真有必要再建设网站吗
- 沿着ogre来实现material system(一)
- POJ 题目2796 Feel Good(动态规划)
- 关于IOS深浅拷贝的深入分析
- Leetcode #62 Unique Paths
- 【Spring五】AOP之使用注解配置
- synopsys DC
- iOS工程自动打包 (Shell脚本打包)
- C++函数重载
- 如何快速成为数据分析师?
- GRE写作必备句型
- PAT 00-自测1. 打印沙漏(20)
- 几何模板
- MSSQL Server 创建分区表