Spring启动@Configurable

时间:2016-03-06 21:52:56

标签: spring spring-boot javabeans spring-aop

我正在尝试在spring-boot应用程序下的非spring托管类中配置Autowired。 我在tomcat服务器下部署的Web应用程序下运行成功。 但是,当我想在春季启动下运行时,没有任何作用。

我制作了非常简单的应用来检查此功能:

来自网络应用程序int tomcat的控制台结果:

...:::TEST CONTROLLER
...:::TEST autowired in cotrnoller: com.mycompany.test_aop.Test@30ea7445
...:::NEW
...:::Check: com.mycompany.test_aop.Test@30ea7445

Spring boot app的控制台结果:

...:::TEST CONTROLLER
...:::TEST autowired in cotrnoller: com.mycompany.test_aop.Test@627ae77d
...:::NEW
...:::Check: null

我阅读了很多热门话题来配置Spring启动应用程序,但没有任何作用。

我尝试将param -javaagent设置为spring-instrument.jar但没有效果。

我为此奋斗了三天但没有效果

应用

的pom.xml:

 <?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>pl.com.eurohost</groupId>
    <artifactId>aop_test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <repositories>
        <repository>
            <id>unknown-jars-temp-repo</id>
            <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
            <url>file:${project.basedir}/lib</url>
        </repository>
    </repositories>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.2.5.RELEASE</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.3.3.RELEASE</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-agent</artifactId>
            <version>2.5.6.SEC03</version>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.3.3.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Main.class:

package com.mycompany.test_aop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
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;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;

@SpringBootApplication
@EnableSpringConfigured
@EnableAspectJAutoProxy
@EnableLoadTimeWeaving
@ComponentScan(value = "com.mycompany.test_aop")
public class Main {

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

    @Bean
    public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
        InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
        return loadTimeWeaver;
    }
}

Web.java:

package com.mycompany.test_aop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class web {
    @Autowired
    Test t;

    @RequestMapping(value = "/")
    public @ResponseBody String root(){
        System.out.println("...:::TEST CONTROLLER");
        System.out.println("...:::TEST autowired in cotrnoller: "+t);
        Test2 a = new Test2();
        a.check();
        return "HI!";
    }
}

Test.java:

package com.mycompany.test_aop;

import org.springframework.stereotype.Service;


@Service
public class Test {
    public void display() {
        System.out.println("...:::TEST CLASS CALL DISPLAY");

    }
}

Test2.java

package com.mycompany.test_aop;

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

@Configurable
public class Test2 {
    @Autowired
    private Test t;

    public Test2() {
        System.out.println("...:::NEW");
    }

    public void check() {
        System.out.println("...:::Check: "+t);
    }


}

Webapp有不同的pom,addittional文件app-servlet.xml,没有主要方法:

Main.java

package com.mycompany.test_aop;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@EnableSpringConfigured
@EnableLoadTimeWeaving
@ComponentScan(value = "com.mycompany.test_aop")
@Configuration
@EnableWebMvc
public class Main extends WebMvcConfigurerAdapter {


}

APP-servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <mvc:default-servlet-handler/>

    <context:annotation-config/>
    <bean class="com.mycompany.test_aop.Main"/>


    <mvc:annotation-driven>
    </mvc:annotation-driven>

</beans>

1 个答案:

答案 0 :(得分:-1)

使用Autowire Annotations。

@Autowired
Test2 a;  

 @RequestMapping(value = "/")
    public @ResponseBody String root(){
        System.out.println("...:::TEST CONTROLLER");
        System.out.println("...:::TEST autowired in cotrnoller: "+t);
        Test2 a = new Test2();//instead of this use autowired of test2          
        a.check();
        return "HI!";                

如果你给thisTest2 a = new Test2();它将以新的Object启动,那么它将只显示NULL