AspectJ NoSuchMethodError with Spring - java

I have a spring web rest project (3.0.4) running with aspectJ (1.8.14) for logging.
I have a pointcut defined in all methods that are annotated with #Logging.
When I call a generic helloWorld controller annotated with #Logging. AspectJ works fine.
When I call the real controller annotated with #Logging, which also happens to call a lib jar method that is also annotated with #Logging. Unfortunally, during the inner method call the error below occurs.
How does it come that only the method inside the lib has such error?
java.lang.NoSuchMethodError:
logging.aop.LoggingAspect.ajc$inlineAccessFieldGet$logging_aop_LoggingAspect$logging_aop_LoggingAspect$pLogger()Lorg/slf4j/Logger;
service.impl.DAOClass.getMethod_aroundBody1$advice(DAOClass.java:101)
service.impl.DAOClass.getMethod(DAOClass.java:1)
What I tried: I checked jdk compiled versions, jar versions in maven dependencies, web.xml and aop.xml configuration and installing AJDT plugin for eclipse photon to debug. I tried to deploy in weblogic to see the class loader, but I was not able to, trying in tomcat i cannot see what is loaded or not.
P.S: please do not downgrade the question for lack of code, i would have to make a complete mock to avoid showing company code for now I am looking for people that worked with AOP with spring and has some idea what I am using based on this info plus the error stack. I can make a mock code later if nobody guess what is the cause until there.
Thanks!

No SuchMethodError usually happens when runtime class path contains different version/both version of jar the library was compile against. You should check the pom dependency against runtime class path. I bet it was promoted to newer version which is not backward compatible. How do you package your application, spring boot?

Related

ERROR [org.springframework.web.servlet.DispatcherServlet]

My code is built in maven and using jBoss 6 and java 7
The code is working fine on local machine but when i try running war on server i an getting error
ERROR [org.springframework.web.servlet.DispatcherServlet]
I tried using aop jar but still no success
Can some one explain me the error and how to solve it
It is failing during the creation of your #Beans. In particular, it is failing because NoClassDefFoundError: org/springframework/core/convert/converter/ConvertingComparator which means it can't find the Class definition for "ConvertingComparator". The earliest documentation I can find on the ConvertingComparator is Spring
3.2.0. Bear in mind, you are using Spring 3.1.0.RELEASE. I tried running a basic Spring project using your POM and ran into similar issues using the spring version you provided. I also ran into conflicts with Spring 3.2.0. I recommend using Spring 4.3.5.RELEASE in your POM. I had no issues running my basic example after setting the properties you have to:
<properties>
<org.springframework.version>4.3.5.RELEASE</org.springframework.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
That being said, you may have other issues in your code as well. It is difficult to provide a holistic solution without more information.
EDIT:
So here are all your spring dependencies:
spring-beans
spring-web
spring-webmvc
spring-tx
spring-jdbc
So first lets talk about redundancy. spring-webmvc actually contains spring-web and spring-beans. You can therefore remove those dependencies from your POM as they are redundant. spring-jdbc contains spring-tx, rendering that inclusion also redundant. You can remove all of those from your POM right now for clean up.
In your comment, you mentioned a new error being thrown in regards to package org.springframework.mail not being found. This package is found in spring-context-support. Spring context support is actually found in the spring-webmvc as well as an optional dependency. (so you would have to include it manually)
According to this thread, that package was moved into context-support separately. My guess is that you are trying to specifically use some of the objects available in the .mail package and because you were not including it, it wasn't being found. With more information as to your project, the more we can dive into why 3.2 did not work and 4.1 does work. Specifics aside, what this all means is that the 3.2 dependencies you were calling did not expose the required packages, but the 4.1 dependencies do.
Have you stopped the process, I mean that particular bean could be in-use ?

wildfly 10 - javax.servlet.ServletContainerInitializer: Provider org.springframework.web.SpringServletContainerInitializer not a subtype

I am using Spring 4.3 and Servlet 3.1.
Deployment fails with error
javax.servlet.ServletContainerInitializer: Provider org.springframework.web.SpringServletContainerInitializer not a subtype.
I know that it is some wrong class is loading
The Spring jars and Servlet 3.1 jars are configured as a module and mentioned in module.xml.
javax.servlet.ServletContainerInitializer is present in only one jar jboss-servlet-api_3.1_spec-1.0.0.Final.jar that I have included in module.xml.
SpringServletContainerInitializer is needed for my application.
Let me know if you need me to post more details.
Any help is appreciated.
I had the same problem and solved it by goig through my dependencies to see if anything crashed with my application-servers libraries.
Make sure your java-ee and java-ee-web dependencies are set to "provided" scope in your maven/gradle configuration.
I was getting the same error and after researching, I identified somehow SpringServletContainerInitializer was not loading because of Maven packaging issue. In my case, spring-web-4.3.8.Release.jar was not bundling correctly with application WAR.
This can be fixed by
resolving application bundling conflict
using server shared libraries
if using IDE for application bundling, one can avoid bundling using IDE or switch the IDE (like STS to Eclipse/RAD)
Another potential source of extra copies of servlet-api and other similar Java EE APIs is Spring Boot's tomcat-embed-core-<version>.jar, which you may get from spring-boot-starter-web.

Maven - what the correct scope for JDBC jar files?

For my development station, I need my project to "see" the JDBC drivers. But when I deploy the project to the server, if the JDBC drivers are in the /lib folder they'll cause the container to misbehave.
I tried setting the <scope>provided</scope> to the drivers I don't want to package on my .war file, but I can't get my IDE to run the project.
What's a correct scope to declare the JDBC drivers on pom.xml so they don't get packaged for deploy and I can work with them on my development station? <scope>runtime</scope>?
Thanks in advance,
The short answer for your question is: you should use provided scope.
Why not runtime? Let's check Maven docs:
provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
runtime
This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
So, Maven may not expose runtime scoped dependencies in compile's classpath, thus you won't be able to use them in you code directly. However, the code like Class.forName("class.from.runtime.Scope") will compile ok.
I guess the problem is your IDE which didn't seized pom.xml changes. Usually, this problem is fixed by "cleaning cache" or "updating" / "syncing" your project. Here is how to do this in Eclipse and IDEA.

Spring Aop Dependencies

NOTE: Please ignore if I ask any kind of stupid questions. Apologies in advance.
PROBLEM: I am new to spring and spring-aop. Everything was going great untill #Aspect annotation is used in java class and aop:aspectj-autoproxy in spring.xml
I have included following jars in runtime classpath:
aspectjrt.jar
aspectjweaver.jar
cglib.jar
asm.jar
aopalliance.jar
Now i am facing an issue of NoClassDefFoundError for org.aspectj.lang.annotation.Around.class although this class exist in aspectjrt.jar
NOTE: I am using Spring-4.1
QUESTION: What are the jars that are required for Spring-AOP? If I have all the jars (as far as i have searched, i have all the jars that are required), then is there any version compatibility issue.
Any kind of help will be much appreciated.
Did you add spring-aop? The easiest way would be to use maven for building (or similar dependency management tool) and add dependency to spring-aop
http://mvnrepository.com/artifact/org.springframework/spring-aop/4.1.0.RELEASE
As of Spring 3.2, it is no longer necessary to add CGLIB to your project classpath, so when using Spring-4.1 you can ignore it
the only jar you need to work Aop on spring project is is aspectjweaver,
version 1.6.8 or later This library is available in the 'lib' directory of an AspectJ distribution or via the Maven Central repository.=> here

Indirectly referenced from required .class files

I'm getting below error in STS:
The type org.springframework.core.env.EnvironmentCapable cannot be resolved. It is indirectly referenced from required .class files
This sounds like a transitive dependency issue. What this means is that your code relies on a jar or library to do something - evidently, you depend on Spring framework code. Well, all that Spring code also depends on libraries and jars.
Most likely, you need to add the corerctly versioned org.springframework.core jar to your classpath so that the EnvironmentCapable class can be found when your IDE attempts to build your project.
This might also be a jar collision issue as well, although that sounds less likely. When an application experiences jar collision (also known as "dll hell"), the compiler is finding multiple jars and classes with the same fully-qualified name. For example, let's say you added Spring to your classpath, along with the entire Tomcat server library. Well, those two jars may contain the same exact named classes, maybe the same version, maybe different versions. But either way, when the compiler looks for that EnvironmentCapable class, it finds two (in this contrived example) - one in the Spring jar and one in the Tomcat jar. Well, it doesn't know which one to choose, and so it throws a ClassDefNotFoundException, which would/could manifest itself as the error you experienced.
I faced same error while i work with spring security on spring-security-config.i jsut deleted that jar in maven repo and gave maven->update Project in eclipse.
it is resolved.Please try it once.
From command line, run "mvn clean install", you'll see project failed and you'll see artifacts in the logs that cause such a problem.
After that, remove artifacts from .m2/repository, then maven update from eclipse.
To avoid jar collision, make sure you declare your dependency versions under the properties tag in the aggregate pom.xml, and use the property name as a placeholder throughout the project. For example 4.2.5.RELEASE in the parent pom, and then in the child modules just use ${spring.version} instead of 4.2.5.RELEASE. This way you can avoid having two different versions of the same library on the classpath.
Also it is recommended to be consistent with the version of spring dependencies. Use the same version for spring-core, spring-web etc.
If you are using maven, then you can use the maven enforcer plugin to ensure dependency convergence, and avoid further issues with transitive dependencies.

Categories

Resources