zoukankan      html  css  js  c++  java
  • spring学习---2

    参考书籍《spring in action》

    1.装载bean

    依赖注入的本质是装配(wiring),指的是创建应用对象之间协作关系的行为。

    装配方式:

    1)传统:XML should not be first choice

    核心框架的10个命名空间:

    <aop>
    <beans>
    <context>
    <jee>
    <jms>
    <lang>
    <mvc>
    <oxm>
    <tx>
    <util> constant/list/map/properties/property-path/set
     

    2)explicit configuration in java

    step1:creating a configuration class
    @Configuration
    public class CDPlayerConfig{...}
    
    step2:declaring a simple bean
    @Bean(name="newname")
    public CompactDisc sgtPeppers(){
         return new SgtPeppers();
    }
    
    step3 injecting with javaConfig
    @Bean
    public CDPlayer cdPlayer(){
       return new CDPlayer(sgtPeppers());
    }

    3 ) implicit bean discovery and automatic wiring(只有自己写的源代码可以用)

    ##compoent scanning---在应用上下文中自动发现需要被创建的bean

    step1: naming an component-scanned bean

    @Component("newbeanID")
    
    public subclass implements superinterface{.....}

    step2:setting a base package for component scanning

    @Configuration
    
    @ComponentScan(basePackage={"a","b","c"})
    
    //@ComponentScan(basePackageClasses={xx.class,xxx.class}
    
    public class classconfig{.....}

    ##autowiring---自动实现bean注入

    @Component
    public class CDPlayer implements MediaPlayer{
        private CompactDisc cd;
       
        @Autowired   ( or @Inject)
        public CDPlayer(CompactDisc cd)
        {   
               this.cd =cd;
        }
       
        public void play()
        {
              cd.play();
        }
    }

    4)混合装载

    referencing XML configuration in JavaConfig

    referencing JavaConfig in XML configuration

  • 相关阅读:
    解题:AHOI 2005 航线规划
    解题:SCOI 2008 天平
    解题:SCOI 2014 方伯伯运椰子
    解题:APIO 2008 免费道路
    解题:USACO15JAN Grass Cownoisseur
    669. 换硬币(dp动态规划)
    8. 旋转字符串
    147. 水仙花数
    1131. 排列中的函数
    78. 最长公共前缀
  • 原文地址:https://www.cnblogs.com/flyingbee6/p/5348115.html
Copyright © 2011-2022 走看看