I tried to use #configurable on my project to have #autowired service in object that are outside of spring context or something like that but didn't manage to make it work. The service is always null.
(I compared 4 different tutorials but nothing worked)
So, by despair I tried to download working examples directly to compare them but they didn't worked as well. (but I had to change them a bit, see in "Notes")
Here are the two examples I tried to download but gave me a nullPointerException too :
https://github.com/kenyattaclark/Spring-Configurable-Example
https://www.baeldung.com/spring-inject-bean-into-unmanaged-objects (the link is at the end of the page)
So, are these examples working on your side? Did I miss something really important?
Notes :
I'm using the correto version of java(but I suppose it's unlikely that the problem come from here)
I had to make some changes in the two examples ot make them work so maybe that's why :
for the first one I added a <pluginManagement> in the pom because if I didn't, eclipse was giving me an error "Plugin execution not covered by lifecycle configuration" and if I tried to compile I had the error : "error can't determine superclass of missing type java.lang.Object
when batch building BuildConfig[null] #Files=3 AopXmls=#0
[Xlint:cantFindType]"
for the second one I didn't use <pluginManagement>. But I didn't download the parent folder so I changed that in the pom and also forced the java version to 11 or maven was resetting it to 1.5 and had to update the junit test to junit5.
So there is room for me screwing all this up, but I'm tired of looking everywhere on internet and couldn't even get one working example.
So, if someone know what's wrong or how to make one example of #configurable work please tell me.
I looked at the first example. It uses very old versions of Spring, AspectJ and AspectJ Maven Plugin. I upgraded the POM to use
Spring 5.3.16,
AspectJ 1.9.9 (supports up to Java 18)
AspectJ.dev AspectJ Maven Plugin 1.13.1 (better and more up to date than Mojohaus, also supports Java 18 and can support any more recent version in the future by simply upgrading the aspectjtools plugin dependency, no plugin upgrade necessary).
Please use JDK 11+ on your build system. More recent AspectJ compiler versions need it. You can still compile to Java 8 byte code, though. (But why would you?)
Here is the Maven POM. I also added a separate dependency management section in order to globally manage dependency versions and exclusions. Then I used mvn dependency:analyze and mvn dependency:tree in order to clean up used undeclared and delcared unused dependencies. If your project needs another set of dependencies, you need to adjust it to your needs, of course. I configured it just for what is used in this tiny sample program. In doing so, I also cleaned out a few unused XML name-space and schema declarations. If you need them in your program, just add them again.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.brightdome</groupId>
<artifactId>spring-configurable-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Configurable Sample</name>
<description>
Sample project to show how to work with Spring's #Configurable capability
to inject dependencies into classes not instantiated by Spring.
</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<aspectj.version>1.9.9</aspectj.version>
<spring.version>5.3.16</spring.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>dev.aspectj</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.13.1</version>
<dependencies>
<!-- Override older AspectJ compiler in plugin, use latest one -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
<exclusions>
<!-- Only needed for load-time weaving, not compile-time -->
<exclusion>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!-- Needed by spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<!-- Needed by compile-time aspects during runtime -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
"
>
<context:spring-configured/>
<context:component-scan base-package="com.brightdome"/>
<context:annotation-config/>
</beans>
package com.brightdome.sample.spring;
import org.springframework.stereotype.Service;
#Service
public class HelloWorldService {
public void sayHello() {
System.out.println("Hello world!");
}
}
package com.brightdome.sample.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
#Configurable
public class HelloWorldClient {
#Autowired
private HelloWorldService service;
public void sayHello() {
// Used injected instance of service
service.sayHello();
}
}
package com.brightdome.sample.spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorld {
public static void main(String[] args) {
// Initialize Spring Context
new ClassPathXmlApplicationContext("/META-INF/applicationContext.xml");
// Instantiate class by new'ing it up. i.e., Do not obtain from Spring context
HelloWorldClient client = new HelloWorldClient();
client.sayHello();
}
}
This compiles and runs just fine in both my IDE (IntelliJ IDEA) and with Maven. On the console, it should simply print "Hello world!" and then exit.
Related
we are using eclipse+spring tool suite to build java application, we can start the application in eclipse IDE, but if we export as jar with all dependency jars, and run it on linux machine. we always gets error as following:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.ctrip.framework.apollo.demo.api.SimpleApolloConfigDemo.<clinit>(SimpleApolloConfigDemo.java:22)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
here is our POM.xml below:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>1.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-demo</artifactId>
<name>Apollo Demo</name>
<packaging>jar</packaging>
<properties>
<java.version>1.7</java.version>
<github.path>${project.artifactId}</github.path>
</properties>
<dependencies>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>${project.version}</version>
</dependency>
<!-- for spring demo -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- for spring boot demo -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- for refresh scope demo -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<!-- take over jcl -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>
2.1 we already included slf4j-log4j12 and slf4j-api as depdency.
2.2 the application can run normally in eclipse IDE window.
2.3 if we export executable jar including all dependencies, and ship jar on linux machine, run the jar file will get the above error.
2.4 we also checked generated jar package on windows with winRAR, the slf4j and log4j* jar package were there, see picture below.
we have struggled this for half day, but did not get any progress. Hope each expert can share with us some light or any suggestions. really appreciated!!!
You need to tell maven to compile the dependencys like this, the jars are compiled but you need the src in youre jar and not the jars in there:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<configuration>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
and dependencys normaly have a <scope>compile</scope> tag, to tell the compile that they are needed at runtime
I've faced a similar problem few days ago, right I add the jar libraries but the core (scripts/classes) of the jar file are no included due some reason I was able to solve that problem by right click on the jar file go to properties and set the class path for the classes of that jar folder.
if you wanna check that the jar file has no class Def inside it explore the jar file from the explorer and double click on any class you'll see a page showing up and says there's no script for this class and it asks you to set the class path.
Another approach I tried which also worked for me is I've imported the same jar files on netbeans and they worked 100%
I'm learning Spring from this tutorial:
http://courses.caveofprogramming.com/courses/the-java-spring-tutorial/lectures/38024
In this tutorial, instructor downloads spring dependencies (spring-beans, spring context, spring-core) in version 3.2.3.RELEASE.
and then writes this code:
package com.caveofprogramming.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
Person person = (Person)context.getBean("person");
person.speak();
}
}
When I use: spring-context, spring-beans and spring-core in last version 4.3.3.RELEASE then ApplicationContext import doesn't work. It works when I change it to the old version. "Doesn't work" means that eclips doesn't know what I should import when I write "ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");" and when I write "import org.springframework.context.ApplicationContext" by myself it's underline.
What should I do to import ApplicationContext with newest version dependencies?
Edit:
This is the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
ApplicationContext cannot be resolved to a type
FileSystemXmlApplicationContext cannot be resolved to a type
at com.caveofprogramming.spring.test.App.main(App.java:10)
and this is my pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.caveofprogramming.spring.test</groupId>
<artifactId>spring-tutorial-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
</dependencies>
</project>
Edit 2: I also saw that program accepts 4.1.1.RELEASE version. Mabye the newest version of dependencies isn't necessary? I'm just starting with Spring and everyone says that I should work on the newest version.
Edit 3;
The only solution which I found is using spring-context 4.1.1.RELEASE
Either add these jars in your class path org.springframework.context.support-3.1.0.RELEASE.jar and org.springframework.context-3.1.0.RELEASE.jar
Or add this dependency if you are using maven and update the project.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.x.x.RELEASE</version>
</dependency>
You have to add dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.x.x</version>
</dependency>
This might be a problem with eclipse, especially if you are using an older version. Eclipse used to have issues with Maven projects, when you added new dependencies.
You can force Eclipse to update it's Maven dependencies by right clicking your project and selecting Maven->Update project
After that your project should compile just fine:
PS: Check the current documentation for up-to-date setup of XML-based application context setup
I added this in my pom.xml file inside the dependencies tag.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.7</version>
<type>pom</type>
</dependency>
What it does is that it downloads the spring-context-5.3.7.jar file from which ApplicationContext class can be imported.
Hope it works for u too.
Here is my pom.xml file for better reference.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.Sharma</groupId>
<artifactId>NachoVarga</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>NachoVarga</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.7</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
When updating my maven project, the java compiler is changing from 1.7 to 1.5.
this is my pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.chart.simple</groupId>
<artifactId>core</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>core Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<java-version>1.7</java-version>
<org.springframework-version>4.0.3.RELEASE</org.springframework-version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>
</dependencies>
<build>
<finalName>core</finalName>
</build>
</project>
Help!!
The default compiler source and target level for Maven is 1.5.
"Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in "Setting the -source and -target of the Java Compiler"."
Source:
http://maven.apache.org/plugins/maven-compiler-plugin/
One reason that your attempt at setting "java-version" property has no effect is that "java-version" is not one of the recognized Maven built-in properties. This page lists the built-in properties that are available:
http://docs.codehaus.org/display/MAVENUSER/MavenPropertiesGuide
Note that though there is a "java.version" property, it is an (effectively) read-only that reports the java version of the JVM that is running Maven. It doesn't control the Java compiler source and target levels.
Well, if you don't specify an explicit version, you get the default.
While it might look like setting java.version is doing something to select a particular JVM, it isn't. It's just a name bound to a value. Maven can't switch JVMs after it is invoked, so you are probably overwriting the original value (which was probably 1.5). Keep in mind that if you have Java 1.5 installed, if it's before the other JVMs it will get invoked first. Also, you can have "maven specific" settings in any "maven launching" scripts, and other such items.
Configure your compiler plugin like so. Set a source and target version. Also, keep in mind that Maven doesn't "include" a compiler, so it will do it's best to configure the installed compiler to fit your declared source and target settings.
Add the compiler plugin
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
I usually put the above on the parent POM that way any projects under it which are JAR/EAR/WAR projects will use 1.7 without triggering the plugin as part of the build.
If you have a solitary JAR module just get rid of pluginManagement to keep things simpler.
I'm working on a multi-module maven project called acme-platform, with the modules set up like so:
acme-admin
acme-common
acme-global
acme-services
acme-client
acme-registration
acme-properties
acme-test
(They are listed in this order in the acme-platform pom.)
In some of the modules, I have been able to use Spring's ReflectionTestUtils class. However, in the last module, acme-test, where I really want to use it, I am unable to. There was no dependency section in the acme-test pom, so I added one. Here is the pom:
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>acme-platform</artifactId>
<groupId>com.awesomeness.acme</groupId>
<version>1.21.0</version>
<relativePath>../</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>acme-test</artifactId>
<version>1.21.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
Before adding the dependency lines, I couldn't import any of Spring's api into my classes. After importing these lines, I was able to access most of the classes, but not all of them, and in particular not ReflectionTestUtils, even though it is part of the spring-test module (as can be verified here).
I am using Intellij. I have looked at answers to other questions (such as this one) to make sure I'm updating my dependencies correctly. To no avail.
Does anyone have any idea as to why I can't import org.springframework.test.util.ReflectionTestUtils into acme-test?
Let me know if you need any aditional information.
EDIT
The version information of the dependencies are not in any of the module poms, but they are specified in the root pom (acme-platform). Again, I can import ReflectionTest in the other modules, just not in acme-test. So I deduce from this that as long as the dependency is declared with a specified version in the root pom, it doesn't need a version specified in any of the module poms. (If I'm wrong on this, please correct me.)
ADDITIONAL EDIT
By the way, I can't import junit either.
You need to set the Maven scope to test for both the spring-test and junit dependencies.
For example:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
I created aspectJ class in seperate Maven project:
#Aspect
public class AspectE {
#Pointcut("execution(#EntryPoint * *.*(..))")
public void defineEntryPoint() {
}
#Before("defineEntryPoint()")
public void setThreadName(JoinPoint joinPoint) {
...
}
#After("defineEntryPoint()")
public void removeThreadName(JoinPoint joinPoint) {
...
}
}
Then in second project I annotated several methods and added to pom.xml:
<dependency>
<groupId>first-project</groupId>
<artifactId>first-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.0</version>
</dependency>
But still aspects aren't seen at all. Am I missing some steps? What should I do?
Did you take a look at this?
AspectJ compiler Maven Plugin - Usage
In order to weave correctly your code with your libraries, you should declare them within your dependencies AND within the aspectj weaver:
<dependencies>
<!-- Aspectj lib -->
<dependency>
<groupId>com.my.group</groupId>
<artifactId>my-aspect-lib</artifactId>
<version>1.0</version>
</dependency>
<!-- Other dependencies -->
</dependencies>
<build>
<!-- Specific build configuration -->
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>com.my.group</groupId>
<artifactId>my-aspect-lib</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
</plugin>
<!-- Other plugins configuration -->
</plugins>
</build>
<!-- Other settings -->
You have to weave the aspects with the code. This can be done in 2 ways:
Compile-time weaving, using the AspectJ compiler Maven plugin as Andrei suggested
Load-time weaving (LTW), using an agent or a custom class-loader
Load-time weaving is a bit more versatile, but can be a bit challenging to set up properly. It consumes more CPU during startup (when the weaving happens), and also has a memory footprint.
Compile-time weaving consumes more CPU during the compilation, obviously, but then you don't pay the price on each restart.
I had the same problem ... but after I added this maven repo it's working
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>