框架学习之spring整合struts2、hibernate-02AOP编程
2017-07-31 19:24
381 查看
面向切面编程
AOP?在运行时,动态地将代码切入到类的指定方法、指定位置上的编程思想就是面向切面的编程
1. 连接点(Joinpoint): 程序执行过程中明确的点,如方法的调用或特定的异常被抛出。
2. 切入点(Pointcut): 指定一个通知将被引发的一系列连接点的集合。
3. 通知(Advice): 在特定的连接点,AOP框架执行的动作。
4. spring通过动态代理来实现
AOP的实现和使用
applicationContext.xml配置
1. 在xml中配置切面
<aop:config> <aop:pointcut expression="execution(* com.bwf.book.dao.impl.*.*(..))" id="daoPointcut"/> <aop:aspect ref="logAdvice"> <aop:before method="write" pointcut-ref="daoPointcut" /> <aop:after-returning method="write2" returning="rt" pointcut-ref="daoPointcut" /> <aop:after-throwing method="write3" throwing="ex" pointcut-ref="daoPointcut" /> <aop:after method="write4" pointcut-ref="daoPointcut" /> <aop:around method="write5" pointcut-ref="daoPointcut" /> </aop:aspect> </aop:config>
说明:expression表示切面的切入点,logAdvice表示对应实现类的实例,由spring容器提供,method表示在该实现类中的通知,也就是具体的连接点的动作
在实现类中需要加注解:@Component(“logAdvice”)
通知包括有:“before”、“after-returning”、“after-throwing”和“around”;其中round通知类型需要调用proceed方法才会继续执行之后的程序,否则程序就不会再继续往下运行
2. 在类中配置注解完成切面设计
- 创建一个MyAspect类,在类上声明注解:@Aspect
- 在具体的方法(动作)上面声明注解:
@Before(通知类型)+切入点execution(“….”)
或者在一个方法上注解:
@Pointcut(“…”),在需要处理的方法上面直接调用这个方法@Before(“方法名()”)
3. 最后需要在applicationContext.xml文件中配置:
<!-- 在扫描组件类时,将切面实现类也扫描进来 --> <context:component-scan base-package="com.bwf.book" > <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/> </context:component-scan> <!-- 负责将容器中所有的“切面”(在类的声明上,使用了@Aspect注解的类)进行自动代理生成 --> <aop:aspectj-autoproxy />
相关文章推荐
- 学习三大框架Struts2 、 Hibernate 、 Spring时,涉及到三大框架的配置文件以及整合。
- hibernate-JPA,struts2,spring三大框架整合的步骤
- Struts2,Hibernate和Spring之间的框架整合
- 搭建整合Struts2,Spring,Hibernate的Web开发框架
- Activiti5工作流框架学习之Struts2+Hibernate+Spring整合配置
- struts2,spring,hibernate三大框架整合
- 框架学习之Spring 第五节 SSH整合开发[Spring2.5+Hibernate3.3+Struts2]
- Spring 学习日记 (四) Spring 整合Mybaits 和 struts2 框架的配置文件
- 框架(Spring、Struts2和Hibernate三者)整合
- 最近S2SH框架整合和简单测试 Struts2-2.3.14+Spring-4.0.6+Hibernate-4.3.6
- SSH三大框架整合(Hibernate,Spring,struts2)
- 整合开发Struts2,Hibernate,Spring简单框架的搭建详解
- spring3、struts2、hibernate3框架整合
- Struts2SpringHibernate框架的简单整合1
- Spring与Hibernate Struts2等框架的整合
- JAVA_WEB项目之Struts2、Spring和Hibernate三大框架的整合
- Spring 框架整合Struts2 框架和 Hibernate 框架
- SSH(spring、struts2、hibernate)三大框架整合
- struts2,hibernate,spring三大框架整合的注册程序(xml 配置,mysql数据库)
- spring框架的学习(七)——hibernate-5.0.7+struts-2.3.24+spring-4.2.4三大框架整合