Home  >  Spring Core

Example of @EnableAspectJAutoProxy in Spring

By Arvind Rai, March 23, 2013
@EnableAspectJAutoProxy in spring provides the same functionality as . It is used with @Configuration. All the classes which are annotated with @Aspect, need to configure in configuration file. Find the sample example.

UserAspect.java
package com.concretepage;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class UserAspect {
	@Before("execution(* com.concretepage.bean.UserService+.*(..))")
	public void userAdvice(){
		System.out.println("find my advice before your task.");
	}
}
 





AspectJAutoProxyConfig.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.concretepage.bean.UserService;

@Configuration
@EnableAspectJAutoProxy
public class AspectJAutoProxyConfig {
  @Bean
  public UserService userService(){
	 return new UserService();
  }
  @Bean	
  public UserAspect userAspect(){
	 return new UserAspect();
  }
}
 


AspectJAutoProxyTest.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.concretepage.bean.UserService;
public class AspectJAutoProxyTest {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		 
		ctx.register(AspectJAutoProxyConfig.class);
		ctx.refresh();
	
		UserService userService= ctx.getBean(UserService.class);
		userService.doTask();
	}
}
 


Output
find my advice before your task.
Do some task.
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us