Announcement Announcement Module
Collapse
No announcement yet.
AOP example not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP example not working

    Hi there,

    iam trying to see the AOP concepts in Spring 3.0 and here is my code

    Code:
    public class User {
    
    	private String userName;
    
    	private long userId;
    
    	private String company;
    
    	public String getCompany() {
    		return company;
    	}
    
    	public void setCompany(String company) {
    		this.company = company;
    	}
    
    	public long getUserId() {
    		return userId;
    	}
    
    	public void setUserId(long userId) {
    		this.userId = userId;
    	}
    
    	public String getUserName() {
    		return userName;
    	}
    
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    }
    Code:
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    
    
    @Aspect
    public class LogInterceptor {
    	 @Pointcut("execution(public * *(..))")
    	  public void aMethod() {}
    
    	  @Before("aMethod()")
    	  public void beginPreProcess()
    	  {
    	    System.out.println("pre stringMethod advice now executing ... ");
    	  }
    
    }
    Code:
    <?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-2.0.xsd 
    	      http://www.springframework.org/schema/aop 
    	      http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    
    	<aop:aspectj-autoproxy />
    
    	<bean id="user" class="User" />
    
    </beans>
    Code:
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class App {
    	public static void main(String[] args) {
    		ApplicationContext ctx = new ClassPathXmlApplicationContext(
    				new String[] { "ApplicationContext.xml" });
    
    		User u = (User) ctx.getBean("user");
            u.setUserName("Spring");
    		System.out.printf(u.getUserName());
    	}
    }
    i cant see the AOP getting triggered.

    i expected "pre stringMethod advice now executing ..." to be printed before i print the UserName.

    could any body correct me if i had made any mistake ?

    Thanks

  • #2
    adding
    <bean class="LogInterceptor"/>

    to applicationcontext.xml solved the problem.

    But now im getting thsi error

    Code:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [ApplicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalAccessError
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:543)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:730)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:387)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    	at App.main(App.java:6)
    Caused by: java.lang.IllegalAccessError
    	at net.sf.cglib.core.ClassEmitter.setTarget(ClassEmitter.java:45)
    	at net.sf.cglib.core.ClassEmitter.<init>(ClassEmitter.java:37)
    	at net.sf.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:165)
    	at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    	at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    	at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
    	at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117)
    	at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
    	at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
    	at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
    	at java.lang.J9VMInternals.initializeImpl(Native Method)
    	at java.lang.J9VMInternals.initialize(J9VMInternals.java:177)
    	at org.springframework.aop.framework.Cglib2AopProxy.createEnhancer(Cglib2AopProxy.java:228)
    	at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:170)
    	at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:476)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:404)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1401)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
    	... 11 more
    could any body help me ?

    Comment


    • #3
      The same example works with me
      please check your classpath

      atleaset you must need the following jar file
      Code:
      	<classpathentry kind="lib" path="lib/spring-beans.jar"/>
      	<classpathentry kind="lib" path="lib/spring-core.jar"/>
      	<classpathentry kind="lib" path="lib/aspectjrt.jar"/>
      	<classpathentry kind="lib" path="lib/aspectjweaver.jar"/>
      	<classpathentry kind="lib" path="lib/spring-aop.jar"/>
      	<classpathentry kind="lib" path="lib/aopalliance.jar"/>
      	<classpathentry kind="lib" path="lib/spring-context.jar"/>
      	<classpathentry kind="lib" path="lib/commons-logging.jar"/>
      	<classpathentry kind="lib" path="lib/cglib-nodep-2.1_3.jar"/>

      Comment

      Working...
      X