Spring基础(一)------装配Bean
一、Spring配置的可选方案
三种主要的装配机制:
在xml文件中进行显示配置;
在java中进行显示配置;
隐式的bean发现机制和自动装配。
使用建议:尽可能使用自动配置的机制,显示配置越少越好,若必须要显式配置bean的时候,(例如有些源码并非自己维护,需要为这些代码配置bean的时候),推荐使用类型安全并且比XML更加强大的JavaConfig,
只有当你想要使用遍历的XMl命名空间,并且在JavaConfig中同样没有实现是,才使用XML。
二、自动化装配bean
2.1 Spring从两个角度来实现自动化装配
组件扫描(component scanning):Spring会自动发现应用的上下文中所创建的bean。
自动装配(autowiring):Spring自动满足bean之间的依赖。
CompactDisc接口在Java中定义CD的概念
package soundsystem;
public interface CompactDisc{
void play();
}
带有@Component注解的CompactDisc实现类SgtPeppers
package soundsystem; import org.springframework.stereotype.Component; @Component
public class SgtPeppers implements CompactDisc {
private String title = "Sgt.Pepper's Lonely Hearts Club Band";
private String artist = "The Beatles"; public void play() {
System.out.println("Playing " + title + " by " + artist);
}
}
此处实现类功能暂时不重要,重要的是 @Component注解的作用:表明该类会作为组件类,并告知Spring需要为这个类创建bean,故而不需要显式配置SgtPeppers bean。
@ComponentScan注解启用了组件扫描
package soundsystem; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan
public class CDPlayerConfig {
}
组件扫描默认是不启用的,需要显式的配置Spring,此处通过Java代码定义了Spring的装配规则,在Spring中启用组件扫描。
在没有其他配置下,@ComponentScan只会默认扫描与配置类相同的包。
当然,也可以用XML文件来启用组件扫描,即可以使用Spring context命名空间的<context:component-scan>元素。
通过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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="soundsystem"/>
</beans>
2.2 为组件扫描的bean命名
Spring应用上下文会把所有的bean都会给一个ID,尽管上文SgtPeppers bean并未明确的设置ID,但Spring会默认的设置该bean ID为sgtPeppers,但若你想把期望的ID作为值传递给@Component注解,比如将这个bean标识为lonelyHeartClub,则配置如下:
@Component("lonelyHeartsClub")
public class SgtPeppers implements CompactDisc {
...
}
设置组件扫描的基础包:
如果需要指定包,需要在@ComponentScan的value属性指定包名称:@ComponentScan("soundsystem");
若想表明所设置的是基础包,可以通过basePackages属性:@ComponentScan(basePackages="soundsystem");
basePackages可以设置多个基础包,如@ComponentScan(basePackages="soundsystem","video")
2.3 通过为bean添加注解实现自动装配
@Autowired
public CDPlayer(CompactDisc cd){
this.cd = cd
}
即通过@Autowired注解在构造器之上,当然@Autowired注解不仅能够用在构造器之上,还能用在属性的Setter方法。
三、通过java代码装配bean
3.1 创建配置类
修改CDPlayerConfig的配置:
package soundsystem; import org.springframework.context.annotation.Configuration; @Configuration
public class CDPlayerConfig { }
3.2 声明简单的bean
在JavaConfig中申明bean,需要编写一个方法,这个方法会创建所需类型的实例,然后给这个方法添加@Bean注解,如下方代码:
@Bean
public CompactDisc sgtPeppers(){
return new SgtPeppers();
}
@Bean 注解会告诉Spring这个方法将会返回一个对象,该对象要注册为Spring应用上下文中的bean。
默认情况下,bean的ID与带有@Bean注解的方法名是一样的。
也可以通过name属性指定一个不同的名字:
@Bean(name="lonelyHeartsClubBand")
public CompactDisc sgtPeppers(){
return new SgtPeppers();
}
四、通过XML装配bean
4.1 创建XML配置规范
在XML配置中,意味着要创建一个XML文件,并且要以<beans>元素为根。
最简单的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--configuration details go here-->
</beans>
4.2 声明一个简单的<bean>
声明CompactDisc bean:
<bean class="soundsystem.SgtPeppers"/>
4.3 借助构造器注入初始化bean
<bean id ="cdPlayer" class ="soundsystem.CDPlayer">
<constructor-arg ref = "compactDisc"/>
</bean>
当Spring遇到这个<bean> 元素,它会创建一个CDPlayer实例。<constructor-arg>元素会告知Spring 要将一个ID为compactDisc的bean引用传递到CDPlayer的构造器中。
五、导入和混合配置
5.1 在JavaConfig中引用XML配置
1、如果需要将两个类组合在一起,使用方式就是使用@Import注解导入另一个类,如在CDPlayerConfig类中导入CDConfig:
package soundsystem; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; @Configuration
@Import(CDConfig.class)
public class CDPlayerConfig {
@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc));
}
}
2、或者采用一个更好的办法,也就是不在CDPlayerConfig中使用@Import,而采用一个更高级别的SoundSystemConfig,在这个类中使用@Import将两个配置类组合在一起:
package soundsystem; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; @Configuration
@Import({CDConfig.class,CDPlayerConfig.class})
public class SoundSystemConfig{ }
3、让Spring同时加载XML配置文件以及其他基于Java的配置:
package soundsystem; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource; @Configuration
@Import(CDPlayerConfig.class)
@ImportResource("classpath:cd-config.xml")
public class SoundSystemConfig{ }
5.2 在XML配置中引用JavaConfig
将BlankDisc bean 拆分到自己的配置文件中,该文件名为cd-config.xml,在XML配置文件中使用<import> 元素来引用该文件:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="cd-config.xml"/>
<bean id="cdPlayer"
class="soundsystem.CDPlayer"
c:cd-ref = "compactDisc"/>
</beans>
为了将JavaConfig类导入到XML配置中,可以这样声明bean:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="soundsystem.CDConfig"/>
<bean id="cdPlayer"
class="soundsystem.CDPlayer"
c:cd-ref = "compactDisc"/>
</beans>
参考文档:《Spring实战第4版》
注:本篇学习文章的实例代码以及内容大多数来源于参考文档,仅供本人参考学习,加深理解之用,无任何商业用途,转载请与本人联系,若私自转载用于商业用途,一切后果自负。
Spring基础(一)------装配Bean的更多相关文章
- Spring 之自动化装配 bean 尝试
[Spring之自动化装配bean尝试] 1.添加dependencies如下所示(不是每一个都用得到 <dependencies> <dependency> <grou ...
- Spring总结 1.装配bean
本随笔内容要点如下: 依赖注入 Spring装配bean的方式 条件化装配 一.依赖注入 我理解的依赖注入是这样的:所谓的依赖,就是对象所依赖的其他对象.Spring提供了一个bean容器,它负责创建 ...
- 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框架---IOC装配Bean
IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...
- spring学习总结——装配Bean学习一(自动装配)
一.Spring配置的可选方案 Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系.但是,作为开发人员,你需要告诉Spring要创建哪些bean并且如何将其装配在一起.当描 ...
- Spring实战之装配Bean
1.1Spring配置的可选方案 Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系.但是,作为开发人员,你需要告诉Spring要创建哪些bean并且如何将其装配在一起.当 ...
- Spring学习(二)--装配Bean
一.Spring装配机制 Spring提供了三种主要的装配机制: 1.在XML中进行显示配置 2.在Java中进行显示配置 3.隐式的bean发现机制和自动装配--自动化装配bean Spring可以 ...
- Spring学习笔记—装配Bean
在Spring中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质. ...
随机推荐
- 新拉的项目在idea中启动时报如下错误:org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
今天真的是很苦恼,之前启动项目没有任何问题,今天突然启动时给我报了如下一个错误. 详细报错信息: org.apache.catalina.core.ContainerBase.addChildInte ...
- javaweb中关于转发与重定向的写法
转发: RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/main.jsp"); rd.forward(r ...
- (七)mybatis 的输入映射与输出映射
目录 前言 输入映射 - - 传递 pojo 包装类型 (复杂查询 ) 输出映射 - - resultType 输出映射 - - resultMap 前言 通过 paramterType 指定输入参数 ...
- C标准库常用函数概要
stdio.h printf()/fprintf() printf的返回值是打印的字符数, 发生错误则返回负数 scanf()/fscanf() scanf的返回值是成功赋值的变量个数, 失败则返回E ...
- 超级简单的requests模块教程
在web后台开发过程中,会遇到需要向第三方发送http请求的场景,python中的requests库可以很好的满足这一要求,这里简要记录一下requests模块的使用! 说明: 这里主要记录一下req ...
- not or and 的优先级是不同的
not or and 的优先级是不同的: not > and > or 请用最快速度说出答案: not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 an ...
- Python运算符和编码
Python运算符和编码 一.格式化输出 现在有以下需求,让⽤户输入name, age, job,hobby 然后输出如下所⽰: ----------info of dogfa---------- n ...
- c c++各种类型的取值范围
int类型的变量存储值从-2147483648到2147483647 //例子 #include <iostream> using namespace std; int main(void ...
- VS219 没有.net core 3.0模板
控制台命令 dotnet --info dotnet --version 都正常显示有安装3.0 需要升级VS 2019 16.3.3,本地版本为16.3.2
- MySQL设置自增字段
1.MySQL每张表只能有1个自增字段,这个自增字段即可作为主键,也可用作非主键使用,但是请注意将自增字段当做非主键使用时必须为其添加唯一索引,否则系统将会报错 )将自动增长字段设置为主键 CREAT ...