<util:properties id="config" location="jdbc.properties"></util:properties> @Value("#{config.username}") @Value("#{config[username]}") @Value("#{config['jdbc.username']}") <context:property-placeholder location="jdbc.properties" /> @Value("${jdbc.username}")
无论是注解还是xml方式,都必须提供非static的setter方法 主要:非static //注解实现 @Component public class AppConfig { public static String userName; @Autowired(required = true) public void setUserName(@Value("${jdbc.username}") String userName) { System.err.println(userName); AppConfig.userName = userName; } } //xml只有有非static的setter方法方法即可