1 自动化装配bean

Spring通过两个方面实现对bean的自动装配
1 ) 组件扫描(component scaning):Spring会自动发现Spring上下文中的bean
2 ) 自动装配(autowriting):Spring自动满足bean之间的依赖

1.1 创建可被发现的bean

  现在我们创建一个接口:
public interface CompactDisc {
void play();
}

接着来一个实现类:

import org.springframework.stereotype.Component;

/**
* Created by cao zhiguo on 2017/9/1.
*/
@Component
public class SgtPeppers implements CompactDisc {
private String title="享受孤独的音乐";
private String article="奏出和谐的篇章";
@Override
public void play() {
System.out.println(title+article);
}

上述的@Component注解的含义是:声明该类是一个bean,此时Spring就有权利去管理这个对象,但是一般的情况下我们需要让Spring容器知道这个类是一个bean,光存在这个注解是不够的,因为Spring容器是发现不了这个注解的,此时可以开启注解的扫描模式,默认的情况下是关闭的。同样的有注解开启的方式和机遇XML的配置方式:比如我们正在CDPlayer这个类中开启这个自动注解的扫描;如下所示:因为我们要在这个类中使用SgtPeppers对象。

package cn.czg.springdemo02;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* Created by cao zhiguo on 2017/9/1.
*/
@Configuration
@ComponentScan
public class CDPlayConfig {
}

上述中@Configuration的注解的作用是配置;@ComponentScan的作用是开启注解扫描,默认是该对象同包下的所有的类或者子包下的所有的类 
下面是基于XML的配置方式:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.czg.springdemo02"/>
</beans>

我们可以做相应的测试

package cn.czg.springdemo02;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static junit.framework.TestCase.assertNotNull; /**
* Created by cao zhiguo on 2017/9/1.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayConfig.class)
public class CDPlayerTest {
@Autowired
private CompactDisc cd; @Test
public void test(){
assertNotNull(cd);
}
}

上述中的测试: 
1) 使用的是SpringJunit4测试 
2) @ContextConfiguration(classes = CDPlayConfig.class)的作用是去类CDPlayerConfig中去加载配置,因为在CDPlayerConfig中包含了注解@ComponenScan注解。 
3) 因此我们最后使用的是断言测试,测试bean中包含CompactDisc这个对象吗。显然是测试通过的,是由Spring自行装配这个bean的。所有添加注解@Component都是bean,而使用ComponentScan注解就可以开启扫描。目前为止我们用到的一些注解及其作用做个总结: 
@Component org.springframework.stereotype.Component; 依赖Context包 
@Configuration org.springframework.context.annotation.Configuration; 依赖Context包 
@ComponentScan org.springframework.context.annotation.ComponentScan; 依赖Context包 
@RunWith(SpringJUnit4ClassRunner.class) org.junit.runner.RunWith; 依赖JUnit包以及Spring test包 
@ContextConfiguration(classes = CDPlayConfig.class) 
org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 依赖test包 
@Autowired org.springframework.beans.factory.annotation.Autowired; 依赖bean包 
@Test org.junit.Test; 依赖Junit测试

1.2 为组件扫描的bean命名

在使用XML配置bean的时候,我们一般用id来作为bean的唯一标识,但是使用注解也可以来实现:

package cn.czg.springdemo02;

import org.springframework.stereotype.Component;

/**
* Created by cao zhiguo on 2017/9/1.
*/
@Component("loneiest")
public class SgtPeppers implements CompactDisc {
private String title="享受孤独的音乐";
private String article="奏出和谐的篇章";
@Override
public void play() {
System.out.println(title+article);
}
}

在@Componenet(“xxx”)来为bean命名,作为唯一的标识,也可以使用Spring的依赖注入来完成.

package cn.czg.springdemo02;

import javax.inject.Named;

/**
* Created by cao zhiguo on 2017/9/1.
*/
/*@Component("loneiest")*/
@Named("loneiest")
public class SgtPeppers implements CompactDisc {
private String title="享受孤独的音乐";
private String article="奏出和谐的篇章";
@Override
public void play() {
System.out.println(title+article);
}
}

1.3 设置组件扫描的基础包

我们面临的问题是在上述的代码中,我们所有的类都在相同的包中进行的测试,因此@ComponentScan是没有任何属性的,但是如果我们要扫描不同的包该怎么实现呢?
比如说我要装配的bean在不同的包中,此时我们就要为这个注解添加属性了。
基础包:就是以配置类所在的包为基础包,比如上述的代码我们要配置CDPlayerConfig类,因此他所在的包就是base-Package.但是如果我们的bean不在这个基础包中就尴尬了。
我们的做法有下面的几种方式:
package cn.czg.springdemo02;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* Created by cao zhiguo on 2017/9/1.
*/
@Configuration
@ComponentScan("cn.czg.springdemo02")
public class CDPlayConfig {
}

还有一种方式:设置基础包。两种方式都是一样的

package cn.czg.springdemo02;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* Created by cao zhiguo on 2017/9/1.
*/
@Configuration
@ComponentScan(basePackages = "cn.czg.springdemo02")
public class CDPlayConfig {
}

大家可以看到,上述用到的是basePackages是复数的形式,因此可以以数组的形式扫描不同的包

package cn.czg.springdemo02;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* Created by cao zhiguo on 2017/9/1.
*/
@Configuration
@ComponentScan(basePackages = {"cn.czg.springdemo02","cn.czg.gosaint"})
public class CDPlayConfig {
}

但是上述的做法还是不安全的,只能的IDE可能会提示你,但是不太智能的IDE应该不会提示你,因为你的基础包使用的是String类型的,因此什么类型都可以写,因此就有如下的做法,直接扫描具体的类。

package cn.czg.springdemo02;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* Created by cao zhiguo on 2017/9/1.
*/
@Configuration
/*@ComponentScan(basePackages = {"cn.czg.springdemo02","cn.czg.gosaint"})*/
@ComponentScan(basePackageClasses ={ CompactDisc.class,SgtPeppers.class})
public class CDPlayConfig {
}

这样的做的更好的有点在于代码因重构,有可能包发生了变化,但是具体的类是没有改变的,我们的扫描依然是正确的。更加安全。

1.4 通过为bean添加注解实现自动装配

@Autowird的注解的使用

package cn.czg.springdemo02;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; /**
* Created by cao zhiguo on 2017/9/2.
*/
@Component
public class CDPlayer implements MediaPlayer {
private CompactDisc sc; @Autowired
public CDPlayer(CompactDisc sc) {
this.sc = sc;
} public void play(){
sc.play();
}
}

在这里CDPlay构造器在构建对象的过程中,同时把sc以参数传递进来,进行注入。这是通过该构造器进行实例化并且自动装配这个bean. 
@Autowired注解不仅可以用在构造器上,还可以用在setter方法或者任何方法之上。也是表明这种注入的关系。让我们下面看一看这两种的用法。

package cn.czg.springdemo02;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; /**
* Created by cao zhiguo on 2017/9/2.
*/
@Component
public class CDPlayer implements MediaPlayer {
private CompactDisc sc; @Autowired
public CDPlayer(CompactDisc sc) {
this.sc = sc;
} /**
* setter方法
* @param sc
*/
@Autowired
public void setSc(CompactDisc sc) {
this.sc = sc;
} /**
* 一般的额普通方法
* @param sc
*/
@Autowired
private void insert(CompactDisc sc){
this.sc=sc;
} public void play(){
sc.play();
}
}

关于下一节我们就来验证自动装配是否成立。

 
 

《Spring实战》系列之Bean的装配-Days01的更多相关文章

  1. Spring MVC系列-(2) Bean的装配

    2. Bean的装配 Spring容器负责创建应用程序中的bean,并通过DI来协调对象之间的关系.Spring提供了三种主要的装配机制: XML显式配置: Java配置类进行显式配置: 隐式的bea ...

  2. Spring MVC系列-(3) Bean的装配

    3. 高级装配Bean 3.1 Bean的作用域 默认情况下,Spring中的bean都是以单例的形式存在的,无论注入多少次,每次注入的都是同一个实例. 考虑到某些bean可能是可变的,Spring定 ...

  3. #Spring实战第二章学习笔记————装配Bean

    Spring实战第二章学习笔记----装配Bean 创建应用对象之间协作关系的行为通常称为装配(wiring).这也是依赖注入(DI)的本质. Spring配置的可选方案 当描述bean如何被装配时, ...

  4. 《Spring实战》系列之Bean的装配-Days02

    2.1 回顾 对于我第一天在bean的装配中写的,是一些基本的语法或者是Spring本身的一些规定,但是我没有对此进行深究.接下来就让我们仔细的讨论一下细节问题.和传统的类的定义和方法的调用做一些比较 ...

  5. spring实战四之Bean的自动装配(注解方式)

    使用注解装配: 从spring2.5开始,Spring启用了使用注解自动装配Bean的属性,使用注解方式自动装配与在XML中使用 autowire 属性自动装配并没有太大区别,但是使用注解方式允许更细 ...

  6. spring实战二之Bean的自动装配(非注解方式)

    Bean的自动装配 自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bea ...

  7. spring实战五之Bean的自动检测

    在spring实战四中,使用在Spring中增加<context:annotation-config>的方式告诉Spring,我们打算使用基于注解的自动装配,希望Spring特殊对待我们所 ...

  8. Spring学习记录(三)---bean自动装配autowire

    Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref autowire: no ---默认情况,不自动装配,通过ref手动引用 byName---根据 ...

  9. Spring学习日志之Bean的装配

    Spring容器负责创建应用程序中的bean并通过依赖注入来协调这些对象之间的关系.但是,作为开发人员,要告诉Spring需要创建哪些bean并且如何将其装配在一起.当描述bean如何装配时,Spri ...

随机推荐

  1. elk示例-精简版

    作者:Danbo 2016-03-09 1.Grok正则捕获 input {stdin{}} filter { grok { match => { "message" =&g ...

  2. 基于WebServices简易网络聊天工具的设计与实现

    基于WebServices简易网络聊天工具的设计与实现 Copyright 朱向洋 Sunsea ALL Right Reserved 一.项目内容 本次课程实现一个类似QQ的网络聊天软件的功能:服务 ...

  3. Data Structure Array: Find if there is a subarray with 0 sum

    http://www.geeksforgeeks.org/find-if-there-is-a-subarray-with-0-sum/ #include <iostream> #incl ...

  4. 0420模块 序列化模块 hashlib模块

    复习:内置方法 __len__ len(obj)的结果依赖于obj.__len__()的结果,计算对象的长度__hash__ hash(obj)的结果依赖于obj.__hash__()的结果,计算对象 ...

  5. java-从这里开始认识

    <java是什么:>Programming language 程序语言Development environment 开发环境Application environment 应用环境Dep ...

  6. RabbitMQ之Exchange Direct模式

    场景: 生产者发送消息到交换机并指定一个路由key, 消费者队列绑定到交换机时要指定路由key(key匹配就能接受消息,key不匹配就不能接受消息) 例如:我们可以把路由key设置为insert ,那 ...

  7. 大话设计模式--策略模式 strategy -- C++实现实例

    1. 策略模式: 它定义了算法家族, 分别封装起来,使他们之间可以相互替换,此模式让算法变化, 不会影响到使用算法的客户. 用相同的方法调用不同的算法,减少各种算法类与使用算法类之间的耦合. 实例中策 ...

  8. QT 按键处理 快捷键处理 shift + ctrl

    原味地址:http://www.cnblogs.com/codingmylife/archive/2010/08/30/1812739.html CTRL+Enter发送信息的实现 在现在的即时聊天程 ...

  9. HTTP-接触

    [HTTP]http是基于TCP/IP协议的应用层协议,他不涉及数据包(packet)传输,主要规定了客户端和服务器之间的通信格式,默认端口是80端口. 不同版本http协议里的相关概念[Conten ...

  10. 一個在WCF學習中的小教訓(本人非科班菜鳥,此經驗無參考價值,衹是自己的經驗記錄)

    1.关于“ServiceHost 仅支持类服务类型”的解决:   Service属性必须执行,不是接口. 改为下图所示: 解决! (注:按朱哥的方法WCF已经可以通信---截至今天的11:11(例子在 ...