服务器之家

服务器之家 > 正文

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

时间:2020-07-08 13:56     来源/作者:java教程网

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

java" id="highlighter_168366">
?
1
2
3
4
5
6
7
8
9
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 *
 */
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 *
 */
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
 *
 */
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. 测试:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 *
 */
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类的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
返回顶部