SSM框架项目搭建系列(七)—Spring AOP之基于注解的声明式AspectJ
2016-11-03 19:18
861 查看
工程结构
其中AOP和com.ssm包下面的文件不用管;dispatcher-servlet.xml和web.xml和之前项目中的内容一样。
applicationContext.xml
Person.java
AdivceMethod.java
AopTest.java
其中AOP和com.ssm包下面的文件不用管;dispatcher-servlet.xml和web.xml和之前项目中的内容一样。
applicationContext.xml
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--自动搜索切面类--> <context:component-scan base-package="AOP2"/> <!--启动@AspectJ支持 默认是false--> <aop:aspectj-autoproxy proxy-target-class="true"/> </beans>
Person.java
package AOP2; import org.springframework.stereotype.Component; /** * DateTime: 2016/11/2 21:35 * 功能: * 思路: */ @Component public class Person { public void eatBreakfast(){ System.out.println("......eatBreakfast()早餐......"); } public void eatLunch(){ System.out.println("......eatLunch()午餐......"); } public void eatSupper(){ System.out.println("......eatSupper()晚餐......"); } }
AdivceMethod.java
package AOP2; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; /** * DateTime: 2016/11/2 21:40 * 功能: * 思路: */ @Component @Aspect public class AdivceMethod { @Before("execution(* AOP2.Person.*(..))") //匹配Person中的所有方法 public void beforeEat(){ System.out.println("...吃饭之前洗手..."); } @After("execution(* AOP2.Person.eatLunch())") //匹配eatLunch方法 public void afterEat(){ System.out.println("...吃饭之后洗碗..."); } @Around("execution(* AOP2.Person.eatSupper())") //匹配eatSupper方法 public Object aroundEat(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ System.out.println("........吃饭前先逛一逛........"); Object value = proceedingJoinPoint.proceed(); System.out.println("........吃完后要睡觉了........"); return value; } }
AopTest.java
package AOP2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * DateTime: 2016/11/2 22:10 * 功能: * 思路: */ public class AopTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); AOP2.Person person= (AOP2.Person) context.getBean("person"); System.out.println(); person.eatBreakfast(); System.out.println(); person.eatLunch(); System.out.println(); person.eatSupper(); System.out.println(); } }
相关文章推荐
- SSM框架项目搭建系列(六)—Spring AOP之基于XML的声明式AspectJ
- 10、SSM框架-Spring AOP之基于注解的声明式AspectJ(10)
- SSM搭建-Spring AOP之基于注解的声明式AspectJ(9)
- SSM搭建-Spring AOP之基于XML的声明式AspectJ(8)
- SSM框架项目搭建系列(五)—Spring之Bean的注解注入
- 9、SSM框架-Spring AOP之基于XML的声明式AspectJ(9)
- SSM框架项目搭建系列(四)— Spring之bean的XML注入方式
- SSM基于注解配置Spring4.1.6、springMVC4.1.6、mybatis3.2.8的maven框架搭建
- Intellij IDEA创建Spring项目之基于Aspectj的AOP操作(注解方式)
- Spring(十二)AspectJ框架开发AOP(基于注解)
- SSM框架项目搭建系列(三)—Spring中的Bean生命周期
- J2EE项目系列(四)--SSM框架构建积分系统和基本商品检索系统(Spring+SpringMVC+MyBatis+Lucene+Redis+MAVEN)(2)建立商品数据库和Lucene的搭建
- SSM框架项目搭建系列(二)—Spring第一个HelloWorld
- Spring基于注解@AspectJ的AOP
- Spring AOP的使用 基于全注解AspectJ
- Spring基于注解@AspectJ的AOP
- Spring(十一)AspectJ框架开发AOP(基于xml)
- Spring3.2.0-mybatis3.2.0 基于全注解搭建的后台框架-基础版
- Spring-AOP:基于AspectJ注解的AOP
- SpringMVC经典系列-04基于Spring3.1注解的方式进行项目开发---【LinusZhu】