0

Spring AOP in ViewModel

asked 2013-07-30 10:44:21 +0800

acirasa gravatar image acirasa
48 1

updated 2013-07-30 10:45:49 +0800

Hi,

I try to integrate Spring AOP in my ZK 6 + Shiro application.

I use BinderComposer+MVVM and I want to intercept Shiro Authorization exception from an aspect.

The problem is the creation and init of VieModel. If I create ViewModel from Spring, Zk not found Annotations (@Command @Init...) in the Proxy AOP object. If I create ViewModel from ZK, the class is out of Spring control.

Can anyone found a solution / workaround to this?

Best Regards

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2013-08-08 16:13:25 +0800

acirasa gravatar image acirasa
48 1

updated 2013-08-08 16:19:44 +0800

[SOLVED]

after two day of testing I found the solution and I want to share with you.

the trick is very simple: you can instument your tomcat class loader with a context.xml like this:

   <Context> 

      <Loader
loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"

</Loader>

    </Context>

this spring class instrument ALL class of your project for AOP. Next with aop.xml you can specify which class or package you want to monitoring:

<aspectj>

    <!-- definitions of aspects available to the weaver, and which
         ones should be used or not used -->
    <aspects>
       <aspect name="your.package.aspects.base.MyAspect"/>
    </aspects>

    <!-- control over the weaver itself and the types that will be
         woven -->
    <weaver options="-showWeaveInfo">
        <include within="your.base.package.viewmodels..*"/>
    </weaver>

with this you can monitoring all viewmodels in your package and subpakage, with an aspect class like this

    @Apect
public class MyAspect{

    @Around("execution(* your.base.package.viewmodels..*(..)) && @annotation(reqPermissions)")
        public Object permInterceptor(ProceedingJoinPoint point,RequiresPermissions reqPermissions) throws Throwable{
    ...
         }
    }

that's all

I hope it is useful for someone

best regards

Andrea

link publish delete flag offensive edit
0

answered 2019-01-04 00:41:21 +0800

hf gravatar image hf
0

updated 2019-01-18 12:26:21 +0800

cor3000 gravatar image cor3000
6280 2 7

Hi Andrea,

I tried your solution, but for me it doesn't work. In my applicationContext.xml I added this lines:

<aop:config>
    <aop:aspectj>

        <!-- definitions of aspects available to the weaver, and which ones should 
            be used or not used -->
        <aop:aspects>
            <aop:aspect name="it.hf.gerapweb.spring.LoggingAspect" />
        </aop:aspects>

        <!-- control over the weaver itself and the types that will be woven -->
        <aop:weaver options="-showWeaveInfo">
            <aop:include within="it.hf.gerapweb.bind.funzioni..*" />
        </aop:weaver>
    </aop:aspectj>
</aop:config>

The class LoggingAspect is:

package it.hf.gerapweb.spring;

import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Aspect
public class LoggingAspect {

    private static Logger log = LoggerFactory.getLogger(LoggingAspect.class);

    public LoggingAspect() {
        log.info("Costruttore");
    }


    @Around("execution(* it.hf.gerapweb.bind.funzioni..*(..))")
    public void invoke() {      
        log.info("Before");
        log.info("After");
    }

}

I added spring-instrument-tomcat-4.3.21.RELEASE.jar in my tomcat7 lib and in context.xml:

<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"></Loader>

Thanks a lot. Nico

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2013-07-30 10:44:21 +0800

Seen: 26 times

Last updated: Jan 18 '19

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More