美文网首页
【Spring】配置类

【Spring】配置类

作者: 嘻洋洋 | 来源:发表于2019-05-20 17:30 被阅读0次

1.基本概念

(1)概念:从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法。
@Configuration可理解为用spring的时候xml里面的<beans>标签
@Bean可理解为用spring的时候xml里面的<bean>标签
(2)用途
作为spring配置Bean的一种方式之一。
(3)@Bean注解
会实例化、配置并初始化一个新的对象,这个对象会由spring IoC 容器管理。

@Configuration  
public class RestClientConfig {  
  
    @Bean  
    public RestTemplate restTemplate() {  
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate;  
    }  
  
}  
--------------------- 
@Service  
public class UserService {  
  
    @Autowired  
    private RestTemplate restTemplate;  
  
    // ...  
  
} 

相关文章

网友评论

      本文标题:【Spring】配置类

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