Reputation: 8387
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 the AspectClass
will not be executed before the beforeAspect()
in the AopTest
, when running Main method.
Good answers are definitely appreciated.
Upvotes: 5
Views: 28617
Reputation: 10562
I don't see your AspectClass
in the beans configuration. You should also declare it as a Bean.
Upvotes: 0
Reputation: 2297
You are missing the point cut definition in your aspect class.
For example;
@Pointcut("execution(* *.advice(..))")
public void logBefore(){}
@Before("logBefore()")
public void beforeAdvicing(){
System.out.println("Listen Up!!!!");
}
You first have to defin the point to weave your aspect to. You do this by using Point cuts.It is the point cut name you give within your @Before annotation. Have a look at my blog post for more information @ http://dinukaroshan.blogspot.com/2010/06/aop-with-spring.html
Upvotes: 1
Reputation: 11
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
Upvotes: 1
Reputation: 279880
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.
Upvotes: 4