package com.hub.mymvn.aopanno;
package com.hub.mymvn.aopanno;
import org.springframework.stereotype.Component;
@Component
publicclassEmployee {
private String name;
public String getName() {
returnname;
}
@Loggable
publicvoid setName(String name) {
System.out.println("Start to set name as "+name);
this.name = name;
}
publicvoid throwException(){
thrownew RuntimeException("Dummy Exception");
}
}
====================== EmployeeService
package com.hub.mymvn.aopanno;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
publicclass EmployeeService {
@Autowired
private Employee employee;
public Employee getEmployee(){
returnthis.employee;
}
publicvoid setEmployee(Employee e){
this.employee=e;
}
}
====================== EmployeeAnnotationAspect
package com.hub.mymvn.aopanno;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
publicclassEmployeeAnnotationAspect {
@Before("@annotation(com.hub.mymvn.aopanno.Loggable)")
publicvoid myAdvice(){
System.out.println("Executing myAdvice!!");
}
/*
@Before("execution(public String getName())")
public void getNameAdvice(){
System.out.println("Executing Advice on getName()");
}
@Before("execution(* com.journaldev.spring.service.*.get*())")
public void getAllAdvice(){
System.out.println("Service method getter called");
}
*/
}
======================
package com.hub.mymvn.aopanno;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.validation.BindException;
import org.springframework.validation.Validator;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class DemoTest {
@Autowired
private EmployeeService employeeService;
@Test
public void helloTest() {
System.out.println("Start helloTest");
Employee e = employeeService.getEmployee();
System.out.println(e);
System.out.println(e.getName());
e.setName("XXX");
System.out.println(e.getName());
System.out.println("End helloTest");
}
}
======================运行结果
Start helloTest
com.hub.mymvn.aopanno.Employee@7f5338bf
null
Executing myAdvice!!
Start to set name as XXX
XXX
End helloTest
相关推荐
spring中自定义注解(annotation)与AOP中获取注解.通过实例演示自定义注解。
Spring_Annotation_AOP
Spring mvc Aop+annotation实现系统日志记录功能实现的jar包asm-3.3.jar ,aspectjrt.jar , aspectjweaver.jar , cglib-nodep-2.1_3.jar , spring-aop.jar
NULL 博文链接:https://bijian1013.iteye.com/blog/2142528
这里zip压缩包囊括了学习Spring过程中用到的所有的jar包; 有: commons-logging-1.2.jar spring-beans-4.0.4.RELEASE.jar spring-context-4.0.4.RELEASE.jar spring-core-4.0.4.RELEASE.jar spring-expression-...
NULL 博文链接:https://tianhei.iteye.com/blog/978969
NULL 博文链接:https://baobeituping.iteye.com/blog/1201798
NULL 博文链接:https://dyygusi.iteye.com/blog/1995858
spring-aop实践项目,基于annotation方式的面向切面实践小项目
aop入门。
在微服务流行的当下,在使用SpringCloud/Springboot框架开发中,AOP使用的非常广泛,尤其是@Aspect注解方式当属最流行的,不止功能强大,性能也很优秀,还很舒心!所以本系列就结合案例详细介绍@Aspect方式的切面的...
NULL 博文链接:https://whp0731.iteye.com/blog/356167
NULL 博文链接:https://tuoxinquyu.iteye.com/blog/1465187
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/548314
自己动手做的一个spring aop 的小demo,主要是annotation标签的配置联系,比较简单.
虽然Spring AOP的切点表达式尚未 全部实现 @AspectJ 的切点指示符,但是也已经足够多和灵活,本文主要介绍@Pointcut切点表达式中的@within、within这两种切点指示符,结合案例,十分钟让你彻底搞懂! 文末已附源码,...
nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: ...
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo
@Component("userService")等价于在spring配置文件中定义一个<bean id="userService"/> @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework....