AOP using AspectJ not working in spring?
Solution 1
First of all if you're going to use an annotation based configuration, use AnnotationConfigApplicationContext
instead of FileSystemXmlApplicationContext
. And get rid of the applicationContext.xml
file and simply add a @Bean
method in your configuration class. Something like this:
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "your.aspect.package")
public class AspectConfig {
@Bean
public AopTest aopTest() {
return new AopTest();
}
}
In your main
public class MainMethod {
public static void main(String[] args) {
AnnotationConfigApplicationContextcontext = new AnnotationConfigApplicationContext(AspectConfig.class);
// don't forget to refresh
context.refresh();
AopTest test = (AopTest)context.getBean("aopTest");
test.beforeAspect();
}
}
In AspectClass
you should have @Component
, @Aspect
, and your method should have the advice or pointcut annotation like @Before
. It needs to be a @Component
, so that Spring knows to scan it.
Solution 2
Here some code need to add in xml to use annotations- 1.for @component annotation.
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
2.after that use component scan to get all annotated bean class which use @component annotation,and use aop autoproxy-
<context:annotation-config/>
<context:component-scan base-package="mypackage"></context:component-scan>
<aop:aspectj-autoproxy>
</aop:aspectj-autoproxy>
for examples visit-www.technicaltoday.com/p/spring.html
Human Being
Always willing to learn the new things to enhance my knowledge .
Updated on August 01, 2020Comments
-
Human Being almost 4 years
My Aspect class will be ,
@Configuration @EnableAspectJAutoProxy @Component @Aspect public class AspectClass { @Before("execution(* com.pointel.aop.test1.AopTest.beforeAspect())") public void logBefore(JoinPoint joinPoint) { System.out.println("Before running the beforeAspect() in the AopTest.java class!"); System.out.println("Hijacked Method name : " + joinPoint.getSignature().getName()); System.out.println("************************"); } }
My other java Class
public class AopTest { public void beforeAspect() { System.out.println("This is beforeAspect() !"); } }
My Main Class is
public class MainMethod { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext("ApplicationContext/applicationContext.xml"); AopTest test = (AopTest)context.getBean("bean1"); test.beforeAspect(); } }
My applicationContext.xml is ,
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <bean id="bean1" class="com.pointel.aop.test1.AopTest" /> </beans>
In this the
@Before("execution(* com.pointel.aop.test1.AopTest.beforeAspect())")
in theAspectClass
will not be executed before thebeforeAspect()
in theAopTest
, when running Main method.Good answers are definitely appreciated.