1

i really hope someone can help me, i am trying to create some very simple AOP Operations in my web-application. Unfortunately it is not triggered at all. Here my configs: web.xml (to load application.xml)

<!-- web.xml -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/application*.xml</param-value>
</context-param>

And the application.xml

<!--application.xml--> 
<aop:aspectj-autoproxy proxy-target-class="true" /> <!-- i tried with and without proxy-->
<bean id="config" class="com.whatever.config.Configuration"/> <!-- beans are not definend in application.xml, but in a separate class-->

The config class

 @Configuration
 @PropertySource({ "classpath:/ui.properties" })
 @Import({ somclass.class, ScanBean.class})
 public class Configuration {

 }

The scanbean class

 @Configuration
 @ComponentScan(basePackages = { "com.ui.common", "com.ui.aspects" })
 public class ScanBean {

   @Bean
   public DefaultEntityServiceAspect defaultEntityServiceAspect() {
     return new DefaultEntityServiceAspect();
   }
 }

Beans are there and everything starts without any error. The aspect beans... The annotation

 @Retention(RetentionPolicy.RUNTIME)
 public @interface DefaultEntity {}  
 // the aspect itself

 @Aspect
 public class DefaultEntityServiceAspect {

 private final Logger logger = Logger.getLogger(DefaultEntityServiceAspect.class);

   @Around("@annotation(com.ui.aspects.DefaultEntity)")
   public void setDefaultEntityFields(ProceedingJoinPoint joinPoint) throws Throwable {
      logger.warn("doing something 2 huhu");
      joinPoint.proceed();
   }
 }

And finally the bean itself

 @Service
 @Scope("session")
 public class AdminBean implements Serializable {
 // some code...

   @DefaultEntity
   public void wtfIsWrongWithYou() {
     logger.debug("am i working?");
   }
 }

If i write a simple JUNIT Test, it is working like expected. As soon as i deployed it to the server (tomcat). Nothing works anymore. I found a 1000 tips, but non of them was working. I think i read everything here on stackoverflow :) but a solution was not found. I would really appreciate every tip.

Cheers!

Edit:

I created a small project on github. I still did not get it run .. if someone has a hint, please let me know! Thank you!

EstevaoLuis
  • 2,422
  • 7
  • 33
  • 40
djnose
  • 917
  • 7
  • 19
  • Your aspect is never being picked up. a `@Aspect` isn't a `@Component` so it will never be picked up by your component scan. Also make sure you aren't scanning for the components twice! Make sure you are loading them only once. Also your aspect is flawed an around aspect should always return `Object` and you must return the result of the call to `proceed`. If you don't you have a high chance of breaking the proper functioning of your program as everything that this aspect applies to suddenly returns `null`. – M. Deinum Sep 12 '14 at 07:54
  • Sorry, missed to copy that part :-/. I edited the post. The aspect functionality works fine in a test ... Any other suggestions? Thank You! – djnose Sep 12 '14 at 08:00
  • Trust me your aspect is broken and will break programs. It works because yo now have it applied to void methods as soon as you apply it to methods that return something, it will break. As mentioned in my first comment make sure you aren't scanning for the same components twice if you do you will have a proxied and unproxied instance if you are unlucky. – M. Deinum Sep 12 '14 at 08:48
  • I changed the aspect now, so that it returns an object. Also i changed the application.xml to ``. Still it is not successful. Could you give me an example, of how it could happen that my components are scanned twice? Thank you! – djnose Sep 12 '14 at 09:49
  • Loading the same configuration twice (`ContextLoader` and `DispatcherServlet` for instance). – M. Deinum Sep 12 '14 at 09:59
  • Hi, i created now a very simple, stupid project. Also that is not running. [https://github.com/jwiesmann/aspectj] if someone would take a look and tell me what is wrong? Thank you! – djnose Sep 12 '14 at 11:56
  • 1
    Because, as I mentioned in my first comment, your `@Aspect` isn't a `@Component` and as such isn't detected. You have `` but no aspects... – M. Deinum Sep 12 '14 at 12:25
  • Yeah, my example was less than complete .. i guess i was to tired of trying different things. With your hint, it is now working in my "little" example. I will give it a try now in the "big" app. Thanks for your help! – djnose Sep 16 '14 at 08:16

1 Answers1

0

I finally made it work in my "big" application as well. The root cause was that i externalized all the spring / hibernate aso. libraries. In fact i was using the shared lib settings from tomcat and had all my spring libraries on that server. This is working fine as long as you do not use aspectj / spring aop. After i created a war file with the libs included (WEB-INF/lib) it was working immediately.

It also kind of makes sense, since the libs are loaded by different classloaders aso... hope it helps somebody as well!

djnose
  • 917
  • 7
  • 19
  • I took a look at your github project. You haven't used the maven-aspectj-plugin to compile the classes. How is your code working without the use of this? Please can you explain? Thanks. – Binoy Dalal Sep 28 '16 at 11:47
  • You might want to check out this post [link](http://stackoverflow.com/questions/1606559/spring-aop-vs-aspectj). I think it is very well explained there :) – djnose Sep 30 '16 at 09:59