Spring @Value property for custom class

后端 未结 3 1618
故里飘歌
故里飘歌 2021-01-04 10:04

Is it possible to use Spring\'s @Value annotation to read and write property values of a custom class type?

For example:

@Component
@PropertySource(\         


        
3条回答
  •  情书的邮戳
    2021-01-04 11:10

    It's possible but reading Spring documentation. You could see this example: Example usage

     @Configuration
     @PropertySource("classpath:/com/myco/app.properties")
     public class AppConfig {
         @Autowired
         Environment env;
    
         @Bean
         public TestBean testBean() {
             TestBean testBean = new TestBean();
             testBean.setName(env.getProperty("testbean.name"));
             return testBean;
         }
     }
    

    See details here

提交回复
热议问题