2016-03-06 104 views
1

我試圖在spring引導應用程序下的非spring管理類中配置Autowired。 我在tomcat服務器下部署的web應用程序下成功運行。 但是,當我想在春季開機運行這個沒什麼用。Spring boot @Configurable

我做了很簡單的應用程序,以檢查此功能:

從Web應用程序INT tomcat的控制檯結果:

...:::TEST CONTROLLER 
...:::TEST autowired in cotrnoller: [email protected] 
...:::NEW 
...:::Check: [email protected] 
從春天啓動的應用程序

控制檯結果:

...:::TEST CONTROLLER 
...:::TEST autowired in cotrnoller: [email protected] 
...:::NEW 
...:::Check: null 

我讀了很多話題熱點配置春季啓動應用程序,但沒有任何作品。

我試着將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; 
    } 
} 

網絡。 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

通過使用自動裝配註解。

@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 =新的Test2();它將與新的對象,然後開始將只顯示NULL