春季启动@Configurable [英] Spring boot @Configurable

查看:100
本文介绍了春季启动@Configurable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Spring Boot应用程序下的非spring托管类中配置 Autowired .我在Tomcat服务器下部署的Web应用程序下成功运行了此操作.但是,当我想在春季靴子上运行此软件时,没有任何效果.

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

Web应用程序int tomcat的控制台结果:

  ... ::: TEST CONTROLLER... ::: TEST自动连线到cotrnoller:com.mycompany.test_aop.Test@30ea7445...:::新的... :::检查:com.mycompany.test_aop.Test@30ea7445 

Spring Boot应用程序的控制台结果:

  ... ::: TEST CONTROLLER... ::: TEST在cotrnoller中自动接线:com.mycompany.test_aop.Test@627ae77d...:::新的... :::检查:null 

我阅读了许多热门话题,以配置Spring Boot应用程序,但没有任何效果.

我尝试将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><存储库><存储库>< id> unknown-jars-temp-repo</id>< name>由NetBeans为无法识别的库和jar创建的临时存储库.请使用正确的库替换该库中的依赖项,然后删除该库.</name>< url>文件:$ {project.basedir}/lib</url></存储库></存储库><依赖关系><依赖关系>< groupId> org.springframework.boot</groupId>< artifactId> spring-boot-starter</artifactId><版本> 1.3.3.发布</version></dependency><依赖关系>< groupId> org.springframework.boot</groupId>< artifactId> spring-boot-starter-web</artifactId><版本> 1.3.3.发布</version></dependency><依赖关系>< groupId> org.springframework.boot</groupId>< artifactId> spring-boot-starter-tomcat</artifactId><版本> 1.3.3.发布</version></dependency><依赖关系>< groupId> org.springframework.boot</groupId>< artifactId> spring-boot-starter-aop</artifactId>< version> 1.3.3.RELEASE</version></dependency><依赖关系>< groupId> org.springframework.boot</groupId>< artifactId> spring-boot-starter-actuator</artifactId><版本> 1.3.3.发布</version></dependency><依赖关系>< groupId> com.sun.jersey</groupId>< artifactId>球衣-json</artifactId>< version> 1.19</version></dependency><依赖关系>< groupId> com.sun.jersey</groupId>< artifactId>球衣-servlet</artifactId>< version> 1.19</version></dependency><依赖关系>< groupId> org.springframework.boot</groupId>< artifactId> spring-boot-maven-plugin</artifactId><版本> 1.3.3.发布</version></dependency><依赖关系>< groupId> org.springframework</groupId>< artifactId> spring-aspects//artifactId>< version> 4.2.5.RELEASE</version>< type> jar</type></dependency><依赖关系>< groupId> org.springframework</groupId>< artifactId> spring-aop</artifactId>< version> 4.2.5.RELEASE</version></dependency><依赖关系>< groupId> org.aspectj</groupId>< artifactId> aspectjweaver</artifactId>< version> 1.8.6</version></dependency><依赖关系>< groupId> org.springframework</groupId>< artifactId> spring-instrument</artifactId>< version> 4.2.5.RELEASE</version></dependency><依赖关系>< groupId> org.springframework.boot</groupId>< artifactId> spring-boot-starter-parent</artifactId>< version> 1.3.3.RELEASE</version>< type> pom</type></dependency><依赖关系>< groupId> org.springframework</groupId>< artifactId> spring-agent</artifactId>< version> 2.5.6.SEC03</version></dependency></dependencies><属性>< 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><版本> 1.3.3.发布</version><执行><执行><目标>< goal>重新包装</goal></goals></execution></executions></plugin></plugins></build></project> 

Main.class:

  package com.mycompany.test_aop;导入org.springframework.boot.SpringApplication;导入org.springframework.boot.autoconfigure.SpringBootApplication;导入org.springframework.context.annotation.Bean;导入org.springframework.context.annotation.ComponentScan;导入org.springframework.context.annotation.EnableAspectJAutoProxy;导入org.springframework.context.annotation.EnableLoadTimeWeaving;导入org.springframework.context.annotation.aspectj.EnableSpringConfigured;导入org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;@SpringBootApplication@EnableSpringConfigured@EnableAspectJAutoProxy@EnableLoadTimeWeaving@ComponentScan(值="com.mycompany.test_aop")公共班级主要{公共静态void main(String args []){SpringApplication.run(Main.class,args);}@豆角,扁豆public InstrumentationLoadTimeWeaver loadTimeWeaver()抛出Throwable {InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();返回loadTimeWeaver;}} 

Web.java:

  package com.mycompany.test_aop;导入org.springframework.beans.factory.annotation.Autowired;导入org.springframework.stereotype.Controller;导入org.springframework.web.bind.annotation.RequestMapping;导入org.springframework.web.bind.annotation.ResponseBody;@控制器公共类网站{@Autowired测试t;@RequestMapping(值="/")公共@ResponseBody字符串root(){System.out.println("... ::: TEST CONTROLLER");System.out.println("... ::: TEST自动连接到cotrnoller:" + t);Test2 a =新的Test2();a.check();返回"HI!";}} 

Test.java:

  package com.mycompany.test_aop;导入org.springframework.stereotype.Service;@服务公开课测试{公共无效的display(){System.out.println("... ::: TEST CLASS CALL DISPLAY");}} 

Test2.java

  package com.mycompany.test_aop;导入org.springframework.beans.factory.annotation.Autowired;导入org.springframework.beans.factory.annotation.Configurable;@可配置公共课程Test2 {@Autowired私人测试t;公共Test2(){System.out.println("... ::: NEW");}公共无效check(){System.out.println("... ::: Check:" + t);}} 

Web应用程序具有不同的pom,附加文件app-servlet.xml且没有主要方法:

Main.java

  package com.mycompany.test_aop;导入org.springframework.context.annotation.ComponentScan;导入org.springframework.context.annotation.Configuration;导入org.springframework.context.annotation.EnableLoadTimeWeaving;导入org.springframework.context.annotation.aspectj.EnableSpringConfigured;导入org.springframework.web.servlet.config.annotation.EnableWebMvc;导入org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@EnableSpringConfigured@EnableLoadTimeWeaving@ComponentScan(值="com.mycompany.test_aop")@配置@EnableWebMvc公共类Main扩展了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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://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:注释驱动></mvc:注释驱动></beans> 

解决方案

通过使用自动装配注释.

  @Autowired测试2@RequestMapping(值="/")公共@ResponseBody字符串root(){System.out.println("... ::: TEST CONTROLLER");System.out.println("... ::: TEST自动连接到cotrnoller:" + t);Test2 a = new Test2();//代替自动使用test2a.check();返回"HI!"; 

如果给thisTest2 a = new Test2();它将以new Object初始化,那么它将仅显示NULL

I'm trying to configure Autowired in non-spring managed class under spring boot application. I run successfully this under web application deployed under tomcat server. But when i want run this under spring boot nothing works.

I made very simple app to check this functionality:

Console result from web app int tomcat:

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

Console result from spring boot app:

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

I read a lot of topics hot to configure spring boot app but nothing works.

I trying set param -javaagent to spring-instrument.jar but no effect.

I struggle with this for three days but no effect

App:

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 has different pom, addittional file app-servlet.xml and no main method:

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>

解决方案

By using 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!";                

If you give thisTest2 a=new Test2();It will initiate with new Object then it will show only NULL

这篇关于春季启动@Configurable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆