Announcement Announcement Module
Collapse
No announcement yet.
Can methods of POJO be advised? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can methods of POJO be advised?

    Hi, I am a beginner to Spring AOP. I have a question, that is, can methods of POJO be advised? For example, in JPetStore, the method setAccountName() of Class Account, can I define it as a pointcut?

    In the referrence manual, I saw that "If the target object does not implement any interfaces then a CGLIB proxy will be created". So I tried to use this
    Code:
    <aop:config proxy-target-class="true">
    <aop:advisor pointcut="execution(* *..Account.set*(..))" advice-ref="account" />
    </aop:config>
    <bean id="account" class="org.springframework.samples.jpetstore.domain.AccountValidator">
    but it did't work... Is there anything wrong?

    Thanks very much~

  • #2
    Do you see that necessary aspect is initialized from the logs?

    You may post complete small example that illustrates the problem here. I.e. it would include just target pojo, advice class and spring config.

    Comment


    • #3
      My question should be : How to make the domain objects be advised?

      Sorry for making the question unclear.

      After reading more of the spring-referrence, I found that my question should be : How to make the domain objects be advised?

      In 6.8.1 Using AspectJ to dependency inject domain objects with Spring, it is said that domain object which is out of the scope of the Spring container, can be advised using @Configurable annotation. But I don't understand where to write my pointcut and advice? Is there any examples of this?

      Comment


      • #4
        You need to use AspectJ weaver then. I posted an example of load-time weaving here.

        Comment


        • #5
          Thank you so much~~ It helps a lot

          Comment


          • #6
            I don't know if you use Maven, but I just posted a reply on compile-time weaving taking care of @Configurable.

            http://forum.springframework.org/showthread.php?t=62472

            Comment


            • #7
              Hi,

              I thing that I'm having the same problem. I'd like to know if is possible define a pointcut to intercept methods in hibernate entity? These entities is not defined in the spring, only my dao is in the applicationContext. When I define a pointcut or advice in the spring managed object (dao, session, services) everything works fine, but in entities instantiated by hibernate it seems don't work.
              @Before("execution(* net.weg.eng.dao.*Dao.save*(..)) && args(obj,..)")
              public void logChanges(Object obj) {
              }

              Anyone has an tip?

              Thanks,
              Cleiton.

              Comment


              • #8
                AspectJ integration - 6.8. Using AspectJ with Spring applications.

                I referenced complete load-time weaving example above.

                Comment


                • #9
                  Cleiton,

                  You need to use AspectJ weaving if you wish to advise non-beans.

                  -Ramnivas

                  Comment


                  • #10
                    Hi, when I put <context:spring-configured/> in my application context I'm receiving the following message. Anyone knows this problem?

                    ERROR-Context initialization failed
                    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.context.config.internalBeanCo nfigurerAspect': Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionSt oreException: Factory method [public static org.springframework.beans.factory.aspectj.Annotati onBeanConfigurerAspect org.springframework.beans.factory.aspectj.Annotati onBeanConfigurerAspect.aspectOf()] threw exception; nested exception is org.aspectj.lang.NoAspectBoundException: Exception while initializing org_springframework_beans_factory_aspectj_Annotati onBeanConfigurerAspect: java.lang.InstantiationError: org.springframework.beans.factory.wiring.BeanConfi gurerSupport
                    at org.springframework.beans.factory.support.Construc torResolver.instantiateUsingFactoryMethod(Construc torResolver.java:396)
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateUsingFactory Method(AbstractAutowireCapableBeanFactory.java:936 )
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:851)
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:514)
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:485)
                    at java.security.AccessController.doPrivileged(Native Method)
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:455)
                    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 51)
                    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:169)
                    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:248)
                    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:170)
                    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:413)
                    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:735)
                    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:369)
                    at org.springframework.web.context.ContextLoader.crea teWebApplicationContext(ContextLoader.java:251)
                    at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:190)
                    at org.springframework.web.context.ContextLoaderListe ner.contextInitialized(ContextLoaderListener.java: 45)
                    at org.apache.catalina.core.StandardContext.listenerS tart(StandardContext.java:3727)
                    at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4162)
                    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1012)
                    at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:718)
                    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1012)
                    at org.apache.catalina.core.StandardEngine.start(Stan dardEngine.java:442)
                    at org.apache.catalina.startup.Embedded.start(Embedde d.java:821)
                    at org.codehaus.mojo.tomcat.AbstractRunMojo.startCont ainer(AbstractRunMojo.java:253)
                    at org.codehaus.mojo.tomcat.AbstractRunMojo.execute(A bstractRunMojo.java:113)
                    at org.apache.maven.plugin.DefaultPluginManager.execu teMojo(DefaultPluginManager.java:579)
                    at org.apache.maven.lifecycle.DefaultLifecycleExecuto r.executeGoalAndHandleFailures(DefaultLifecycleExe cutor.java:498)
                    at org.apache.maven.lifecycle.DefaultLifecycleExecuto r.executeTaskSegmentForProject(DefaultLifecycleExe cutor.java:265)
                    at org.apache.maven.lifecycle.DefaultLifecycleExecuto r.executeTaskSegments(DefaultLifecycleExecutor.jav a:191)
                    at org.apache.maven.lifecycle.DefaultLifecycleExecuto r.execute(DefaultLifecycleExecutor.java:149)
                    at org.apache.maven.DefaultMaven.execute_aroundBody0( DefaultMaven.java:223)
                    at org.apache.maven.DefaultMaven.execute_aroundBody1$ advice(DefaultMaven.java:304)
                    at org.apache.maven.DefaultMaven.execute(DefaultMaven .java:1)
                    at org.apache.maven.embedder.MavenEmbedder.execute_ar oundBody2(MavenEmbedder.java:904)
                    at org.apache.maven.embedder.MavenEmbedder.execute_ar oundBody3$advice(MavenEmbedder.java:304)
                    at org.apache.maven.embedder.MavenEmbedder.execute(Ma venEmbedder.java:1)
                    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java :176)
                    at org.apache.maven.cli.MavenCli.main(MavenCli.java:6 3)
                    at org.apache.maven.cli.MavenCli.main(MavenCli.java:5 2)
                    Caused by: org.springframework.beans.factory.BeanDefinitionSt oreException: Factory method [public static org.springframework.beans.factory.aspectj.Annotati onBeanConfigurerAspect org.springframework.beans.factory.aspectj.Annotati onBeanConfigurerAspect.aspectOf()] threw exception; nested exception is org.aspectj.lang.NoAspectBoundException: Exception while initializing org_springframework_beans_factory_aspectj_Annotati onBeanConfigurerAspect: java.lang.InstantiationError: org.springframework.beans.factory.wiring.BeanConfi gurerSupport
                    at org.springframework.beans.factory.support.SimpleIn stantiationStrategy.instantiate(SimpleInstantiatio nStrategy.java:127)
                    at org.springframework.beans.factory.support.Construc torResolver.instantiateUsingFactoryMethod(Construc torResolver.java:387)
                    ... 39 more

                    Comment


                    • #11
                      Did you put META-INF/aop.xml in the right place? I think I have encountered the same problem before...
                      At first I put META-INF/aop.xml in WebRoot/META-INF/aop.xml, but it can't be found. Then I put META-INF/aop.xml into the src folder, and it worked.
                      I'm not sure whether it is the same problem...

                      Comment


                      • #12
                        I put in classification\src\main\webapp\META-INF\aop.xml
                        I'm using maven.

                        Comment


                        • #13
                          This is not my problem because I set to aspectJ to generate a message and the messange was printed.

                          <weaver options="-verbose -showWeaveInfo -XmessageHandlerClass:org.springframework.aop.aspec tj.AspectJWeaverMessageHandler">

                          Comment


                          • #14
                            My problem was only version. I updated maven dependencies to 2.5.5 and xxaspectxx to 1.6 then finally it works.

                            Comment

                            Working...
                            X