Spring 静态工厂实例
直接上代码,看注释。
创建实体类:
package com.spring.classs;
public class Test {
private String name;
private String age;
public Test(String name, String age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void hellod(){
System.out.println("hello:"+name+"年龄"+age);
}
@Override
public String toString() {
return "Test [name=" + name + ", age=" + age + "]";
}
}
创建静态的工厂类
package com.spring.beansfactory;
import java.util.HashMap;
import java.util.Map;
import com.spring.classs.Test;
/**
*
* @author 周永发
*静态工厂方法:直接调用某一个类的静态方法就可以返回Bean 的实例
*/
public class StaticCarFactory {
private static Map<String,Test> testMap = new HashMap<String,Test>();
static{
testMap.put("aud1", new Test("test1","32"));
testMap.put("aud2", new Test("test2","15"));
}
//静态工厂方法
public static Test getCar(String name){
return testMap.get(name);
}
}
在Spring的配置文件中配置
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置bean的全类名 -->
<!-- 通过静态方法配置Bean,不是配置静态工厂的实例,而是配置Bean的实例 -->
<!-- class属性:指向静态工厂方法的全类名
factory-method:指向静态工厂方法的名字
constructor-arg:如果工厂方法需要传入参数,则使用Constructor-arg
来配置参数
-->
<bean id="test1" class="com.spring.beansfactory.StaticCarFactory" factory-method="getCar">
<constructor-arg value="aud1"></constructor-arg>
</bean>
</beans>
在Main方法中调用
package com.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.classs.Test;
public class Spring1 {
public static void main(String[] args){
//1.创建Spring的IOC容器对象
//ApplicationContext 代表IOC容器,且必须要在创建BEAN实例之前
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-Spring.xml");
//2.从IOC容器中获取BEAN
Test test =(Test)ctx.getBean("test1");
//3.调用方法
test.hellod();
System.out.println(test);
}
}
核心思想是在静态工厂中的对象不用实例化!
Spring 静态工厂实例的更多相关文章
- 使用Junit测试一个 spring静态工厂实例化bean 的例子,所有代码都没有问题,但是出现java.lang.IllegalArgumentException异常
使用Junit测试一个spring静态工厂实例化bean的例子,所有代码都没有问题,但是出现 java.lang.IllegalArgumentException 异常, 如下图所示: 开始以为是代码 ...
- 4.spring对象的创建(静态工厂 实例工厂 泛型,嵌套类型)
1.原料类 namespace CreateObjects{ public class GenericClass<T> { }} PersonDao 类 包含嵌套类型 name ...
- spring静态工厂方法得到单例bean
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationConte ...
- Spring实例化Bean三种方法:构造器、静态工厂、实例工厂
Spring中Bean相当于java中的类,可以通过xml文件对bean进行配置和管理. 一.Bean的实例化: 构造器实例化.静态工厂实例化.实例工厂方式实例化. 目录: 构造器实例化: xml配置 ...
- [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring(十三):使用工厂方法来配置Bean的两种方式(静态工厂方法&实例工厂方法)
通过调用静态工厂方法创建Bean 1)调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不需要关心创建对象的具体细节. 2)要声明通过静态 ...
- Spring学习--静态工厂方法、实例工厂方法创建 Bean
通过调用静态工厂方法创建 bean: 调用静态工厂方法创建 bean 是将对象创建的过程封装到静态方法中 , 当客户端需要对象时 , 只需要简单地调用静态方法 , 而不需要关心创建对象的细节. 要声明 ...
- java:Spring框架2(bean的作用域,静态工厂和实例工厂,自动装配,动态代理)
1.bean的作用域,静态工厂和实例工厂: bean.xml: <?xml version="1.0" encoding="UTF-8"?> < ...
- 【Spring】Spring中的Bean - 2、Baen的实例化 (构造器、静态工厂、实例工厂)
Bean的实例化 文章目录 Bean的实例化 构造器实例化 静态工厂方式实例化 实例工厂方式实例化 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-S ...
随机推荐
- [2-SAT] poj 3207 Ikki's Story IV - Panda's Trick
题目链接: id=3207">http://poj.org/problem? id=3207 Ikki's Story IV - Panda's Trick Time Limit: 1 ...
- SecureCRT图形界面(通过设置调用Xmanager - Passive程序)
首先,在server进行设置 假设server是图形化界面启动的,xhost +命令能够不用运行 [root@test ~]# xhost + xhost: unable to open displ ...
- hdu 2883 kebab(时间区间压缩 && dinic)
kebab Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 《转》Ceilometer Alarm API 參数具体解释 及 举例说明
Ceilometer Alarm是H版新加入的功能,监控报警是云平台必不可少的部分,Ceilometer已经实现了比較完好的监控体系.报警怎么能缺少呢?用过AWS CloudWatch Alarm的人 ...
- oc20--继承2
// // Phone.h #import <Foundation/Foundation.h> // 被继承的这个类我们称之为父类/ 超类 @interface Phone : NSObj ...
- oc4--方法
// main.m // 第一个OC类-方法2 #import <Foundation/Foundation.h> // 1.编写类的声明 @interface Iphone : NSOb ...
- vim插件系列
http://foocoder.com/blog/mei-ri-vimcha-jian-ping-hua-gun-dong-accelerated-smooth-scroll-dot-vim.html ...
- 框架-Java:Spring MVC
ylbtech-框架-Java:Spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 We ...
- css兼容性的一些问题
1. 文字本身的大小不兼容.同样是font-size:14px的宋体文字,在不同浏览器下占的空间是不一样的,ie下实际占高16px,下留白3px,ff 下实际占高17px,上留白1px,下留白3px, ...
- python 飞机大战 实例
飞机大战 #coding=utf-8 import pygame from pygame.locals import * import time import random class Base(ob ...