脚本之家

电脑版
提示:原网页已由神马搜索转码, 内容由www.jb51.net提供.
您的位置:首页软件编程java→ spring xml配置

java实现Spring在XML配置java类的方法

  更新时间:2016年11月28日 10:00:59  投稿:jingxian 
下面小编就为大家带来一篇java实现Spring在XML配置java类的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

1. 创建自己的bean文件:beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<busi-beans>
<beans>
<bean id="SysHelloImpl" type="com.cxm.test.SysHello">
<desc>test</desc>
<impl-class>com.cxm.test.SysHelloImpl</impl-class>
</bean>
</beans>
</busi-beans>

2. 提供解析xml类:XmlUtils

/**
*
*/
package com.cxm.xmlutil;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import com.cxm.beaninfo.BeanInfo;
/**
* @author admin
*
*/
public class XmlUtils
{

public static void parseXmlDef(InputStream in, Map<String,BeanInfo> beanDefMap,
StringBuffer sb) throws Exception
{
SAXBuilder reader = new SAXBuilder(false);
Document doc = null;
try
{
doc = reader.build(in);
Iterator beanIt = XPath.selectNodes(doc, "/busi-beans/beans/bean")
.iterator();
Element e;
BeanInfo beanDef;
while (beanIt.hasNext())
{
beanDef = new BeanInfo();
e = (Element) beanIt.next();
Attribute attrId = e.getAttribute("id");
Attribute attrType = e.getAttribute("type");
Attribute singleType = e.getAttribute("single");
boolean isSingle = true;
if(null != singleType&&"1".equals(singleType.getValue())){
isSingle= false;
}
beanDef.setSingle(isSingle);
beanDef.setBeanId(attrId.getValue());
beanDef.setType(attrType.getValue());
beanDef.setBeanDesc(getText(e, "desc"));
beanDef.setImplClassName(getText(e, "impl-class"));
//处理初始化参数
beanDefMap.put(attrId.getValue(), beanDef);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
* 根据指定的element, xPath获取XML文档内容
*
* @param p_element
* @param p_xPath
* @return
* @throws Exception
*/
public static String getText(Element p_element, String p_xPath)
throws Exception {
String text = null;
Element e=(Element)XPath.selectSingleNode(p_element,p_xPath);
if (e != null) {
text = e.getText();
} else {
}
return text;
}
}

3.定义bean IO

/**
*
*/
package com.cxm.beaninfo;
/**
* @author admin
*
*/
public class BeanInfo
{
private String beanId;

private String type;

private String beanDesc;

public String getBeanDesc()
{
return beanDesc;
}
public void setBeanDesc(String beanDesc)
{
this.beanDesc = beanDesc;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
private String implClassName;

public String getBeanId()
{
return beanId;
}
public void setBeanId(String beanId)
{
this.beanId = beanId;
}

public String getImplClassName()
{
return implClassName;
}
public void setImplClassName(String implClassName)
{
this.implClassName = implClassName;
}
public boolean isSingle()
{
return isSingle;
}
public void setSingle(boolean isSingle)
{
this.isSingle = isSingle;
}
private boolean isSingle = true;

}

4.bean的创建类:BeanUtil

/**
*
*/
package com.cxm.bean;
/**
* @author admin
*
*/
public class BeanUtil
{
private static XmlBeanFactory factory = new XmlBeanFactory();
/**
* 获取定义好的Bean对象
* @param p_beanId
* @return
* @throws Exception
*/
public static Object createBean(String p_beanId)
throws Exception {
return factory.createBean(p_beanId);
}
}

/**
*
*/
package com.cxm.bean;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import com.cxm.beaninfo.BeanInfo;
import com.cxm.exception.NoSuchBeanDefinitionException;
import com.cxm.xmlutil.XmlUtils;
/**
* @author admin
*
*/
public class XmlBeanFactory
{
private static String BEAN_XML = "/beans.xml";

private static Map<String,BeanInfo> beanDefMap = new HashMap<String,BeanInfo>();

private static Map<String,Object> instanceMap = new HashMap<String,Object>();

static {
InputStream in = XmlBeanFactory.class.getResourceAsStream(BEAN_XML);
if(in ==null){
try{
throw new FileNotFoundException();
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
StringBuffer sb = new StringBuffer();
try
{
XmlUtils.parseXmlDef(in, beanDefMap, sb);
}
catch (Exception e)
{
throw new RuntimeException();
}
}

public Object createBean(String beanId) throws Exception{
if(beanId==null || beanId.trim()==""){
throw new Exception("BeanId can not NULL or '' ");
}
BeanInfo beanInfo = beanDefMap.get(beanId);
if(null ==beanInfo ){
throw new NoSuchBeanDefinitionException(" beanid is not define in xml");
}
Object instance;
if(beanInfo.isSingle()){
instance =instanceMap.get(beanId);
if(null != instance){
return instance;
}
}
String implClass = beanInfo.getImplClassName();
Constructor<?> constructor = Class.forName(implClass.trim()).getConstructor();
instance = constructor.newInstance();
if(beanInfo.isSingle()){
instanceMap.put(beanId, instance);
}
return instance;
}
}

5. 测试:

/**
*
*/
package com.cxm.test;
/**
* @author admin
*
*/
public interface SysHello
{
void sysHello();
}

/**
*
*/
package com.cxm.test;
/**
* @author admin
*
*/
public class SysHelloImpl implements SysHello
{
@Override
public void sysHello()
{
System.out.println("hello world!");

}

}

/**
*
*/
package com.cxm.test;
import com.cxm.bean.BeanUtil;
/**
* @author admin
*
*/
public class Test
{

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
SysHello s = (SysHello)BeanUtil.createBean("SysHelloImpl");
s.sysHello();
}

}

以上这篇java实现Spring在XML配置java类的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

    • 下面小编就为大家带来一篇浅谈java中的TreeMap 排序与TreeSet 排序。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
      2016-12-12
    • 这篇文章主要介绍了Mybatis中的 ${ } 和 #{ }的区别使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
      2020-07-07
    • 这篇文章给大家介绍SSM整合详细流程步骤 Spring SpringMVC,Spring整合MyBatis 事务配置,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
      2020-10-10
    • 这篇文章主要介绍了Idea自动生成Entity实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
      2020-09-09
    • 这篇文章主要介绍了IntelliJ IDEA 2021.1 EAP 4 发布:字体粗细可调整,Git commit template 支持,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
      2021-02-02
    • 这篇文章主要介绍了一个处理用户登陆的servlet简单实例,可通过servlet实现处理用户登录的功能,具有一定参考借鉴价值,需要的朋友可以参考下
      2015-01-01
    • 这篇文章主要介绍了MyBatis update标签,使用 Map 传递参数会导致业务可读性的丧失,继而导致后续扩展和维护的困难,所以在实际应用中我们应该果断废弃该方式,需要的朋友可以参考下
      2023-10-10
    • jcommander 是一个只有几十 kb 的 Java 命令行参数解析工具,可以通过注解的方式快速实现命令行参数解析,本文就来和大家介绍一下JCommander是如何解析命令行参数吧
      2023-06-06
    • AWT的事件处理是一种委派式事件处理方式:普通组件(事件源)将整个事件处理委托给特定的对象(事件监听器);当该事件源发生指定的事件时,就通知所委托的事件监听器,由事件监听器来处理这个事件
      2022-04-04
    • 这篇文章主要介绍了Java 反射机制实例详解的相关资料,这里对java中反射机制进行了详细的分析,需要的朋友可以参考下
      2017-09-09

    最新评论