1.不使用 Annotation
2.前提:约定大于配置, Service配置有规则可循
3.实现方式:
在Spring初始化时生成Service XML 配置文件,加载到初始化过程中
4.代码
1.MyXmlWebApplicationContext.java
/**
*
*/
package service.utils;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.xml.DomUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* @author yangjk
* @since 2009-9-13
*/
public class MyXmlWebApplicationContext extends XmlWebApplicationContext {
private static Log LOG = LogFactory.getLog(MyXmlWebApplicationContext.class);
private Properties getProperties() {
try {
return PropertiesLoaderUtils.loadAllProperties("config.properties");
} catch (IOException e) {
LOG.error("读取config.properties失败.");
throw new RuntimeException(e);
}
}
public byte[] getGenServiceInputStream() {
Properties prop = getProperties();
String path = prop.getProperty("service.path");
String pkg = prop.getProperty("service.package");
try {
File file = ResourceUtils.getFile("classpath:" + path);
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
List list = DomUtils.getChildElementsByTagName(doc.getDocumentElement(), "bean");
Map map = new HashMap();
for (int i = 0; i < list.size(); i++) {
Element ele = (Element) list.get(i);
map.put(ele.getAttribute("id"), ele.getAttribute("class"));
}
Map genMap = this.getPackageService(pkg);
for (Iterator iterator = genMap.entrySet().iterator(); iterator.hasNext();) {
Map.Entry entry = (Map.Entry) iterator.next();
String id = (String)entry.getKey();
String clazz = (String)entry.getValue();
if(map.get(id) == null) {
Element comm = doc.createElement("bean");
comm.setAttribute("id", id);
comm.setAttribute("class", clazz);
doc.getDocumentElement().appendChild(comm);
if (LOG.isDebugEnabled()) {
LOG.info("Service Gen XML :" + id +" -> " + clazz);
}
}
}
DOMSource ds = new DOMSource(doc);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);
transformer.transform(ds, result);
return writer.toString().getBytes();
} catch (Exception e) {
e.printStackTrace();
}
return null ;
}
private static final String RESOURCE_PATTERN = "**/*.class";
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
public Map getPackageService(String pkg) {
Map map = new HashMap();
String[] pkgs = pkg.split(",");
try {
for (int i = 0; i < pkgs.length; i++) {
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
+ ClassUtils.convertClassNameToResourcePath(pkgs[i])
+ RESOURCE_PATTERN;
Resource[] resources;
resources = this.resourcePatternResolver.getResources(pattern);
MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
for (Resource resource : resources) {
if (resource.isReadable()) {
MetadataReader reader = readerFactory.getMetadataReader(resource);
String className = reader.getClassMetadata().getClassName();
String beanId = getBeanId(className);
if (beanId != null) {
map.put(beanId, className);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(
"Failed to scan classpath for unlisted classes", e);
}
return map;
}
//约定
private String getBeanId(String className) {
if(StringUtils.isNotBlank(className) && className.indexOf('$') == -1) {
String simpleName = StringUtils.substringAfterLast(className, ".");
if(simpleName.endsWith("Impl")) {
simpleName = simpleName.substring(0, simpleName.length()-"Impl".length());
}
simpleName = WordUtils.uncapitalize(simpleName);
return simpleName ;
}
return null ;
}
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
Resource resource2 = new ByteArrayResource(this.getGenServiceInputStream());
reader.loadBeanDefinitions(resource2);
}
}
2.config.properties
#spring service xml path
service.path=conf/service.xml
#spring service package
service.package=service.impl
3.service.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" xmlns:aop="http://www.springframework.org/schema/aop"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- add your service here -->
</beans>
4.web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/conf/dataAccessContext-local.xml
/WEB-INF/classes/conf/applicationContext.xml
/WEB-INF/classes/conf/controller.xml
</param-value>
</context-param>
<context-param>
<param-name>contextClass</param-name>
<param-value>service.utils.MyXmlWebApplicationContext</param-value>
</context-param>
5.按照规则可以随意扩展
分享到:
相关推荐
Autofac的ServiceLocator模式应用,零配置
mysql 5.7.x 所需mysqld.service文件
零配置,用于安装PWA功能。 为什么很棒 零配置,用于安装PWA功能。 尽管作为快速安装Service Worker支持的轻量级工具非常有用,但它目前并未提供用于安装特定“功能”的大量功能。 产品特点 从pwa.config.js读取...
a-service )平台以龙头企业为核心的多租户业务关联特性,并结合其面向企业联盟的独立数据库与共享数据库共享表相结合的数据存储特点,分析了产业链协同SaaS平台以龙头企业为核心的多租户业务数据的安全加密配置需求...
产生具有离线支持的Service Worker(通过 ) 自动注入Web App WIP :策略选项 WIP :元注入 WIP :不同尺寸的图标生成 用法 :information_selector: v0.3.x支持Vite 2, v0.3.x支持Vite 1。 npm i vite-plugin-...
本系统属于产品级的零代码平台,无需自动代码生成器,不需要生成Controller,Service,Repository,Entity等业务代码,程序运行就可以使用,真正的0代码,可以覆盖基本的和业务无关的CRUD RESTful API。 官网地址: ...
实现的零配置原型开发。 一个运行时依赖 (@vue/cli-service),该依赖: 可升级; 基于 webpack 构建,并带有合理的默认配置; 可以通过项目内的配置文件进行配置; 可以通过插件进行扩展。 一个丰富的官方插件...
创建微服务创建无需配置的基于P2P的微服务 :warning: 在早期阶段进行项目。 有些事情可能无法正常工作tl; dr 软件包尚未发布。 以下是预期的工作/使用方式。 您仍然可以克隆和npm link它。 npm install -g geut@...
用于零配置插件 这个插件由工作和其他好东西提供支持。 产品特点 :keycap_0: 零配置,用于注册和生成服务人员 :sparkles: 优化的预缓存和运行时缓存 :hundred_points: 最大化灯塔得分 :balloon: 容易理解的例子...
通过 @vue/cli + @vue/cli-service-global 快速开始零配置原型开发。 一个运行时依赖 (@vue/cli-service),该依赖: 可升级; 基于 webpack 构建,并带有合理的默认配置; 可以通过项目内的配置文件进行配置; 可以...
ASP.NET程序的创建、配置和调用 ASP.NET开发环境Visual Studio 2005介绍 常量、变量、流程控制、面向对象 页面对象:Application、Session、Cookie、Request和Response 智能表单的创建:文本、表格、图像、上传等 ...
SAP_PI接口服务开发配置手册(从零带你配置PI接口(REST协议 JSON格式)) SAP S/4 系统通过 PI 与外围系统进行集成连接;SAP S/4 系统与 PI 通过 ABAP Proxy 适配器集成; PI 与外围系统集成通过 SOAP(web ...
基于注解的零配置struts2 1 页面跳转 2 查数据库 3 事务控制到service 4 前台一个list的展现 5 任务调度 可以实现自定义一个任务在XML中 隔多长时间执行一次 把jar包注掉了 6 实现了一个ztree 压缩包里有一个说明...
零、前期准备 数据库ssm_power_edu.sql 0.1 pom.xml 0.2 application.yml 0.3 实体类 0.4 UserMapper接口 0.5 UserMapper.xml配置文件 0.6 service层接口 0.7 service层实现类 0.8 测试类 一、问题描述 二、使用步骤...
PWA模块 Nuxt.js的零配置PWA解决方案 :open_book: 发展历程 克隆此存储库 使用yarn install或npm install安装依赖项 使用npm run dev启动开发服务器 执照 版权所有(c)-Nuxt社区
通过 @vue/cli + @vue/cli-service-global 快速开始零配置原型开发。 一个运行时依赖 (@vue/cli-service),该依赖:一个丰富的官方插件集合,集成了前端生态中最好的工具。 可升级; 基于 webpack 构建,并带有...
演示使用 Spring Boot 和 Apache CXF 以零 XML 配置托管 JAX-WS 服务的能力。 运行服务 在终端中,执行: ./gradlew bootRun 或者,可以通过将demo.spring.service.Application.java作为普通 Java 应用程序运行,...
EZ-S是的分支,它使您能够以零配置在本地运行具有绿色徽标的HTTPS服务器! 没有证书创建,没有隧道,没有麻烦。 只需运行ez-s并访问https://ez-s.io:5000即可看到文件夹带有可爱的绿色徽章 :exploding_head: 。 ...
环境: ...一:使用SecureCRT连接服务器 ... ... ... 连接成功 ...二:配置服务器主机名 vim /etc/sysconfig/network 重启后主机名生效(重启命令 ... 命令:临时关闭防火墙:service iptables stop 永久关闭防火墙:chkcon
ipfs部署 将静态网站上传到IPFS固定服务,并选择更新DNS。 ipfs-deploy的目标是ipfs-deploy将静态网站部署到IPFS的过程。 目录 安装 npm install -g ipfs-deploy 要么 yarn global add ipfs-deploy ...