Java spring-boot 属性不是@Autowired

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22138366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:50:29  来源:igfitidea点击:

spring-boot properties not @Autowired

javaspring-boot

提问by sonoerin

I am trying to get a Spring-boot application going and I am not sure what I am doing wrong here. I have a application.properties file at src/main/resources & src/test/resources. I have an @Bean for my ConfigurationSettings so that I can use them throughout my application:

我正在尝试启动一个 Spring-boot 应用程序,但我不确定我在这里做错了什么。我在 src/main/resources & src/test/resources 有一个 application.properties 文件。我的 ConfigurationSettings 有一个 @Bean,以便我可以在整个应用程序中使用它们:

@Component
public class ConfigurationSettings {

private String product;
private String version;
private String copyright;
private String appName;
private String appDescription;
... 
// getters and setters

}

Here is how I kick the application off:

这是我启动应用程序的方式:

@Configuration
@EnableJpaRepositories
@EnableAutoConfiguration
@EnableConfigurationProperties
@PropertySources(value = {@PropertySource("classpath:application.properties")})
@ComponentScan(basePackages = "com.product")
@EnableScheduling
public class OFAC {

public static void main(String[] args) {
    ApplicationContext ctx = SpringApplication.run( OFAC.class, args );        
}

And here is my configuration class:

这是我的配置类:

@Configuration
@ComponentScan(basePackages = {"com.product"})
@PropertySources(value = {@PropertySource("classpath:application.properties")})
public class OFAConfiguration {

     @Autowired
     private Environment env;

     @Bean
     public ConfigurationSettings configurationSettings() {
         ConfigurationSettings configurationSettings = new ConfigurationSettings();
         configurationSettings.setAppDescription( env.getRequiredProperty("app.description" ) );
         configurationSettings.setAppName( env.getRequiredProperty( "app.name" ) );
         configurationSettings.setServerPort( env.getRequiredProperty( "server.port" ) );
         return configurationSettings;
    }

I am trying to use it in a controller:

我试图在控制器中使用它:

@RestController
public class AboutController {

   @Autowired
   private ConfigurationSettings configurationSettings;

   @RequestMapping(value = "/about", method = RequestMethod.GET)
   public About index() {

     String product = configurationSettings.getProduct();
     String version = configurationSettings.getVersion();
     String copyright = configurationSettings.getCopyright();
    return new About( product, version, copyright );
   }
}

However, when step thru this, all the values of ConfigurationSettings are null. I do have a test that successfully loads the values:

但是,当逐步执行此操作时,ConfigurationSettings 的所有值都为空。我确实有一个成功加载值的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {OFAConfiguration.class})
public class OFAConfigurationTest {
  @Autowired
  private Environment environment;

  @Autowired
  private ConfigurationSettings configurationSettings;

  @Test
  public void testConfigurationLoads() {
    assertNotNull(environment);
    Assert.assertNotNull(configurationSettings);
  }

  @Test
  public void testConfigurationSettingValues() {
     assertEquals("Product Name", configurationSettings.getProduct());
    assertEquals("0.0.1", configurationSettings.getVersion());
    assertEquals("2014 Product", configurationSettings.getCopyright());
 }

Can anyone see why the ConfigurationSettings are not being populated in my Controller?

谁能看到为什么我的控制器中没有填充 ConfigurationSettings ?

采纳答案by M. Deinum

Your configuration leads to 2 instances of the ConfigurationSettingsclass and probably one instance overrides the other.

您的配置导致ConfigurationSettings类的2 个实例,并且可能一个实例覆盖另一个实例。

The 'ConfigurationSettings' has the @Componentannotation as you are scanning for components (@ComponentScan) this will lead to an instance. You also have a @Beanannotated method which also leads to an instance. The latter is overridden with the first.

'ConfigurationSettings' 有@Component注释,因为您正在扫描组件 ( @ComponentScan) 这将导致一个实例。您还有一个带@Bean注释的方法,它也指向一个实例。后者被第一个覆盖。

In short remove the @Componentannotation as that isn't needed because you already have a factory method for this class.

简而言之,删除@Component不需要的注释,因为您已经拥有此类的工厂方法。

public class ConfigurationSettings { ... }

You should also remove the @PropertySourceannotations as Spring-Boot will already load the application.propertiesfor you.

您还应该删除@PropertySource注释,因为 Spring-Boot 已经application.properties为您加载了。

Finally you should not use the @ContextConfigurationannotation on your test class but the @SpringApplicationConfigurationand pass in your application class (not your configuration class!).

最后,您不应@ContextConfiguration在测试类上使用注释,而应@SpringApplicationConfiguration在应用程序类中使用and 传递(而不是您的配置类!)。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=OFAC.class)
public class OFAConfigurationTest {

    @Autowired
    private Environment environment;

    @Autowired
    private ConfigurationSettings configurationSettings;

    @Test
    public void testConfigurationLoads() {
        assertNotNull(environment);
        assertNotNull(configurationSettings);
    }

    @Test
    public void testConfigurationSettingValues() {
        assertEquals("Product Name", configurationSettings.getProduct());
        assertEquals("0.0.1", configurationSettings.getVersion());
        assertEquals("2014 Product", configurationSettings.getCopyright());
    } 

This will fix your runtime configuration problems and will let your test use the power of Spring Boot to configure your application.

这将修复您的运行时配置问题,并让您的测试使用 Spring Boot 的强大功能来配置您的应用程序。