0
点赞
收藏
分享

微信扫一扫

@EnableConfigurationProperties的理解

西特张 2021-09-24 阅读 45
日记本

@EnableConfigurationProperties的作用是开启@ConfigurationProperties。
@ConfigurationProperties的作用是将配置文件转换成类对象,便于修改或者获取值。
所以会在@ConfigurationProperties前面加上@Compent。
@Compent的作用是对spring说,我是一个bean,你现在要来管理我。
然后在JavaConfig中,@Configuration其实就是告诉spring,spring容器要怎么配置(怎么去注册bean,怎么去处理bean之间的关系(装配))。那么久很好理解了,@Bean的意思就是,我要获取这个bean的时候,你spring要按照这种方式去帮我获取到这个bean。
用@Bean注解的方法:会实例化、配置并初始化一个新的对象,这个对象会由spring IoC 容器管理。最后你就可以使用@AutoWired 去的到这个对象啦。

@Configuration和@Bean的配合使用就相当于是配置 xml。
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
交给spring IoC 容器管理。

例子:
@Component
public class Student {

private String name = "lkm";

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

@Configuration
public class WebSocketConfig {
@Bean
public Student student(){
return new Student();
}

}

举报

相关推荐

0 条评论