二级类型依赖注入在 spring 引导中不起作用
Secondary type dependency injection does not work in spring boot
根据文档,spring 引导将自动检查在任何使用 @Configuration 注释的 classes 中创建的 bean class 对象,并将覆盖该 class & return 具有按定义注入的任何属性的对象。但是当我在 junit 中测试这个应用程序时,它没有 return 任何被注入的值。我所有的 classes 都在同一个包中定义我的代码如下,
//引擎class
package com.test.simpletest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Engine {
private String msg;
public Engine() {
System.out.println("Engine class is being called");
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
//测试配置class
package com.test.simpletest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfiguration{
@Bean
public Engine engine() {
Engine eng = new Engine();
eng.setMsg("Message is being called");
return eng;
}
}
//Spring 启动主应用程序
package com.test.simpletest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SimpleTestExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SimpleTestExampleApplication.class, args);
}
}
//JUnit测试class
package com.test.simpletest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SimpleTestExampleApplicationTests {
@Autowired
private Engine engine;
@Test
public void contextLoads() {
engine.getMsg();
//Both above and below approach does not work
// ApplicationContext apx = new
AnnotationConfigApplicationContext(TestConfiguration.class);
// Engine engine = (Engine)apx.getBean(Engine.class);
// engine.getMsg();
}
}
请帮我找到解决上述问题的方法。
能否请您从引擎中删除@Component class 然后重试。我想应该没问题。
演示应用程序
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
引擎
public class Engine {
private String msg;
public Engine() {
System.out.println("Engine class is being called");
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
测试配置
@Configuration
public class TestConfiguration {
@Bean
public Engine getEngine() {
Engine eng = new Engine();
eng.setMsg("Message is being called");
return eng;
}
}
DemoApplicationTests
@RunWith(SpringRunner.class)
@SpringBootTest
@Import(TestConfiguration.class)
public class DemoApplicationTests {
@Autowired
private Engine engine;
@Test
public void contextLoads() {
System.out.println("engine : " + engine.getMsg());
}
}
输出
Engine class is being called
engine : Message is being called
根据文档,spring 引导将自动检查在任何使用 @Configuration 注释的 classes 中创建的 bean class 对象,并将覆盖该 class & return 具有按定义注入的任何属性的对象。但是当我在 junit 中测试这个应用程序时,它没有 return 任何被注入的值。我所有的 classes 都在同一个包中定义我的代码如下,
//引擎class
package com.test.simpletest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Engine {
private String msg;
public Engine() {
System.out.println("Engine class is being called");
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
//测试配置class
package com.test.simpletest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfiguration{
@Bean
public Engine engine() {
Engine eng = new Engine();
eng.setMsg("Message is being called");
return eng;
}
}
//Spring 启动主应用程序
package com.test.simpletest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SimpleTestExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SimpleTestExampleApplication.class, args);
}
}
//JUnit测试class
package com.test.simpletest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SimpleTestExampleApplicationTests {
@Autowired
private Engine engine;
@Test
public void contextLoads() {
engine.getMsg();
//Both above and below approach does not work
// ApplicationContext apx = new
AnnotationConfigApplicationContext(TestConfiguration.class);
// Engine engine = (Engine)apx.getBean(Engine.class);
// engine.getMsg();
}
}
请帮我找到解决上述问题的方法。
能否请您从引擎中删除@Component class 然后重试。我想应该没问题。
演示应用程序
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
引擎
public class Engine {
private String msg;
public Engine() {
System.out.println("Engine class is being called");
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
测试配置
@Configuration
public class TestConfiguration {
@Bean
public Engine getEngine() {
Engine eng = new Engine();
eng.setMsg("Message is being called");
return eng;
}
}
DemoApplicationTests
@RunWith(SpringRunner.class)
@SpringBootTest
@Import(TestConfiguration.class)
public class DemoApplicationTests {
@Autowired
private Engine engine;
@Test
public void contextLoads() {
System.out.println("engine : " + engine.getMsg());
}
}
输出
Engine class is being called
engine : Message is being called