`
zhaojian0910
  • 浏览: 46360 次
社区版块
存档分类
最新评论

Spring笔记之Bean实例的创建方式以及依赖配置

阅读更多

Bean实例的创建通常有如下3中方式:

1、通过构造方法创建Bean

package com.test;
public class Person {
	public Person() {
		System.out.println("Person 构造方法");
	}

	private Hobby hobby;

	public void setHobby(Hobby hobby) {
		System.out.println("通过依赖关系注入");
		this.hobby = hobby;
	}

	public void singASong() {
		System.out.println(hobby.doIt());
	}
}

interface Hobby {
	public String doIt();
}

class Sing implements Hobby {
	public Sing() {
		System.out.println("Sing 构造方法");
	}

	public String doIt() {
		return "I am singing.";
	}
}

 

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
		Person p = ctx.getBean("person", Person.class);
		p.singASong();
	}
}

 

<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="person" class="com.test.Person">
	<property name="hobby" ref="hobbySing"></property>
</bean>

<bean id="hobbySing" class="com.test.Sing"></bean>
	
</beans>

 2、使用静态工厂方法创建Bean

 

package com.test2;

public class PersonCreator {
	public static People createPerson(String type) {
		if (type.equals("chn")) {
			return new Chinese();
		}
		if (type.equals("jpn")) {
			return new Japanese();
		}

		return null;
	}
}

 

package com.test2;

public interface People {
	public void say();
}

class Chinese implements People {

	public void say() {
		System.out.println("I am Chinese.");
	}

}

class Japanese implements People {

	public void say() {
		System.out.println("I am Japanese.");
	}

}

 工厂方法一定要static

<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<!-- 静态工厂方法 -->
	<bean id="chinese" class="com.test2.PersonCreator" factory-method="createPerson">
		<constructor-arg value="chn" />
	</bean>
	<bean id="japanese" class="com.test2.PersonCreator" factory-method="createPerson">
		<constructor-arg value="jpn" />
	</bean>

</beans>

 

package com.test2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
		People chn = ctx.getBean("chinese", People.class);
		chn.say();
		People jpn = ctx.getBean("japanese", People.class);
		jpn.say();
		
	}
}

 输出:I am Chinese.

I am Japanese.

3、调用实例工厂方法创建Bean

注:实例工厂方法与静态工厂方法只有一点不同

静态工厂方法:只需使用工厂类即可,工厂方法必须static

实例工厂方法:必须使用工厂类的实例,工厂方法必须non-static

package com.test2;

public class PersonCreator {
	public People createPerson(String type) {
		if (type.equals("chn")) {
			return new Chinese();
		}
		if (type.equals("jpn")) {
			return new Japanese();
		}

		return null;
	}
}

 

package com.test2;

public interface People {
	public void say();
}

class Chinese implements People {

	public void say() {
		System.out.println("I am Chinese.");
	}

}

class Japanese implements People {

	public void say() {
		System.out.println("I am Japanese.");
	}

}

 

package com.test2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
		People chn = ctx.getBean("chinese", People.class);
		chn.say();
		People jpn = ctx.getBean("japanese", People.class);
		jpn.say();
		
	}
}

 

<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	<!-- 实例工厂方法 -->
	<bean id="creator" class="com.test2.PersonCreator"/>
	
	<bean id="chinese" factory-bean="creator" factory-method="createPerson">
		<constructor-arg value="chn" />
	</bean>
	
	<bean id="japanese" factory-bean="creator" factory-method="createPerson">
		<constructor-arg value="jpn" />
	</bean>

</beans>

 输出:

I am Chinese.

I am Japanese.

 

总结:大多数情况下,BeanFactory直接通过new关键字调用构造器来创建Bean实例,而class属性指定了Bean实例的实现类。因此,<bean .../>元素必须指定Bean实例的class属性,但这并不是唯一的实例化Bean的方法。

使用实例工厂方法创建Bean实例,以及使用子Bean方法创建Bean实例时,都可以不指定class属性。

 

分享到:
评论

相关推荐

    SSH笔记-静态/实例工厂方法配置bean

    SSH笔记-静态工厂方法配置bean和实例工厂方法配置bean

    Spring的学习笔记

    (一) Annotation注解方式配置事务管理 31 (二) Spring事务选项 35 (三) XML文件形式配置Spring事务管理 37 四、 HibernateTemplate 38 (一) HibernateTemplate 38 (二) HibernateDaoSupport 39 第十一课:Spring整合...

    spring2.5 学习笔记

    (一) Annotation注解方式配置事务管理 31 (二) Spring事务选项 35 (三) XML文件形式配置Spring事务管理 37 四、 HibernateTemplate 38 (一) HibernateTemplate 38 (二) HibernateDaoSupport 39 第十一课:Spring整合...

    Spring笔记(第三次)1

    是Pig还是jamesFactoryBean的实例bean呢,调试跟进getBean源码看看(可看第三节课的视频)发现实际是获取getObject创建的对象,

    spring.xls

    * lazy-init为“default/false”当启动spring容器的时候创建bean 但是如果该bean是prototype时,特殊。这种情况无效 * 在spring容器启动的时候,就会发现错误 * 有可能会造成一些数据长时间驻留在内存中 * lazy...

    Spring框架(详细 一).md

    本篇博文适合零基础的同学:主要包括:spring介绍;...装配bean基于xml---实例化方式; bean种类; bean作用域; 生命周期; 属性注入--setter方法 p命名空间; sqel; 集合注入; 装配bean基于注解;

    java查看sun包源码-learning-spring:Spring框架讲解

    在SpringIOC容器读取bean配置创建bean实例之前,必须对它进行实例化。只有在容器实例化后,才可以从IOC容器里获取bean实例并使用 Spring提供了两种类型的IOC容器实现 BeanFactory:IOC容器的基本实现,在调用getBean...

    【2019版】Spring4.3入门视频课程

    bean生命周期、实例化bean的方式和时机、bean的作用域、继承配置 .自动装配、FactoryBean、两种后处理器 二、Spring AOP AOP简介、实现原理、代理技术 Spring AOP的配置实现 AspectJ表达式 三、Spring注解配置 注解...

    springmybatis

    请注意,这种方式是用SqlSession实例来直接执行已映射的SQL语句: session.selectOne("com.yihaomen.mybatis.models.UserMapper.selectUserByID", 1) 其实还有更简单的方法,而且是更好的方法,使用合理描述参数和...

    spring学习笔记

    Bean:....................................................................................................................................12 1.4.1Spring 框架Bean 实例化的方式:.............................

    swing界面设计之JTree

    创建 Spring app-context.xml bean 定义文件 19 运行应用程序 20 定义 bean 属性 21 创建 to-do 列表:创建一个可重用组件并在表中显示数据 23 创建一个可重用的面板 23 将 bean 组合在一起 24 添加一个表并重用这个...

    day_01.rar

    Spring第一天学习笔记,包含使用IOC管理项目资源、实例化Bean的三种方式,以及bean对象生命周期的代码演示

    java7hashmap源码-java-note:笔记

    实例化](./docs/spring/观察 Spring bean 实例化.md) [spring 中的设计模式](./docs/Spring 中的设计模式.md) Spring MVC Mybatis 数据库 Redis 为什么单线程 内存操作、线程切换消耗、多路复用 应用4 HyperLogLog ...

    thinking-in-spring:学春天

    Bean实例化 常规方法 非常规方法 Bean的初始化 顺序:@PostConstruct&gt; InitializingBean#afterPropertiesSet方法&gt; initMethod Bean的预设初始化 延迟初始化和非延迟对象的差异:应用之上启动前后BeanInitialDemo ...

    Java/JavaEE 学习笔记

    Java/JavaEE 学习笔记 作者在杰普学习时的学习笔记,是J2ee初学者必备手册,是大家学习J2EE开发的很好的参考笔记。 Java/JavaEE 学习笔记 内容目录: Unix 学习笔记..........7 一、Unix前言............7 二、...

    Java学习笔记-个人整理的

    {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{subsection.1.3.1} {1.4}数据类型}{23}{section.1.4} {1.4.1}整数与浮点数}{23}{subsection.1.4.1} {1.4.1.1}浮点数原理}...

    J2EE学习笔记(J2ee初学者必备手册)

    Spring学习笔记...............345 第一章 Spring概述..........................345 第二章 Spring IOC(控制反转)........347 第三章 Spring AOP(面向切面编程)..........351 第四章 Spring中的数据访问.............

    互联网创意产品众筹平台

    问题一箩筐-扫描包,以及spring配置文件标签报错) h5 O# v1 e1 U# N │ 4.问题一箩筐-jdbc.properties属性文件设置错误,无法连接数据库, Q) {; h4 t( ?: r& ^ │ 5.问题一箩筐-关于url扩展名称问题 │ 6.问题一箩筐-...

    跟我学javaweb全套ppt

    《跟我学Java Web》内容包括搭建Web开发环境、HTML相关技术基础知识、JavaScript相关技术基础知识、JSP技术...Spring2.5(容器、装配Java Bean、JDBC和Hibernate模板等)以及Hibernate3技术详解(会话、映射、HQL等)...

    整理后java开发全套达内学习笔记(含练习)

    数值保存方式: 正数= 二进制 负数= 补码 补码= 反码 +1 正数=负数的补码(反码+1) 反码= 非(二进制数) 八进制数,零开头 011(八进制)=9(十进制) 十六进制数,零x开头 0x55(十六进制)=5*16+5(十进制) ...

Global site tag (gtag.js) - Google Analytics