我正在尝试使用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;
    }

我正在尝试在 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 );
   }
}

但是,通过此步骤时,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());
 }

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

最佳答案

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

在扫描组件(@Component)时,“ConfigurationSettings”具有@ComponentScan批注,这将导致一个实例。您还可以使用带有@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的功能来配置您的应用程序。

10-07 21:01