public void setName(String name) {
this.name = name;
}
}
这个很简单,没啥好说的。
接下来就是我们的重轴戏,自动配置类的定义,用了很多别人定义的自定义类之后,我们也来自己定义一个自定义类。先来看代码吧,一会松哥再慢慢解释:
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setName(helloProperties.getName());
helloService.setMsg(helloProperties.getMsg());
return helloService;
}
}
关于这一段自动配置,解释如下:
-
首先 @Configuration 注解表明这是一个配置类。
-
@EnableConfigurationProperties 注解是使我们之前配置的 @ConfigurationProperties 生效,让配置的属性成功的进入 Bean 中。
-
@ConditionalOnClass 表示当项目当前 classpath 下存在 HelloService 时,后面的配置才生效。

最低0.47元/天 解锁文章
1257

被折叠的 条评论
为什么被折叠?



