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

posted @ 2016-04-03 11:17  Miranda2015  阅读(121)  评论(0编辑  收藏  举报