Spring Boot学习笔记

1.所有Spring IoC容器都需要实现接口BeanFactory,它是顶级容器接口。包含多种getBean方法、判断是否为单例的方法等等。


2.ApplicationContext是BeanFactory子接口之一,大部分Spring IoC容器是ApplicationContext接口的实现类。ApplicationContext接口通过集成HierarchicalBeanFactory,进而继承BeanFactory
ApplicationContext有四个子接口:
消息国际化接口(MessageSource)
环境可配置接口(EnvironmentCapable)
应用事件发布接口(ApplicationEventPublisher)
资源模式解析接口(ResourcePatternResolver)

 

3.

 1 @Configuration
 2 public class Appconfig
 3 {
 4     @Bean(name= "user")
 5     public User initUser()
 6     {
 7         User user = new User();
 8         user.setId(1L);
 9         user.setUserName("user_name_1");
10         user.setNote("note_1");
11         return user;
12     }
13 }
  • @Configuration 代表该类是java配置文件,Spring的容器会根据它来生成IoC容器去装配改注解所在类中定义的Bean
  • @Bean 代表将initUser()方法返回的POJO装配到IoC容器中,属性name定义了这个Bean的名称,如果不设置它,默认将方法名称即initUser作为Bean的名称存到Spring IoC容器中。

 

4.@Component 和 @ComponentScan

 1 @Component 
 2 public class User
 3 {
 4     @Value("1");
 5     private Long id;
 6     
 7     ...
 8     ...
 9     ...
10 }
  • @Component 将普通JavaBean实例化到spring容器中,Spring容器统一管理,不用自己new了

 

1 @Configuration
2 @ComponentScan
3 public class Appconfig
4 {
5     ...
6     ...
7     ...
8 }
  • @ComponentScan 告诉Spring从哪里找到bean 以上的@ComponentScan注解使用后,只会扫描类Appconfig所在的当前包和其下面的子包中的bean

 

posted @ 2019-06-21 09:56  killerqi  阅读(114)  评论(0编辑  收藏  举报