栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBootCore这一篇就够了(part-1

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBootCore这一篇就够了(part-1

springboot框架关于bean装配

framework这东西就是定义一套规范,使用者遵循这套规范,将会等到很大好处:加快开发效率、提高代码可维护性...

所以,我们要使用好framework,务必要了解他的规则,和定义这套规则的目的。否则,用起来,总是处于东施效颦的状态

1 装配的目的
  • bean加载的解耦

  • 配置参数的处理

2 实现方式2.1 bean加载的解耦

加载的可选方式

  • 类工厂:属于基本的设计模式,实现范畴,不能支撑框架

  • 配置文件,xml等。spring 开始的做法

  • java的anotation。Annotation-based,spring 2.5.6. Java-based configuration, Spring 3.0

2.2 配置参数的处理
  • 代码写死。太土,框架不考虑

  • 配置文件。配置大量的xml文件。spring开始真的做法

  • 配置文件+代码。到了springboot,就是一个applicaiton.yml加上java annotation就可以搞定了。

2.3 xml vs java annotation
  • 二者最终的结果,是一摸一样的

  • xml,对于人的阅读很友好,语义表达很充分。但是配置起来比较烦人

  • java annotation,需要你花不少时间先去研究他的用法。研究明白后,用起来很简便。

3 详解3.0 IoC Container
  • org.springframework.context.ApplicationContext 是IoC容器。负责bean的初始化,配置和组装(instantiating, configuring, and assembling)

  • 容器怎么知道要负责哪些,具体的配置参数是什么呢: configuration metadata。(The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata)

  • The configuration metadata is represented in

    • XML

    • Java annotations. From Spring 2.5

    • Java-based configuration. From Spring 3.0

  • 典型的XML based ApplicationContext

    • ClassPathXmlApplicationContext

    • FileSystemXmlApplicationContext


        contextConfigLocation
        /WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml
    
    
        
    org.springframework.web.context.ContextLoaderListener
    
     // create and configure beans
    ApplicationContext context = new ClassPathXmlApplicationContext("services.xml",    "daos.xml");    // retrieve configured instance
    PetStoreService service = context.getBean("petStore", PetStoreService.class);    // use configured instance
    List userList = service.getUsernameList();
                -----------------                |                |--------------->|   The Spring   |<----------------------------
Configuration   |    Container   | Your Business Objects(POJOS)
  metadata      |________________|
                        |
                        | produces
            ____________V____________
           |                         |
           | Fully Configured System |
           |  Ready for Use          |
           |_________________________|
3.1 bean的定义
  • Class: instantiating beans

  • Name

  • Scope

  • Constructor arguments : Dependency Injection

  • Properties : Dependency Injection

  • Autowiring mode

  • Lzay initialization mode

  • Initialization method

  • Destruction method

Class的实例化
  • 构造方法

  • 静态工厂方法

public class ClientService {
        private static ClientService clientService = new ClientService();        private ClientService() {}        public static ClientService createInstance() {            return clientService;
} }
  • 实例的工厂类

        
        ...
public class DefaultServiceLocator {
        private static ClientService clientService = new ClientServiceImpl();        private static AccountService accountService = new AccountServiceImpl();        public ClientService createClientServiceInstance() {            return clientService;
}        public AccountService createAccountServiceInstance() {            return accountService;
} }
DI
  • Constructor-based Dependency Injection

reference
        
            
            
        
        
        
by parameter type----------------------


    
    

        
        


by parameter name
----------------

        
        



use p, c namespace
-----------------

        
        
        
        
            
            
            

        
        
  • Setter-based Dependency Injection


        
        
        
        
        

        
Bean Scopes
Scopeexplain
singletondefault
prototypeScopes a single bean definition to any number of object instances.
requestonly valid in the context of a web-aware Spring ApplicationContext.
sessionScopes a single bean definition to the lifecycle of an HTTP Session. only valid in the context of a web-aware Spring ApplicationContext.
applicationScopes a single bean definition to the lifecycle of a ServletContext. only valid in the context of a web-aware Spring ApplicationContext.
websocketScopes a single bean definition to the lifecycle of a WebSocket. only valid in the context of a web-aware Spring ApplicationContext.

==when a singleton bean has a prototype bean property!!!==

Autowiring Mode
ModeExplaination
noDefault) No autowiring. Bean references must be defined by ref elements. Changing the default setting is not recommended for larger deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents the structure of a system.
byNameAutowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master and uses it to set the property.
byTypeLets a property be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens (the property is not set).
constructorAnalogous to byType but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.
3.2 annotation-based container configuration
  • @Componet + @ComponentScan + @Configuration

@Configuration
    @ComponentScan(basePackages = "org.example",
            includeFilters = @Filter(type = FilterType.REGEX, pattern =    ".*Stub.*Repository"),
            excludeFilters = @Filter(Repository.class))    public class AppConfig {
 }

等价与


    
            
            
        



作者:小乖猴
链接:https://www.jianshu.com/p/0f4d6fa1b365


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235599.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号