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. OO第一次作业总结

    OO第一次学习总结 1.第一次作业:多项式加法 从未接触过java的我,在从输入输出开始学了几天后,按照C语言的思路,写出了一个与面向过程极其接近的程序. 在这个程序中,存在两个类:一个是Comput ...

  2. JAVA_SE基础——37.main方法的详解

    主函数 大家都会写吧. 大家一直都不知道为何这样设计,这样设计有什么好处呢? 白话解释: main函数的修饰符是public: 公共的 为何不用private 等等的修饰符 而规定只用public呢? ...

  3. 浏览器端类EXCEL表格插件 - 智表ZCELL产品V1.0.0.1版本发布

    智表的优势 智表兼容与依赖 ZCELL 基于jQuery V1.11.3版本研发,兼容性依赖于jQuery自身的兼容性. 经过验证,目前IE.火狐.谷歌.360等主流浏览器均可以正常使用. 智表下载 ...

  4. 阿里云CentOS部署小笔记

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

  5. python 中os.path.join 双斜杠的解决办法

    这两天在写东西的时候遇到了这个问题,主要是上传图片之后,无法在页面展示,原因就出在用join 拼接的路径中出现了"\"而造成的. >>> import os &g ...

  6. Spring Boot整合Spring Security

    Spring Boot对于该家族的框架支持良好,但是当中本人作为小白配置还是有一点点的小问题,这里分享一下.这个项目是使用之前发布的Spring Boot会员管理系统重新改装,将之前filter登录验 ...

  7. 深度学习之 GAN 进行 mnist 图片的生成

    深度学习之 GAN 进行 mnist 图片的生成 mport numpy as np import os import codecs import torch from PIL import Imag ...

  8. build.gradle & gradle.properties

    一.build.gradle buildscript { ext { springBootVersion = '1.5.9.RELEASE' } repositories { maven { cred ...

  9. restful架构风格设计准则(三)资源识别和资源设计

    读书笔记,原文链接:http://www.cnblogs.com/loveis715/p/4669091.html,感谢作者! restful风格的设计中,首先要识别系统中的资源,然后用HTTP规范表 ...

  10. Python学习之参数

    参数 # coding=utf-8 # 函数的参数 def power(x): return x * x; print power(5) 修改后 def power_1(x,n=2): #默认参数可以 ...