Spring学习(四)
Spring Ioc容器
1、具有依赖注入功能的容器。负责实例化,定位,配置应用程序中的对象及建立这些对象间的依赖。在Spring中BeanFactory是Ioc容器的实际代表者。BeanFactory接口提供了IoC容器的基本功能。Spring Ioc容器通过读取配置文件中的配置元数据,通过元数据对应用中各个对象进行实例化及装配。
Spring Ioc容器管理的对象:Bean
1、定义
- Bean就是由Spring容器初始化、装配及管理的对象,此外bean与应用程序中的其他对象没有什么区别.
- Bean在容器内部由BeanDefinition对象表示。
2、BeanDefinition
- 全限定类名(FQN):用于定义Bean的实现类;(必须的)
- Bean行为定义:包括作用域(单例、原型创建)、是否惰性初始化及生命周期等;
- Bean创建方式定义:说明是通过构造器还是工厂方法创建Bean;
- Bean之间关系定义:即对其他bean的引用,也就是依赖关系定义,也就是依赖注入。
3、Bean的作用域
- “singleton”(单例,通过单例缓存池实现)
- “prototype”(原型,每次创建全新的bean)
- 另外提供“request”、“session”、“global session”三种web作用域
Spring IoC容器创建Bean 的方式
1、根据Bean定义里的配置元数据使用反射机制来创建Bean。
- 使用构造
- 静态工程方法
- 实例工厂方法
- 使用 FactoryBean 方式
2、使用构造: 创建对象使用无参构造的方式,使用最多的一种方式,注入数据用setter 方法
Fox类
package ecut.ioc.creation; public class Fox { private String id ; private String name ; public Fox() {
super();
System.out.println( "Fox 无参构造" );
} public Fox(String id , String name) {
super();
this.id = id;
this.name = name;
System.out.println( "Fox( String , String ) 构造" );
} @Override
public String toString() {
return "Fox [id=" + id + ", name=" + name + "]";
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
constructor-creation.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-4.3.xsd"> <!-- 调用无参构造 -->
<bean id="fox1" class="ecut.ioc.creation.Fox" >
<property name="id" value="A001" />
<property name="name" value="苏妲己" />
</bean>
<!-- 使用名称调用有参构造 ,建议使用可读性好-->
<bean id="fox2" class="ecut.ioc.creation.Fox" >
<constructor-arg name="id" value="B001" />
<constructor-arg name="name" value="雪山飞狐" />
</bean>
<!-- 使用索引调用有参构造 -->
<bean id="fox3" class="ecut.ioc.creation.Fox" >
<constructor-arg index="0" value="B002" />
<constructor-arg index="1" value="旋涡鸣人" />
</bean>
</beans>
测试类
package ecut.ioc.creation; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanCreationByConstructor { public static void main(String[] args) { String configLocations = "classpath:ecut/**/constructor-creation.xml" ; AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations ); //需要有无参构造,不然会抛出BeanCreationException,因为没有指定默认的构造函数
Fox fox1 = container.getBean( "fox1" , Fox.class );
System.out.println( fox1 ); Fox fox2 = container.getBean( "fox2" , Fox.class );
System.out.println( fox2 ); Fox fox3 = container.getBean( "fox3" , Fox.class );
System.out.println( fox3 ); container.close(); } }
3、静态工程方法:某个类里面有一个方法专门用创建这个类的对象
Sun类
package ecut.ioc.creation; public class Sun {
/*//"饿汉"式实现单例
private static final Sun SUN =new Sun(); private Sun(){
super();
System.out.println( "构造方法执行" );
}
//调用getInstance()方法获取sun类型实例,调用类的静态方法是主动使用会导致 类被初始化
public static Sun getInstance(){
System.out.println( "使用静态方法返回 Sun 类型的实例" );
//这个Sun 类型的实例是单例的
return SUN ;
}*/ //懒汉式,线程安全实现单例
private static Sun SUN ; private Sun(){
super();
System.out.println( "构造方法执行" );
}
//调用getInstance()方法获取sun类型实例,调用类的静态方法是主动使用会导致 类被初始化
public static Sun getInstance(){
System.out.println( "使用静态方法返回 Sun 类型的实例" );
//同步锁是类对应对象
synchronized ( Sun.class ) {
SUN = new Sun();
}
//这个Sun 类型的实例是单例的
return SUN ;
} }
static-factory-method.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-4.3.xsd"> <!-- factory-method指定的是静态方法,通过调用静态方法之前 -->
<bean id="sun"
class="ecut.ioc.creation.Sun"
factory-method="getInstance" /> </beans>
测试类
package ecut.ioc.creation; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanCreationByStaticFactory { public static void main(String[] args) { String configLocations = "classpath:ecut/**/static-factory-method.xml" ; AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations ); System.out.println( "~~~~~~~~~~~~~~~~~" );
//构造方法只执行一次,默认情况下,容器创建就已经创建了这个bean
Sun s = container.getBean( "sun" , Sun.class );
System.out.println( s ); Sun x = container.getBean( "sun" , Sun.class );
System.out.println( x ); container.close(); } }
4、实例工厂方法:要有一个具体的工厂实例,有这个工厂实例去造bean
Car类
package ecut.ioc.creation; public class Car { private String brand ; // 品牌 public Car(){
super();
System.out.println( "Car无惨构造执行" );
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} }
CarFactory类
package ecut.ioc.creation; public class CarFactory { private String brandName ; public CarFactory() {
super();
} public CarFactory(String brandName) {
super();
this.brandName = brandName;
System.out.println( "CarFactory( String ) 构造执行" );
} public Car create(){
System.out.println( "准备创建 Car 的实例" );
Car c = new Car();
c.setBrand( brandName );
return c ;
} }
instance-factory-method.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-4.3.xsd"> <!-- 先 创建 Car 工厂 -->
<bean id="carFactory" class="ecut.ioc.creation.CarFactory" >
<constructor-arg name="brandName" value="BYD" />
</bean> <!-- 再 通过 工厂实例 ( carFactory ) 的 非静态方法来创建 Car -->
<bean id="car" factory-bean="carFactory" factory-method="create" /> </beans>
测试类
package ecut.ioc.creation; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanCreationByInstanceFactory { public static void main(String[] args) { String configLocations = "classpath:ecut/**/instance-factory-method.xml" ; AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations ); System.out.println( "~~~~~~~~~~~~~~~~~" ); Car c = container.getBean( "car" , Car.class );
System.out.println( c.getBrand() ); container.close(); } }
5、使用 FactoryBean 方式:FactoryBean 是一个接口,由FactoryBean类型的对象造另外一个对象
DateFactoryBean
package ecut.ioc.creation; import java.util.Calendar;
import java.util.Date; import org.springframework.beans.factory.FactoryBean;
//期望返回的类型xx,则xxFactoryBean implements FactoryBean<xx>
public class DateFactoryBean implements FactoryBean<Date>{ private int year ;
private int month ;
private int date ;
private int hour ;
private int minute ;
private int second ; private static final Calendar CALENDAR = Calendar.getInstance(); public DateFactoryBean(){
super();
System.out.println( "DateFactoryBean()" );
} @Override
public Date getObject() throws Exception {
System.out.println( "准备获取 Date 类型实例" );
CALENDAR.set( year, month - 1 , date , hour , minute , second );
Date date = CALENDAR.getTime();
return date ;
} @Override
public Class<?> getObjectType() {
return Date.class;
} @Override
public boolean isSingleton() {
return false;
} public int getYear() {
return year;
} public void setYear(int year) {
this.year = year;
} public int getMonth() {
return month;
} public void setMonth(int month) {
this.month = month;
} public int getDate() {
return date;
} public void setDate(int date) {
this.date = date;
} public int getHour() {
return hour;
} public void setHour(int hour) {
this.hour = hour;
} public int getMinute() {
return minute;
} public void setMinute(int minute) {
this.minute = minute;
} public int getSecond() {
return second;
} public void setSecond(int second) {
this.second = second;
} }
约定:期望返回的类型xx,则xxFactoryBean implements FactoryBean<xx>
factory-bean-creation.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-4.3.xsd"> <!-- 以前: 指定 class 是什么类型,就返回什么类型的实例 -->
<bean id="d" class="java.util.Date" /> <!-- 以前: 指定 class 是什么类型,就返回什么类型的实例,且是使用无参构造 创建bean,则必须要有无参构造-->
<bean id="fox1" class="ecut.ioc.creation.Fox" >
<property name="id" value="A001" />
<property name="name" value="苏妲己" />
</bean> <!-- 这里: 指定的 class 是 XxxFactoryBean ,但是返回的却不是 XxxFactoryBean 类型的实例 -->
<!-- 而是返回了 XxxFactoryBean 内部的 getObject 方法所返回的那个实例 -->
<bean id="birthdate" class="ecut.ioc.creation.DateFactoryBean" >
<property name="year" value="1997" />
<property name="month" value="7" />
<property name="date" value="1" />
<property name="hour" value="12" />
<property name="minute" value="30" />
<property name="second" value="18" />
</bean> </beans>
测试类
package ecut.ioc.creation; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanCreationByFactoryBean { public static void main(String[] args) { String configLocations = "classpath:ecut/**/factory-bean-creation.xml" ; AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations ); Date d = container.getBean( "birthdate" , Date.class );
System.out.println( d ); DateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); System.out.println( df.format( d ) ); container.close(); } }
转载请于明显处标明出处
https://www.cnblogs.com/AmyZheng/p/9249550.html
Spring学习(四)的更多相关文章
- spring学习(四) ———— 整合web项目(SSH)
清楚了spring的IOC 和 AOP,最后一篇就来整合SSH框架把,记录下来,以后应该会用的到. --WH 一.web项目中如何使用spring? 当tomcat启动时,就应该加载spring的配置 ...
- Spring学习(四)--面向切面的Spring
一.Spring--面向切面 在软件开发中,散布于应用中多处的功能被称为横切关注点(cross- cutting concern).通常来讲,这些横切关注点从概念上是与应用的业 务逻辑相分离的(但是往 ...
- spring学习四:springMVC
ref:http://www.cnblogs.com/ysocean/tag/SpringMVC%E5%85%A5%E9%97%A8%E7%B3%BB%E5%88%97/ Spring MVC的处理流 ...
- spring学习 四 对象的创建
spring中,有三种创建对象的方式 (1)构造创建 (2)实例工厂构造 (3)静态工厂构造 一 构造器创建 在构造器创建对象时,有无参构造和有参构造 两种 (1)在spring中,默认的是无参构造 ...
- Spring学习(四)-----Spring Bean引用同xml和不同xml bean的例子
在Spring,bean可以“访问”对方通过bean配置文件指定相同或不同的引用. 1. Bean在不同的XML文件 如果是在不同XML文件中的bean,可以用一个“ref”标签,“bean”属性引用 ...
- Spring学习四----------Bean的配置之Bean的配置项及作用域
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的作用域(每个作用域都是在同一个Bean容器中) 1.singleton:单例,指一个Bean容器中只存在一份(默认) 2.prototype ...
- spring学习四:Spring中的后置处理器BeanPostProcessor
BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...
- Spring学习四
1先导入 Asject的jar包 2配置文件加入标签 ,并加入 <aop:aspectj-autoproxy proxy-target-class="true">(如果 ...
- 我的Spring学习记录(四)
虽然Spring管理这我们的Bean很方便,但是,我们需要使用xml配置大量的Bean信息,告诉Spring我们要干嘛,这还是挺烦的,毕竟当我们的Bean随之增多的话,xml的各种配置会让人很头疼. ...
随机推荐
- maven 配置 阿里云仓库
随便记录下,以后方便查询 <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> < ...
- Go_io操作
I/O操作也叫输入输出操作.其中I是指Input,O是指Output,用于读或者写数据的,有些语言中也叫流操作,是指数据通信的通道. Golang 标准库对 IO 的抽象非常精巧,各个组件可以随意组合 ...
- C#系统库的源代码
.NET Core:https://github.com/dotnet/corefx .NET Framework:https://referencesource.microsoft.com
- 每天进步一点点------DE2-70-TV例子说明
module Reset_Delay(iCLK,iRST,oRST_0,oRST_1,oRST_2); input iCLK; input iRST; output reg oRST_0; outpu ...
- 每天进步一点点------入门视频采集与处理(BT656简介)
凡是做模拟信号采集的,很少不涉及BT.656标准的,因为常见的模拟视频信号采集芯片都支持输出BT.656的数字信号,那么,BT.656到底是何种格式呢? 本文将主要介绍 标准的 8bit B ...
- BFSDFS模板
BFS模板: private static void bfs(HashMap<Character, LinkedList<Character>> graph,HashMap&l ...
- 毕向东java基础总结
Java基础知识总结(超级经典) 写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部分用到哪些语句,方法,和对象. 4,代码实现.用具体的java ...
- C++ string类的使用
C++ string的使用 在了解如何使用string类之前,我们先来看看C语言中使用字符串有多麻烦: 调用头文件:cstring 定义一个C字符串: char str1[51]="Hell ...
- 苗条的生成树 Slim Span--洛谷
传送门 钢哥终于没给黑题紫题了(卑微v 稍稍需要多想一点点 ---------------------------------------------------------------------- ...
- github提交代码
下载git for windows,安装 第一步: 第二步: 第三步:不存在repository,点击 create a repository 第四步:切换至History菜单下,并点击publish ...