Spring 的三种装配Bean的方式

  • 组件扫描+自动装配(隐式)
  • 通过Java config装配bean(显示)
  • 通过XML装配bean(显示)

一、组件扫描+自动装配(隐式配置)

  • 组件扫描:

   Spring会自动发现应用上下文中所创建的bean

  • 自动装配:

Spring会自动满足bean之间的依赖关系

适用情况:

  简单bean,初始化不需要基本bean之外的其他参数,无参构造函数或者仅需要其他bean,如果需要其他bean作为属性需要用@Autowired注入,需要事先定义好装配策略。

  

关键字:@Component:标识Bean,可被自动扫描发现。

    @Configuration+@ComponentScan(basepackages="xx"): 制定扫描基本包,作为Spring上下文的配置文件。

    @Autowired:自动装配,查找容器中满足条件的Bean,注入。

实例:

CompactDisc.java

package cdplayer;

public interface CompactDisc {
void play();
}

SgtPeppers.java

package cdplayer;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component; @Component
public class SgtPeppers implements CompactDisc { private String title="享受孤独的音乐";
private String article="奏出和谐的篇章"; @Override
public void play() {
System.out.println(title+article);
}
}

CDPlayerConfig.java

package cdplayer;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan
public class CDPlayerConfig {
}

CDPlayerTest.java

package cdplayer;

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; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest {
@Autowired
private CompactDisc cd; @Test
public void test(){
cd.play();
}
}

二、通过Java config装配bean(显示)

适用情况:所有情况基本适用,区别方法一:不需要事先定义好装配策略,因为会在config.java中定义。

关键词:@Configuration + @Bean:创建config.java,并定义bean的获取方法且需要用@Bean注释。

CDPlayerConfig.java

package cdplayer;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
//@ComponentScan
public class CDPlayerConfig {
@Bean
public CompactDisc compactDisc(){
return new SgtPeppers();
}
}

SgtPeppers.java

package cdplayer;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component; //@Component
public class SgtPeppers implements CompactDisc { private String title="享受孤独的音乐";
private String article="奏出和谐的篇章"; @Override
public void play() { System.out.println(title+article);
}
}

测试类不变。

三、通过XML装配bean(显示)

cdplayer.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:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="compactDisc" class="cdplayer.SgtPeppers"
c:_0="Sgt. Pepper's Lonely Hearts Club Band"
c:_1="The Beatles">
</bean> </beans>

CDPlayerTest:

package cdplayer;

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; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "cdplayer.xml")
public class CDPlayerTest {
@Autowired
private CompactDisc cd; @Test
public void test(){
cd.play();
}
}

Spring系列之装配Bean的更多相关文章

  1. Spring 系列教程之 bean 的加载

    Spring 系列教程之 bean 的加载 经过前面的分析,我们终于结束了对 XML 配置文件的解析,接下来将会面临更大的挑战,就是对 bean 加载的探索.bean 加载的功能实现远比 bean 的 ...

  2. Spring总结 1.装配bean

    本随笔内容要点如下: 依赖注入 Spring装配bean的方式 条件化装配 一.依赖注入 我理解的依赖注入是这样的:所谓的依赖,就是对象所依赖的其他对象.Spring提供了一个bean容器,它负责创建 ...

  3. Spring 之自动化装配 bean 尝试

    [Spring之自动化装配bean尝试] 1.添加dependencies如下所示(不是每一个都用得到 <dependencies> <dependency> <grou ...

  4. spring中自动装配bean

    首先用@Component注解类: package soundsystem: import org.springframework.stereotype.Component; @Component p ...

  5. spring的自动装配Bean与自动检测Bean

    spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean. 1.自动装配与自动检测: 自动装配:让spring自动识别如何装配bean的依赖关系,减少对<pr ...

  6. Spring学习笔记—装配Bean

    在Spring中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质. ...

  7. spring实战三装配bean之Bean的作用域以及初始化和销毁Bean

    1.Bean的作用域 所有的spring bean默认都是单例.当容器分配一个Bean时,不论是通过装配还是调用容器的getBean()方法,它总是返回Bean的同一个实例.有时候需要每次请求时都获得 ...

  8. spring实战一:装配bean之注入Bean属性

    内容参考自spring in action一书. 创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. 1. 创建spring配置 spring是一个基于容器的框架.如果没有配置spri ...

  9. Spring框架---IOC装配Bean

    IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...

随机推荐

  1. vue style width a href动态拼接问题 ?

    style width 这个问题 折磨了我一个上午了  好惭愧 因为项目涉及到 进度条 所以必须用行内样式  style 用过vue的都知道 vue中style的用法 大众用法 :style=&quo ...

  2. django三种文件下载方式

    一.概述 在实际的项目中很多时候需要用到下载功能,如导excel.pdf或者文件下载,当然你可以使用web服务自己搭建可以用于下载的资源服务器,如nginx,这里我们主要介绍django中的文件下载. ...

  3. windows安装gcc编译器

    由于vc6.0对c语言编译不是很好,有些语句是正确的,但是编译却不能通过 所以决定在windows中安装gcc编译器来使用! http://www.cnblogs.com/cryinstall/arc ...

  4. 阿里云CentOS部署小笔记

    快毕业了,我用近两周的时间完成了一个nodeJs+Vue-Cli+Mysql的毕业设计,到了部署的时候了. 然而,博主使用Linux的经验有限得很,所以只能自己慢慢地填坑了. 一.准备工作 1)阿里云 ...

  5. selenium在页面中多个fream的定位

    在做页面元素定位的时候,遇到多fream的页面定位比较困难,需要先去切换到元素所在的fream才能成功定位. 1,切换到目标fream: driver.switch_to.frame('freamID ...

  6. 搭建一个web服务下载HDFS的文件

    需求描述 为了能方便快速的获取HDFS中的文件,简单的搭建一个web服务提供下载很方便快速,而且在web服务器端不留临时文件,只做stream中转,效率相当高! 使用的框架是SpringMVC+HDF ...

  7. gogs详细配置

    sudo apt-get update sudo apt-get upgrade sudo adduser git //创建用户  密码 ******* su git//切换到git用户 cd ~   ...

  8. 从零搭建 webpack3 环境 #1 - 安装使用

    目录: (1)什么是webpack (2)webpack核心概念 (3)环境安装 (4)开始使用webpack 1.什么是webpack 官网的一幅图对webpack的解释,从图中可以看出,webpa ...

  9. Modelsim的使用——复杂的仿真

    相对于简单的仿真,复杂的仿真是指由多个文件.甚至调用了IP核.使用tcl脚本进行的仿真.其实仿真步骤跟图形化的差不多,只不过每一步用脚本写好,然后再在软件里面run一下,主要过程就是: 1.准备好各种 ...

  10. 批量导入数据到hive表中:假设我有60张主子表如何批量创建导入数据

    背景:根据业务需要需要把60张主子表批量入库到hive表. 创建测试数据: def createBatchTestFile(): Unit = { to ) { val sWriter = new P ...