一尘不染

spring-boot属性不是@Autowired

spring-boot

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

@Component
public class ConfigurationSettings {

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

}

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

@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 );        
}

这是我的配置类:

@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;
    }

我正在尝试在控制器中使用它:

@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 );
   }
}

但是,通过此步骤时,ConfigurationSettings的所有值均为null。我有一个可以成功加载值的测试:

@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());
 }

谁能看到为什么未在我的控制器中填充ConfigurationSettings的原因?


阅读 314

收藏
2020-05-30

共1个答案

一尘不染

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

@Component扫描组件(@ComponentScan)时,“
ConfigurationSettings”具有注释,这将导致一个实例。您还具有带@Bean注释的方法,该方法也可以导致实例。后者被第一个覆盖。

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

public class ConfigurationSettings { ... }

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

最后,您不应@ContextConfiguration在测试类上使用批注,而应@SpringApplicationConfiguration在应用程序类(而非配置类!)中使用和。

@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());
    }

这将解决您的运行时配置问题,并使您的测试使用Spring Boot的功能来配置您的应用程序。

2020-05-30