Spring Bean装配方式
Spring装配机制
- 在xml中进行显示配置
- 在Java中进行显示配置
- 隐式bean发现机制和自动装配
自动化装配bean
- 组件扫描(component scanning),Spring会自动发现应用上下文中的bean
- 自动装配(autowiring),Spring自动满足bean之间的依赖
Speak.java
public interface Speak {
void say();
}
ChineseSpeak.java
@Component("chineseSpeak")
public class ChineseSpeak implements Speak {
public void say() {
System.out.println("用中文说");
}
}
SpeakConfig.java
@Component
@ComponentScan(basePackages = "com.xqh.spring.autowire")
public class SpeakConfig {
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpeakConfig.class)
public class SpeakTest {
@Autowired
private Speak speak;
@Test
public void sayTest(){
speak.say();
}
}
@Component注解告诉Spring这是一个组件类并为之创建bean,bean的id为首字母变小写的类名
@ComponentScan注解启用组件扫描并默认扫描与配置类相同的包
@Autowired注解会在Spring上下文中在自动装配符合的bean
通过java代码装配bean
创建配置类并标记 @Configuration注解
创建方法标记 @Bean注解
Speak.java
public interface Speak {
void say();
}
ChineseSpeak.java
public class ChineseSpeak implements Speak {
public void say(){
System.out.println("通过java代码装配-用中文说");
}
}
SpeakConfig.java
@Configuration
public class SpeakConfig {
@Bean
public Speak chineseSpeak() {
return new ChineseSpeak();
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpeakConfig.class)
public class SpeakTest {
@Autowired
private Speak speak;
@Test
public void sayTest(){
speak.say();
}
}
@Configuration标记的类表名是一个配置类并且包含在Spring上下文中如何创建bean的细节
@Bean注解告诉Spring这个方法将返回一个对象并且注册为Spring上下文中的bean
通过xml装配bean
- 创建Spring Xml配置文件
- 在JavaConfig中导入bean配置文件
Speak.java
public interface Speak {
void say();
}
ChineseSpeak.java
public class ChineseSpeak implements Speak {
public void say() {
System.out.println("通过xml装配bean-用中文说");
}
}
SpeakConfig.java
@Configuration
@ImportResource("classpath:spring-beans.xml")
public class SpeakConfig {
}
spring-beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="chineseSpeak" class="com.xqh.spring.xmlconfig.ChineseSpeak"></bean>
</beans>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpeakConfig.class)
public class SpeakTest {
@Autowired
private Speak speak;
@Test
public void sayTest(){
speak.say();
}
}
Spring Bean装配方式的更多相关文章
- Spring Bean装配详解(五)
装配 Bean 的概述 前面已经介绍了 Spring IoC 的理念和设计,这一篇文章将介绍的是如何将自己开发的 Bean 装配到 Spring IoC 容器中. 大部分场景下,我们都会使用 Appl ...
- Spring Bean装配(下)——注解
@Repository,@Service,@Controller这三个注解是基于component定义的注解 component-scan:组件扫描 base-package:扫描这个下的所有类 &l ...
- Spring Bean装配(上)
Bean:在spring的IOC里面,把配置到IOC容器里面的实体或者是对象都称为Bean Bean配置项 Bean的作用域 Bean的生命周期 Bean的自动装配 Resources&Res ...
- Spring入门篇——第4章 Spring Bean装配(下)
第4章 Spring Bean装配(下) 介绍Bean的注解实现,Autowired注解说明,基于java的容器注解说明,以及Spring对JSR支持的说明 4-1 Spring Bean装配之Bea ...
- Spring Bean装配笔记
Spring Bean装配笔记 Spring中的Bean是一个很重要的概念.Spring作为一个Bean容器,它可以管理对象和对象之间的依赖关系,我们不需要自己建立对象,把这部分工作全部转交给容器完成 ...
- Spring入门篇——第3章 Spring Bean装配(上)
第3章 Spring Bean装配(上) 介绍Bean的作用域.生命周期.Aware接口.自动装配和Resource等内容. 3-1 Spring Bean装配之Bean的配置项及作用域 从上至下依次 ...
- spring Bean装配的几种方式简单介绍
Spring容器负责创建应用程序中的bean同时通过ID来协调这些对象之间的关系.作为开发人员,我们需要告诉Spring要创建哪些bean并且如何将其装配到一起. spring中bean装配有两种方式 ...
- Spring Bean装配学习
解释:所谓装配就是把一个类需要的组件给它设置进去,英文就是wire,wiring:注解Autowire也叫自动装配. 目前Spring提供了三种配置方案: 在XML中进行显式的配置 在Java中进行显 ...
- Spring bean注入方式
版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5619525.html Spring bean提供了3中注入方式:属 ...
随机推荐
- JAVA多线程---高并发程序设计
先行发生原则 程序顺序原则:一个线程内保证语义的串行性 volatile:volatile变量的写,先发生于读,这保证了volatile变量的可见性 锁规则:解锁必然发生在随后的加锁前 传递性:A优先 ...
- 用sqlserver的自定义函数直接获取多级部门全名
好久没写存储过程了,今日正好同事的开发需要,实现显示多级部门的部门全名称. 如 财务部/会计部/会计一部 部门表 人员表 函数 getOrgAllName --OrgID 72 当前的部门ID A ...
- vue实例讲解之vue-router的使用
实例讲解系列之vue-router的使用 先总结一下vue-router使用的基本框架: 1.安装并且引入vue-router 安装:npm install vue-router --save-dev ...
- Java数据结构和算法总结-数组、二分查找
前言:在平时开发中数组几乎是最基本也是最常用的数据类型,相比链表.二叉树等又简单很多,所以在学习数据和算法时用数组来作为一个起点再合适不过了.本篇博文的所有代码已上传 github ,对应工程的 ar ...
- Divisors poj2992
Divisors Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9940 Accepted: 2924 Descript ...
- Bayesian CTR Prediction for Bing
Microsoft published a paper in ICML 2009 named ‘Web-Scale Bayesian Click-Through Rate Prediction for ...
- 五年.net程序员转型Java之路
大学毕业后笔者进入一家外企,做企业CRM系统开发,那时候开发效率最高的高级程序语言,毫无疑问是C#.恰逢公司也在扩张,招聘了不少.net程序员,笔者作为应届生,也乐呵呵的加入到.net程序员行列中. ...
- Jmeter的安装和启动时出现unable to access jarfile apachejmeter.jar error value=1错误处理
Jmeter是纯Java开发的, 能够运行Java程序的系统一般都可以运行Jmeter, 如:Windows. Linux. mac等. 由于是由Java开发,所以自然需要jdk环境. Windows ...
- 将本地代码上传到github
准备工作上传本地代码到github 准备工作 在github上创建自己的Repository. 安装git,centos的git安装教程. 上传本地代码到github git init git add ...
- DataRow和DataRowView的区别
可以将DataView同数据库的视图类比,不过有点不同,数据库的视图可以跨表建立视图,DataView则只能对某一个DataTable建立视图. DataView一般通过DataTable.Defau ...