3

Basically, what I want in the end, is that an object in some piece of code is wired up with my Spring Component, although that object is not created / managed by Spring.

The object in question is:

package demo;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

@Configurable(autowire= Autowire.BY_TYPE)
public class NotSoSpring {

  SpringComponent springComponent;

  public NotSoSpring() {
    springComponent.test();
  }

  @Autowired
  public void setSpringComponent(SpringComponent springComponent) {
    this.springComponent = springComponent;
  }

  public void test() {
    springComponent.test();
  }
}

And the component is as simple as that:

package demo;

import org.springframework.stereotype.Component;

@Component
public class SpringComponent {

  public void test() {
    System.out.println("Hello. Wow, this works!");
  }
}

The Spring configuration is done by the following piece of code:

package demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;

@SpringBootApplication
@EnableSpringConfigured
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "demo")
public class DemoApplication implements CommandLineRunner {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

  @Override
  public void run(String... strings) throws Exception {
    NotSoSpring spring = new NotSoSpring();
    spring.test();
  }
}

And here's the project's configuration:

<?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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.test</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>demo</name>
  <description>Demo project for Spring Boot</description>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath/>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
    </dependency>

    <dependency>
      <groupId>javax.el</groupId>
      <artifactId>javax.el-api</artifactId>
      <version>3.0.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

That's all the files, and here's the JVM command line option I used:

-javaagent:c:/.m2/repository/org/springframework/spring-instrument/4.1.5.RELEASE/spring-instrument-4.1.5.RELEASE.jar

So this results in

Exception in thread "main" java.lang.IllegalStateException: Failed to execute CommandLineRunner
  at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:675)
  at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
  at demo.DemoApplication.main(DemoApplication.java:19)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:497)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NullPointerException
  at demo.NotSoSpring.<init>(NotSoSpring.java:13)
  at demo.DemoApplication.run(DemoApplication.java:24)
  at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:672)
  ... 10 more

I looked through other SO questions, the Spring Documentation and a lot of blogs, but unfortunately they usually refer to Spring 3 and to XML based configurations.

Might be related to this question which has no solution either.

Community
  • 1
  • 1
cimnine
  • 3,987
  • 4
  • 33
  • 49
  • Add `spring-aspects` as a dependency. you can remove the `spring-aop` one as that is already provided by the starter. And you should be using the aspect agent not the spring instrument jar as that isn't adding much. – M. Deinum Sep 01 '15 at 12:42
  • @M.Deinum `spring-aspects` is there. I tried removing `spring-aop`, same thing. Using `org/aspectj/aspectjweaver/1.8.6/aspectjweaver-1.8.6.jar` results in n exception: `Caused by: java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar` – cimnine Sep 01 '15 at 13:00
  • @M.Deinum also, with the agent mentioned above in the question, the following is in the log: `2015-09-01 15:01:04.241 INFO 10892 --- [ main] o.s.c.w.DefaultContextLoadTimeWeaver : Found Spring's JVM agent for instrumentation` which seems right to me, no? – cimnine Sep 01 '15 at 13:03
  • How are you running this? Command line? IDE? Maven? Also when using lead-time weaving you shouldn't need auto proxy behavior so I would remove `@EnableAspectJAutoProxy` as that might conflict. Not sure if there is an issue with the custom Spring Boot class loader . – M. Deinum Sep 01 '15 at 13:11
  • You also might want to check https://github.com/spring-projects/spring-boot/issues/863 and https://github.com/spring-projects/spring-boot/issues/864 – M. Deinum Sep 01 '15 at 13:15
  • Read them, tried some advice, but no success. I removed `@EnableAspectJAutoProxy`. This did not help nor did it harm. – cimnine Sep 01 '15 at 13:31

0 Answers0