配置类上添加注解@PropertySource加载外部配置文件
@Configuration @PropertySource("classpath:/person.properties") @ComponentScan(value = "com.yyc", includeFilters ={@ComponentScan.Filter(type= FilterType.CUSTOM, classes={MyTypeFilter.class})}, useDefaultFilters = false) public class MainConfig { // 可以自定义一个bean的id,否则方法名即为beanId @Bean("person") public Person person() { return new Person(); } }
@Value("13") private int age; @Value("${name}") private String name;
Person{age=13, name='zhangsan'}