首先,
Test应该为您的类添加注释,
@Component以便在spring之前将其注册为Bean(还要确保所有类都位于您的主程序包下-
主程序包是带有注释的类
@SpringBootApplication所在的位置)。
现在,您应该将所有属性移至
application.yml(
src/main/resources/application.yml),由Spring
Boot自动选择(请注意,它应
.yml代替
.yaml或注册一个custom)
PropertySourcesPlaceholderConfigurer。
示例
PropertySourcesPlaceholderConfigurer:
@Beanpublic static PropertySourcesPlaceholderConfigurer PropertySourcesPlaceholderConfigurer() throws IOException { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); MutablePropertySources propertySources = new MutablePropertySources(); Resource resource = new DefaultResourceLoader().getResource("classpath:application.yml"); YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader(); PropertySource<?> yamlProperties = sourceLoader.load("yamlProperties", resource, null); propertySources.addFirst(yamlProperties); configurer.setPropertySources(propertySources); return configurer;}现在,应该将属性加载到spring的环境中,并且可以将其注入
@Value到您的bean中。



