spring-IOC容器(三)
一、通过工厂方法配置Bean:
.xml
<!--
class属性:指向静态工厂方法的全类名
factory-method:指向静态工厂方法的名字
constructor-arg:如果工厂方法需要传递参数,则用constructor-arg来配置参数
-->
<!-- 通过静态工厂方法配置bean,不是配置静态工厂方法实例,而是配置bean实例 -->
<bean id="car1" class="com.atguigu.spring.beans.factory.StaticCarFactory"
factory-method="getCar">
<constructor-arg value="ford"></constructor-arg>
</bean> <!-- 配置工厂的实例 -->
<bean id="carFactory" class="com.atguigu.spring.beans.factory.InstanceCarFactory"></bean>
<!-- 通过实例工厂方法来配置bean -->
<bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="audi"></constructor-arg>
</bean>
.java
1、静态工厂方法:
/**
* 静态工厂方法:直接调用某一个类的静态方法就可以返回bean的实例
*/
public class StaticCarFactory {
private static Map<String, Car> cars = new HashMap<String,Car>(); static {
cars.put("audi", new Car("audi",400000.0));
cars.put("ford", new Car("ford",230000.0));
} //静态工厂方法
public static Car getCar(String name) {
return cars.get(name);
}
}
2、实例工厂方法:
/*
* 实例工厂方法:实例工厂的方法,即需要创建工厂本身,再调用工厂的实例方法来返回bean的实例
* */
public class InstanceCarFactory { private Map<String, Car> cars = null; public InstanceCarFactory() {
cars = new HashMap<String,Car>();
cars.put("audi", new Car("audi", 620000.0));
cars.put("ford", new Car("ford", 340000.0));
} //实例工厂方法
public Car getCar(String brand) {
return cars.get(brand);
}
}
二、通过FactoryBean配置Bean:
.xml:
<!--通过FactoryBean来配置Bean的实例
class: 指向FactoryBean的全类名
property: 配置FactoryBean的属性
但实际返回的却是FactoryBean的getObject()方法返回的实例
-->
<bean id="car" class="com.atguigu.spring.beans.factorybean.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean>
.java:
需要实现FactoryBean<T>接口
public calss XXX implements FactoryBean<T>{
}
三、通过注解配置Bean:
@Component:基本注解,标识了一个受Spring管理的组件;
@Respository:标识持久层(数据持久层)组件;
@Service:标识服务层组件(业务逻辑层);
@Controller:标识表现层组件;
<!-- 需要引用 context命名空间
base-package:指定Spring IOC 容器扫描的包
resource-pattern:指定扫描的资源-->
<context:component-scan base-package="com.atguigu.spring.beans.collection.Person"
resource-pattern="repository/*.class">
</context:component-scan>
<!-- context:exclude-filter:子节点指定排除哪些指定表达式的组件
context:include-filter: 子节点指定包含哪些指定表达式的组件,需要和use-default-filters配合使用-->
<context:component-scan base-package="com.atguigu.spring.beans.collection.Person"
use-default-filters="false">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:exclude-filter type="assignable" expression="com.atguigu.spring.beans.collection.Person2"/>
<!-- 使用context:include-filter时,需要把use-default-filters设置为false(默认为true) -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:include-filter type="assignable" expression="com.atguigu.spring.beans.collection.Person2"/>
</context:component-scan>
@Autowired注解自动装配具有兼容类型的单个Bean属性(构造器,普通字段即使非public,一切具有参数的方法都可以应用@Autowired注解)
默认情况下,所有使用@Autowired注解的属性都需要设置,当Spring找不到匹配的Bean装配属性时,会抛异常,若某一属性允许不被设置,可以设置@Autowired注解的required属性为false。
默认情况下,当IOC容器里存在多个类型兼容的Bean时,通过类型的自动装配将无法工作,此时可以在@Qualifier注解里提供Bean的名称。Spring允许对方法的入参标准@Qualifier已指定注入Bean的名称。
@Autowired 注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的Bean进行自动装配
@Autowired 注解也可以应用在集合属性上,此时的Spring读取该集合的类型信息,然后自动装配所有与之兼容的Bean
@Autowired 注解用在java.util.Map上时,若该Map的键值为String,那么Spring将自动装配与之Map值类型的Bean,此时Bean的名称作为键值
Spring还支持@Resource和@Inject注解,这两个注解和@Autowired注解功能类似(@Resource注解要求提供一个Bean名称的属性)
@Resource(name = "sysConfigItemDaoImpl")
@Autowired()
@Qualifier("sysConfigItemDaoImpl")
public void setDao(SysConfigItem sysConfigItemDao) {
this.sysConfigItem = sysConfigItem;
}
四、泛型依赖注入:
BaseService<T>和BaseRepository<T>
spring-IOC容器(三)的更多相关文章
- Spring IoC容器的初始化过程
Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生 ...
- 对Spring IoC容器实现的结构分析
本文的目标:从实现的角度来认识SpringIoC容器. 观察的角度:从外部接口,内部实现,组成部分,执行过程四个方面来认识SpringIoC容器. 本文的风格:首先列出SpringIoC的外部接口及内 ...
- Spring源码分析:Spring IOC容器初始化
概述: Spring 对于Java 开发来说,以及算得上非常基础并且核心的框架了,在有一定开发经验后,阅读源码能更好的提高我们的编码能力并且让我们对其更加理解.俗话说知己知彼,百战不殆.当你对Spri ...
- Spring IOC(三)依赖注入
本系列目录: Spring IOC(一)概览 Spring IOC(二)容器初始化 Spring IOC(三)依赖注入 Spring IOC(四)总结 目录 1.AbstractBeanFactory ...
- Spring IOC容器基本原理
2.2.1 IOC容器的概念IOC容器就是具有依赖注入功能的容器,IOC容器负责实例化.定位.配置应用程序中的对象及建立这些对象间的依赖.应用程序无需直接在代码中new相关的对象,应用程序由IOC容器 ...
- 1.3浅谈Spring(IOC容器的实现)
这一节我们来讨论IOC容器到底做了什么. 还是借用之前的那段代码 ClassPathXmlApplicationContext app = new ClassPathXmlApplicationCon ...
- spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean
5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...
- Spring IOC容器的实现原理
1 概述 1.1 依赖反转模式 在Java中,一个复杂的功能一般都需要由两个或者两个以上的类通过彼此合作来实现业务逻辑的,这使得每个对象都需要与其合作的对象的引用.如果这个获取依赖对象的过程需要自己去 ...
- spring揭密学习笔记(3)-spring ioc容器:Spring的IoC容器之BeanFactory
1. Spring的IoC容器和IoC Service Provider的关系 Spring的IoC容器和IoC Service Provider所提供的服务之间存在一定的交集,二者的关系如图4-1所 ...
- Spring IOC(三)单例 bean 的注册管理
Spring IOC(三)单例 bean 的注册管理 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 在 Spring 中 ...
随机推荐
- day 68 增删改查 语法
1 普通正则 2 分组正则 url(r'/blog/(\d+)/(\d+)',views.blog) blog(request,arq1,arq2) 按照位置传参 3 分组命名 url(r'/ ...
- vue 缩水版 双向绑定
function Observer(obj, key, value){ var dep = new Dep(); if (Object.prototype.toString.call(value) = ...
- python 正则进阶常用方法
表达式 描述 正则表达式示例 符号 literal 匹配文本字符串的字面值literal foo rel1|rel2 匹配正则表达式rel1或rel2 foo|bar . 匹配任何字符(除了\n之外) ...
- jar安装
安装sdk jar 安装到本地 mvn install:install-file -Dfile=F:\workspace\api-cookbook\java\src\main\lib\sdk-1.10 ...
- pyx文件 生成pyd 文件用于 cython调用
转于:https://www.2cto.com/kf/201405/304168.html 1. 初衷 最近学用python,python不愧是为程序员考虑的编程语言,写起来很快很方便,大大节省开发效 ...
- linux 调试技巧
1.添加log printf("\033[1;43m %s,%s,%d\033[0m\n",__FILE__,__FUNCTION__,__LINE__); FILE:文件名 FU ...
- java网络编程ServerSocket类 和Socket类的常用构造方法及其方法
Socket类Socket(InetAddress address, int port) 创建一个流套接字并将其连接到指定 IP 地址的指定端口号.Socket(String host, int po ...
- java连接操作数据库
Connection 类prepareStatement(String sql) 创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库. PreparedState ...
- 【转载】 从ACM会议看中国大陆计算机科学与国外的差距
ps: 这是一篇06年的文章,与今日的国内计算机行业学术圈环境简直是天翻地覆,很不错的history,值得mark下,今日的cs学术发展十号是坏不发表意见,但是history是值得对比,借鉴,思考 ...
- day 020 常用模块02
主要内容: 什么是序列化 pickle shelve json configparser(模块) 一 序列化 我们在存储数据或者网络传输数据的时候,需要对我们的对象进行处理,把对象处理成方便存储和 传 ...