工厂模式-Spring的InitializingBean实现
一、创建产品角色接口:
package org.burning.sport.design.pattern.factorypattern.spring.factory; public interface SignService {
/**
* 获取签名的工具
* @return
*/
EnumSign getSignTool(); /**
* 签名
* @param name 用户姓名
*/
void write(String name); }
二、创建产品角色抽象类:
package org.burning.sport.design.pattern.factorypattern.spring.factory; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; public abstract class AbstractSignService implements SignService { /**
* 把内容写入到文件中
* @param content 内容
*/
public void writeFileContent(String content) {
File file = new File("d:/test.txt");
FileOutputStream fos = null;
try {
if(file.exists()) {
file.delete();
}
fos = new FileOutputStream(file);
fos.write(content.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
三、具体产品角色
package org.burning.sport.design.pattern.factorypattern.spring.factory.impl; import org.burning.sport.design.pattern.factorypattern.spring.factory.AbstractSignService;
import org.burning.sport.design.pattern.factorypattern.spring.factory.EnumSign;
import org.springframework.stereotype.Component; @Component
public class BrushPenSignServiceImpl extends AbstractSignService {
@Override
public EnumSign getSignTool() {
return EnumSign.BRUSH_PEN;
} @Override
public void write(String name) {
String content = "大家好,我是" + name + ",现在在用毛笔签名";
writeFileContent(content);
}
}
package org.burning.sport.design.pattern.factorypattern.spring.factory.impl; import org.burning.sport.design.pattern.factorypattern.spring.factory.AbstractSignService;
import org.burning.sport.design.pattern.factorypattern.spring.factory.EnumSign;
import org.springframework.stereotype.Component; @Component
public class PencilSignServiceImpl extends AbstractSignService {
@Override
public EnumSign getSignTool() {
return EnumSign.PENCIL;
} @Override
public void write(String name) {
String content = "大家好,我是" + name + ",现在在用铅笔签名";
writeFileContent(content);
}
}
package org.burning.sport.design.pattern.factorypattern.spring.factory.impl; import org.burning.sport.design.pattern.factorypattern.spring.factory.AbstractSignService;
import org.burning.sport.design.pattern.factorypattern.spring.factory.EnumSign;
import org.springframework.stereotype.Component; @Component
public class PenSignServiceImpl extends AbstractSignService {
@Override
public EnumSign getSignTool() {
return EnumSign.PEN;
} @Override
public void write(String name) {
String content = "大家好,我是" + name + "现在在用钢笔签名";
writeFileContent(content);
}
}
四、工厂角色
SignServiceFactory实现了Spring的InitializingBean方法,在容器启动时,就会运行afterPropertiesSet()方法,通过applicationContext把产品角色都存储到一个HashMap中去
package org.burning.sport.design.pattern.factorypattern.spring.factory; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; import java.util.HashMap;
import java.util.Map; @Component
public class SignServiceFactory implements ApplicationContextAware, InitializingBean { private ApplicationContext applicationContext; private static Map<EnumSign, SignService> map = new HashMap<>(); public SignService getInstance(EnumSign enumSign) {
return map.get(enumSign);
} @Override
public void afterPropertiesSet() throws Exception {
Map<String, SignService> beansOfType = applicationContext.getBeansOfType(SignService.class);
for(Map.Entry<String, SignService> entry : beansOfType.entrySet()) {
map.put(entry.getValue().getSignTool(), entry.getValue());
}
} @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
五,客户端访问
package org.burning.sport.design.pattern.factorypattern.spring.factory; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class ClientTest {
public static void main(String[] args) {
ApplicationContext apx = new ClassPathXmlApplicationContext("applicationContext.xml");
SignServiceFactory signServiceFactory = apx.getBean("signServiceFactory", SignServiceFactory.class);
SignService signService = signServiceFactory.getInstance(EnumSign.PEN);
signService.write("红孩儿");
}
}
代码:https://gitee.com/play-happy/base-project
org.burning.sport.design.pattern.factorypattern.spring.factory
工厂模式-Spring的InitializingBean实现的更多相关文章
- 简单工厂模式,工厂方法模式,抽象工厂模式,spring的狂想
菜鸟D在项目中遇见一个比较纠结的高耦合,所以就想办法来解耦.情况是这样的:系统通过用户选择treeview控件的节点判断调用不同的处理,这些处理中某些东西又是类似的.同事的建议是采用简单工厂,耦合就耦 ...
- 使用工厂模式解耦和IoC思想
使用工厂模式解耦. 一.需求场景: 某一层功能需要改动,但其他层代码不变 实现类1:MyDaoImpl查询自己的数据库. ====改为====> 实现类2:MyDaoImpl2从其它地址得到数据 ...
- Java设计模式之工厂模式的两种实现方式
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 1. 为什么要有工厂模式? "Talk i ...
- 工厂模式模拟Spring的bean加载过程
一.前言 在日常的开发过程,经常使用或碰到的设计模式有代理.工厂.单例.反射模式等等.下面就对工厂模式模拟spring的bean加载过程进行解析,如果对工厂模式不熟悉的,具体可以先去学习一下工厂 ...
- Spring中的工厂模式和单例模式
Spring预备知识(适合中小型项目) 作用:集成和管理其他框架 工厂模式: A a = new A( ); 将类所要创建的对象写入工厂,统一进行管理 package com.spring; pu ...
- 从基础知识到重写Spring的Bean工厂中学习java的工厂模式
1.静态工厂模式其他对象不能直接通过new得到某个类,而是通过调用getInstance()方法得到该类的对象这样,就可以控制类的产生过程.顺带提一下单例模式和多例模式: 单例模式是指控制其他对象获 ...
- Spring 实现两种设计模式:工厂模式和单态模式(单例模式)
本文摘自:李刚 著 <轻量级 Java EE企业应用实战 Struts2+Spring+hibernate整合开发> 在Spring 中大量使用的以下两种设计模式:工厂模式和单态模式. 工 ...
- Spring学习13-中IOC(工厂模式)和AOP(代理模式)的详细解释
我们是在使用Spring框架的过程中,其实就是为了使用IOC,依赖注入,和AOP,面向切面编程,这两个是Spring的灵魂. 主要用到的设计模式有工厂模式和代理模式. IOC是工厂模式参考:设计模式- ...
- spring通过工厂模式解决页面耦合问题
spring通过工厂模式解决页面耦合问题
随机推荐
- jdbcTemplate的简单介绍
Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDBC模板类是第一种工作模式. JdbcTempl ...
- c#NPOI读取excel 比interop和Microsoft.Jet.OLEDB.4.0 之类 的好的多
今天下午开始整理excel这块, 微软弄的那些库简直是个坑, 什么com注册之类的净是些报错. 在网上搜资料偶然碰见npoi ,好东西,值得使用 NPOI是指构建在POI 3.x版本之上的一个程序,N ...
- 基于springboot的restful接口的单元测试示例
一.知识点 代码中对应的知识点 1.jsonPath github网址 1)操作符见文档 2)方法见文档 3)例子见文档 2.MockMvc(org.springframework.test.web. ...
- apache提示make_sock?
[root@localhost apache]# /etc/init.d/*_apache restart 停止 *_apache: [失败] 正在启动 *_apache:(98)Address al ...
- Python3实战系列之九(获取印度售后数据项目)
项目现状:已经部署在服务器上并正常运行了. 1.服务器上的部署 2.下载到服务器的文件列表 3.转存在到数据库SQL Server中的数据 项目总结:这次项目采用python来实现,刚开始还是有点担忧 ...
- solr7.7.0搜索引擎使用(三)(添加文件索引)
众所周知,solr与es的最大区别是,solr可以对pdf,txt,doc等文件生成索引 那我们如何添加文件索引呢? 步骤1.添加core,取名暂且为 coreFile 在bin下执行命令 ./sol ...
- 图解HTTP第二章
简单的 HTTP 协议 1>HTTP 协议用于客户端和服务器端之间的通信 HTTP 协议和 TCP/IP 协议族内的其他众多的协议相同,用于客户端和服务器之间的通信.请求访问文本或图像等资源的一 ...
- ABP框架系列之四十:(Notification-System-通知系统)
Introduction Notifications are used to inform users on specific events in the system. ASP.NET Boiler ...
- MySQL--运维内参中的binlog_summary脚本
#!/bin/bash ##===================================================## ## 脚本出自<MySQL运维内参> ##===== ...
- Windows核心编程:第10章 同步设备IO与异步设备IO
Github https://github.com/gongluck/Windows-Core-Program.git //第10章 同步设备IO与异步设备IO.cpp: 定义应用程序的入口点. // ...