美文网首页
Spring @Configuration @Bean @Con

Spring @Configuration @Bean @Con

作者: 技术100万 | 来源:发表于2018-12-28 15:26 被阅读0次

    //配置

    @Configuration 

    public class ScheduleServiceConfig{

    //定义Bean

    @Bean

    public RestTemplate restTemplate(){

    RestTemplate restTemplate = new RestTemplate ();

    return restTemplate;

    }

    @Bean

    @ConfigurationProperties(prefix="email.props")//配置属性

    public EmailProps(){

    return new EmailProps();

    }

    }

    @Data//lombok添加getter,setter

    public class EmailProps{

    private String from;

    private String cc;

    private String bcc;

    private String to;

    }

    application.yml

    email:

            props:

                 from: xxx@a.com

                 cc: a@a.com

                 bcc: b@a.com

                 to: c@a.com

    @RestController

    public class EmailRestEndpoint{

        @Autowired

        private EmailProps emailProps;

       @RequestMapping(value="/test")

        public void test(){

            log.info("email props={}",emailProps.getFrom());

        }

    }

    相关文章

      网友评论

          本文标题:Spring @Configuration @Bean @Con

          本文链接:https://www.haomeiwen.com/subject/oczslqtx.html