3

Hi I have an application using Spring 5, Spring Data and Spring AOP and Java 11 (I am using JAVA 9 module system as well). My spring-context/spring-aspects version is 5.1.2.RELEASE Spring Data version is 2.1.2.RELEASE Both the versions are the latest available. I have created repositories in following manner:

@Repository
public interface AirportRepository extends JpaRepository<Airport, Long> {
}

I have enabled Spring Data Repositories using

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:persistence-jndi.properties")
@PropertySource("classpath:hibernate.properties")
@EnableJpaRepositories(basePackages = "com.xx.yy.persistence")
public class PersistenceJNDIConfig {
  //Datasource configs
}

It was working perfectly until I introduced Spring AOP, using following configuration.

@ComponentScan(basePackages = "com.xx.yy")
@EnableAspectJAutoProxy(exposeProxy = true)
public class AdminServicesConfiguration {
}

And

@Aspect
@Component
public class DasErrorHandler {

    @Around("@target(com.xx.yy.commons.aop.ErrorManagedDAS)")
    public Object handle(ProceedingJoinPoint proceedingJoinPoint){
        //Some code
    }
}

But now on server startup I am getting following error:

Caused by: java.lang.IllegalStateException: Cannot convert value of type 'com.sun.proxy.$Proxy82 implementing org.springframework.data.util.Streamable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments' for property 'repositoryFragments': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:299)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:585)
    ... 103 more

It seems there is a mismatch in the proxy types generated by Spring which is causing the above error, I tried forcing spring to use CGLIB proxies by making:

@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true )

then the error changes to following, even when I have SpringProxy class on my classpath. Please help:

Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class org.apache.tomcat.dbcp.dbcp2.BasicDataSource: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.NoClassDefFoundError-->org/springframework/aop/SpringProxy
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:208)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:473)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:352)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:301)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:434)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1749)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
    ... 88 more
Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.NoClassDefFoundError-->org/springframework/aop/SpringProxy
    at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:503)
    at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:359)
    at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:582)
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:106)
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:104)
    at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
    at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java)
    at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
    at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
    at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:130)
    at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:315)
    at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:569)
    at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:416)
    at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:58)
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:205)
    ... 95 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/SpringProxy
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.System$2.defineClass(System.java:2123)
    at java.base/java.lang.invoke.MethodHandles$Lookup.defineClass(MethodHandles.java:962)
    at java.base/jdk.internal.reflect.GeneratedMethodAccessor42.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:498)
    ... 110 more
Caused by: java.lang.ClassNotFoundException: org.springframework.aop.SpringProxy
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 117 more

Edit: module-info.java for modules are as follows

module com.xx.yy.admin.services {
    //External
    requires spring.webmvc;
    requires spring.web;
    requires spring.context;
    requires javax.servlet.api;
    requires org.apache.commons.collections4;
    requires org.apache.logging.log4j;
    requires org.apache.logging.log4j.core;
    requires spring.aop;
    requires org.aspectj.weaver;
    requires spring.aspects;

    //Internal
    requires com.xx.yy.utils;
    requires com.xx.yy.persistence;
}

module com.xx.yy.persistence {
    requires spring.context;
    requires spring.beans;
    requires spring.core;
    requires spring.data.commons;
    requires spring.data.jpa;
    requires spring.tx;
    requires spring.jdbc;
    requires spring.orm;
    requires spring.aop;
    requires java.sql;
    requires java.persistence;
    requires java.naming;
    requires java.xml;
}

module com.xx.yy.utils {
    requires com.fasterxml.jackson.core;
    requires com.fasterxml.jackson.databind;
    requires com.fasterxml.jackson.datatype.jsr310;
    requires orika.core;
    requires spring.context;
    requires org.aspectj.weaver;
    requires spring.aspects;
}
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
Abhisar
  • 186
  • 8
  • Thats not spring AOP but AspectJ ;P – Antoniossss Nov 10 '18 at 09:21
  • Are you [using a compatible version of Spring with Java11](https://stackoverflow.com/questions/51427248/minimum-spring-version-compatible-with-java-11/51429027#51429027) ? – Naman Nov 10 '18 at 09:25
  • @Antoniossss This is Spring AOP using AspectJ annotations. Spring supports AspectJ annotations. – Abhisar Nov 10 '18 at 09:47
  • @nullpointer Yes I am, I have updated the question with versions – Abhisar Nov 10 '18 at 09:49
  • @Abhisar Since [you're creating a modular project](https://stackoverflow.com/questions/53237556/spring-data-repositories-not-working-after-enabling-spring-aop-java-11#comment93361595_53237771), can you also share your module declarations and dependencies (with versions) in use? – Naman Nov 10 '18 at 11:55
  • @nullpointer I have highlighted the versions and added module-info.java now – Abhisar Nov 10 '18 at 12:53
  • Judging from the stack trace you mis the requires for the commons-dbcp2 project. – M. Deinum Nov 10 '18 at 13:01
  • @ M. Denim : it didnt work even after that....as I have mentioned in the question that, it was working before integrating spring aop. So it should not be a dependency issue. – Abhisar Nov 10 '18 at 13:15
  • Aside: You can make sure, the spring dependency bring in a [compatible version of aspectJ](https://www.eclipse.org/aspectj/doc/released/README-192.html) as well. – Naman Nov 11 '18 at 02:17
  • @nullpointer It is compatible....Finally I had to ditch spring AOP and use pure AspectJ instead. – Abhisar Nov 27 '18 at 08:26

1 Answers1

0

Try adding AOP lib to your dependencies

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.1.2.RELEASE</version>
</dependency>
Antoniossss
  • 31,590
  • 6
  • 57
  • 99