Spring学习系列(二) 自动化装配Bean
一、Spring装配-自动化装配
@Component和@ComponentScan
通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组件扫描默认是不启动的,需要显式的配置Spring,从而命令Spring去寻找带有(@Component)注解的类,并为其创建bean。
1、定义接口
package com.seven.springTest.service;
public interface HelloWorldApi {
public void sayHello();
}
2、定义接口的实现类
package com.seven.springTest.service.impl;
import org.springframework.stereotype.Component;
import com.seven.springTest.service.HelloWorldApi;
@Component //通过注解指定该类组件类,告知spring要为它创建Bean
public class PersonHelloWorld implements HelloWorldApi {
@Override
public void sayHello() {
System.out.println("Hello World,This Is Person!");
}
}
3、前面说过了,spring并不能自动启用组件扫描,需要进行显式的配置,这里通过java类来进行显式的配置,定义java配置类HelloWorldConfig,在配置类中我们没有显式的声明任何bean,只不过是使用了@CompontentScan注解来启用组件扫描
package com.seven.springTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan // 启用组件扫描
public class HelloWorldConfig {
}
现在所有的工作已经完成,我们来测试下
package com.seven.springTest.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.seven.springTest.HelloWorldConfig;
import com.seven.springTest.service.HelloWorldApi;
public class HelloWorldTest {
public static void main(String[] args) {
//1. 声明Spring上下文,采用java配置类
ApplicationContext ac = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
//2. 声明Spring应用上下文,采用xml配置
//ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
//通过Spring上下文获取Bean,在这里Spring通过自动扫描发现了PersonHelloWorld的实现,并自动创建bean。
HelloWorldApi hwapi = ac.getBean(HelloWorldApi.class);
//通过sayHello()的输入内容可以看到,hwapi为PersonHelloWorld的实例
hwapi.sayHello();
}
}
通过上的代码,在控制台会输出下面的内容 “Hello World,This Is Person!”
Spring容器通过组件扫描发现了PersonHelloWorld类,并为它创建了对应的bean。到此为止,我们通过简单的注解实现自动化装配,在上面的案例中,HelloWorldConfig配置类@ComponentSan如果没有其他配置,只会扫描HelloWorldConfig所在包或者它的子包,如果需要制定扫描的包,可以通过
@ComponentScan("com.seven.springTest")
或者
@ComponentScan(basePackages="com.seven.springTest")
basePackages允许设置多个包,,只需要把basePackages熟悉设置成一个数组即可
@ComponentScan(basePackages={"com.seven.springTest.service","com.seven.springTest.impl"})
除了通过java配置类来设置Spring启用组件扫描,还可能通过xml类显式配置,参考下面xml配置,并在获取Spring应用上下文时通过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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 启用Spring组件扫描-->
<context:component-scan base-package="com.seven.springTest"></context:component-scan>
</beans>
在上面的案例中,我们通过@Component和@ComponentScan来隐式的配置完成了Bean的装配工作,接下来我们深入的探讨下@Component和@ComponentScan
Spring容器在管理Bean的时候,会给每一个Bean有一个ID标识,上面的例子,如果HelloWorldApi的实现类有多个,那么Spring容器该怎么分配Bean呢,如果我们在使用@Component的时候,没有明确的给PersonHelloWorld设置一个ID,Spring容器会默认给bean给定一个ID,一般为类名(第一个字母会变为小写,例如跑personHelloWorld),所以下面的代码也是成立的
//通过bean的ID来获取实例
HelloWorldApi hwapi = (HelloWorldApi) ac.getBean("personHelloWorld");
hwapi.sayHello();
同时我们也可以为bean设置ID,如下:
@Component("person") //为bean设置ID为“person”
public class PersonHelloWorld implements HelloWorldApi {
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("Hello World,This Is Person!");
}
}
这样我们在获取bean的时候就可通过ID来获取,如下:
// 根据设置的bean ID来获取bean
HelloWorldApi hwapi = (HelloWorldApi) ac.getBean("person");
在以上的案例中我们使用了@Component和@ComponentScan实现了组件扫描,目前为止我们都是对单一的对象进行操作,如果程序复杂点,对象之间存在依赖,该如何处理呢?下面我们就来研究下如何为bean添加注解实现自动装配
@AutoWired
在上面的案例中Person对整个世界说了一句Hello,可说话只有旁边的人知道,我们需要让更多的听到我们的“hello world”,我们就需要一些工具,我们使用电视来广播就能让更多的人听到了,首先我们定义一个传播工具接口
package com.seven.springTest.service;
public interface TransmittingTool {
void work(String message);
}
接下来我们来创建我们的TV
package com.seven.springTest.service.impl;
import org.springframework.stereotype.Component;
import com.seven.springTest.service.TransmittingTool;
@Component //设置为需要被扫描到的组件
public class TVTool implements TransmittingTool {
@Override
public void work(String message) {
//传播工具工作,把我们的消息传播出去
System.out.println(message);
}
}
接下来我们需要对我们之前的PersonHelloWorld的sayHello()方法进行一些修改,让它可以通过传播工具来对全世界说Hello
package com.seven.springTest.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.seven.springTest.service.HelloWorldApi;
import com.seven.springTest.service.TransmittingTool;
@Component
public class PersonHelloWorld implements HelloWorldApi {
//定义传播工具
@Autowired //1.直接变量添加注解
private TransmittingTool transmittingTool;
// @Autowired //2. 通过构造函数注入依赖
// public PersonHelloWorld(TransmittingTool transmittingTool) {
// this.transmittingTool = transmittingTool;
// }
// @Autowired //3. 通过属性的Setter方法注入依赖
// public void setTransmittingTool(TransmittingTool transmittingTool) {
// this.transmittingTool = transmittingTool;
// }
// @Autowired //4. 通过其他函数注入依赖
// public void inserttool(TransmittingTool transmittingTool){
// this.transmittingTool=transmittingTool;
// }
@Override
public void sayHello() {
// 通过传播工具来sayHello
transmittingTool.work("Hello World,This Is Person!--form TV");
}
}
首先我们定义了一个传播工具,这个工具的具体实现我们不清楚,需要Spring容器去给我注入依赖。
@Autowired直接可以使用在类变量、构造函数、Setter和其他任何方法上,参考代码中1-4的实现
- 直接在变量上添加注解
- 在构造函数上添加注解,在spring容器通过构造器实例化bean的时候,会传入一个提供给transmittingTool的实例,注入依赖;
- 通过Setter方法或者其他函数,Spring在初始化bean以后,会尽量的去满足bean的所有依赖,如果使用第4个种注入,我们在HelloWorldTest中重来没有调用过inserttool()方法,可是sayHello()还是能正常执行,Spring会去根据@Autowired来尽量尝试去注入PersonHelloWorld的依赖。
如果能够配置到1个满足要求的依赖,那么这个被依赖的bean就会被装配进来,如果没有匹配的依赖bean,那么应用上下文创建的时候,Spring会抛出一个异常,为了避免异常,我们可以把@Autowired的required设置为false;
@Autowired(required=false) //2. 通过构造函数注入依赖
public PersonHelloWorld(TransmittingTool transmittingTool) {
this.transmittingTool = transmittingTool;
}
@Autowired的required设置给false后,Spring为尝试给bean自动装配,注入依赖,如果没有匹配的bean的话,Spring将会让这个bean处于未装配的状态,当时把required设置为false的时需要注意,因为这个依赖bean处于未装配状态,在调用依赖的时候,如果你的代码做null的检查,这个处于未装配状态的属性有可能会发生异常。
如果有多个bean能满足依赖关系的话,Spring也会抛出异常,表明没有明确指出选择哪个bean进行自动装配。这个在后面我会单独开一篇讲解Spring的高级装配,到时候在详细说明,大家可以关注后续的文章。
Spring学习系列(二) 自动化装配Bean的更多相关文章
- Spring学习笔记(二)之装配Bean
一,介绍Bean的装配机制 在Spring中,容器负责对象的创建并通过DI来协调对象之间的关系.但是我们要告诉Spring创建哪些Bean并且如何将其装配在一起.,装配wiring就是DI依赖注入的本 ...
- Spring学习之旅(三)--装配Bean
装配 Bean 的方式 在 XML 中进行显式配置 在 Java 中进行显式配置 隐式的 Bean 发现机制和自动装配 Spring 提供了以上三种方式进行 Bean 的配置,可以根据自己的需求选择一 ...
- Spring学习笔记(三)之装配Bean
除了组件扫描与自动装配之外还有基于Java代码的装配与基于XML的装配. 有一些场景是我们不能用自动装配的,比如我们要给第三方库中的组件装配到我们的应用中,这时自动装配无效,因为自动装配只能扫描本应用 ...
- Spring学习记录(二)---容器和bean属性配置
下载spring包,在eclipse搭建spring环境. 这步我在eclipse中无法导入包,看网上的: http://sishuok.(和谐)com/forum/blogPost/list/242 ...
- Spring框架系列(二)--装配和注入Bean
企业日常开发中,几乎都是Spring系的框架,无论是SSM.还是现在大火的SpringBoot+JPA/MyBatis,使用最大的目的就是简化开发 基本模块: 核心容器:Beans.Core.Cont ...
- Spring 之自动化装配 bean 尝试
[Spring之自动化装配bean尝试] 1.添加dependencies如下所示(不是每一个都用得到 <dependencies> <dependency> <grou ...
- spring 学习(二):spring bean 管理--配置文件和注解混合使用
spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...
- Spring3系列8- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- MyBatis学习系列二——增删改查
目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...
随机推荐
- 【js技巧】~(按位非)运算符的妙用
今天在reivew部门牙套姐MM的代码的时候,有点小心得,给大家分享.我们常常说到,“学以致用”,但我发现自己却很少能真正做到.<javascript高级程序设计>即大家常说的“红宝书”, ...
- 更新Literacy
之前都没有做单元测试的啊 昨天离职了,闲着没事做了个单元测试,结果发现一堆问题....大部分都是关于值类型在IL中的操作问题... 这件事让我感觉蛋蛋隐隐发疼....以后一定要做单元测试啊.... 具 ...
- OracleConnection is obsolete
用EF搞Oracle的 fake CodeFirst 时,一直报错以下错误: 对类型“System.Data.OracleClient.OracleConnection”的存储区提供程序实例调用“ge ...
- spark参数调优
摘要 1.num-executors 2.executor-memory 3.executor-cores 4.driver-memory 5.spark.default.parallelism 6. ...
- 浅谈JavaScript的New关键字
原型和闭包算是JavaScript中最常见,最难以理解,最容易被当做问题的两个部分,当然还有它们的延伸,如作用域链,继承等等吧,我最近也是各种看,各种翻,记录点自己的心得,写写总会让自己的理解更深一些 ...
- JS面向对象(2) -- this的使用,对象之间的赋值,for...in语句,delete使用,成员方法,json对象的使用,prototype的使用,原型继承与原型链
相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...
- WCF学习之旅—WCF第二个示例(七)
三.创建客户端应用程序 若要创建客户端应用程序,你将另外添加一个项目,添加对该项目的服务引用,配置数据源,并创建一个用户界面以显示服务中的数据. 在第一个步骤中,你将 Windows 窗体项目添加到解 ...
- C#开源日志Nlog入门
c#语言使用的日志比较多,比如:Log4.NLog等,今天我就简单随笔记录哈NLog的使用. 1.NLog的安装: 直接在VS编译器中打开程序包管理器,输入Install-Package NLogin ...
- MVVM框架下,WPF实现Datagrid里的全选和选择
最近的一个项目是用MVVM实现,在实现功能的时候,就会有一些东西,和以前有很大的区别,项目中就用到了常用的序号,就是在Datagrid里的一个字段,用checkbox来实现. 既然是MVVM,就要用到 ...
- 深入理解DOM事件类型系列第一篇——鼠标事件
× 目录 [1]类型 [2]顺序 [3]坐标位置[4]修改键[5]相关元素[6]鼠标按键[7]滚轮事件[8]移动设备 前面的话 鼠标事件是web开发中最常用的一类事件,毕竟鼠标是最主要的定位设备.本文 ...