码迷,mamicode.com
首页 > 编程语言 > 详细

spring学习---2

时间:2016-04-03 11:47:58      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

参考书籍《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

spring学习---2

标签:

原文地址:http://www.cnblogs.com/flyingbee6/p/5348115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!