Spring系列之装配Bean
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的更多相关文章
- Spring 系列教程之 bean 的加载
Spring 系列教程之 bean 的加载 经过前面的分析,我们终于结束了对 XML 配置文件的解析,接下来将会面临更大的挑战,就是对 bean 加载的探索.bean 加载的功能实现远比 bean 的 ...
- Spring总结 1.装配bean
本随笔内容要点如下: 依赖注入 Spring装配bean的方式 条件化装配 一.依赖注入 我理解的依赖注入是这样的:所谓的依赖,就是对象所依赖的其他对象.Spring提供了一个bean容器,它负责创建 ...
- Spring 之自动化装配 bean 尝试
[Spring之自动化装配bean尝试] 1.添加dependencies如下所示(不是每一个都用得到 <dependencies> <dependency> <grou ...
- spring中自动装配bean
首先用@Component注解类: package soundsystem: import org.springframework.stereotype.Component; @Component p ...
- spring的自动装配Bean与自动检测Bean
spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean. 1.自动装配与自动检测: 自动装配:让spring自动识别如何装配bean的依赖关系,减少对<pr ...
- Spring学习笔记—装配Bean
在Spring中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质. ...
- spring实战三装配bean之Bean的作用域以及初始化和销毁Bean
1.Bean的作用域 所有的spring bean默认都是单例.当容器分配一个Bean时,不论是通过装配还是调用容器的getBean()方法,它总是返回Bean的同一个实例.有时候需要每次请求时都获得 ...
- spring实战一:装配bean之注入Bean属性
内容参考自spring in action一书. 创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. 1. 创建spring配置 spring是一个基于容器的框架.如果没有配置spri ...
- Spring框架---IOC装配Bean
IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...
随机推荐
- Document Object Model
什么是DOM W3C制定的书写HTML分析器的标准接口规范 全称 Document Object Model 文档对象模型DOM为HTML文档提供的一个API(接口) 可以操作HTML文档 <! ...
- 聊一聊C#的Equals()和GetHashCode()方法
博客创建一年多,还是第一次写博文,有什么不对的地方还请多多指教. 关于这次写的内容可以说是老生长谈,百度一搜一大堆.大神可自行绕路. 最近在看Jeffrey Richter的CLR Via C#,在看 ...
- ArrayList源码学习----JDK1.7
什么是ArrayList? ArrayList是存储一组数据的集合,底层也是基于数组的方式实现,实际上也是对数组元素的增删改查:它的主要特点是: 有序:(基于数组实现) 随机访问速度快:(进行随机访问 ...
- Python内置函数(49)——isinstance
英文文档: isinstance(object, classinfo) Return true if the object argument is an instance of the classin ...
- python RE模块的使用
摘要: re模块包括操作正则表达式的函数,一些工作中都需要用到,现在说明下使用方法. 使用说明: 一,re模块下的函数: 函数 描述 compile(pa ...
- Extensions in UWP Community Toolkit - Overview
概述 UWP Community Toolkit 中有一个 Extensions 的集合,它们可以帮助开发者实现很多基础功能,省去自己造轮子的过程,本篇我们先来看一下 Extensions 的功能都 ...
- 新概念英语(1-131)Don't be so sure
Lesson 131 Don't be so sure! 别那么肯定! Listen to the tape then answer this question. What's the problem ...
- Angular 学习笔记 ( CDK - Overlays )
更新 : 2018-01-30 ng 的 overlap 在关闭的时候对 backdrop 做了一个 style pointer 目的是让 backdrop 不被 2 次点击, 但是呢, css p ...
- Docker学习笔记 - Docker的容器
docker logs [-f] [-t] [--tail] 容器名 -f -t --tail="all" 无参数:返回所有日志 -f 一直跟踪变化并返回 -t 带时间戳返 ...
- Vue框架axios请求(类似于ajax请求)
Vue框架axios get请求(类似于ajax请求) 首先介绍下,这个axios请求最明显的地方,通过这个请求进行提交的时候页面不会刷新 <!DOCTYPE html> <html ...