spring基础---->spring自定义初始化(一)

  这里我们简单的实现一下spring中的初始化bean,以大概了解他的流程。受委屈几乎是一个人成长最快的途径,吃下去的是委屈,消化掉后得到的是格局。

spring的自定义初始化

测试的项目结构如下:

spring基础---->spring自定义初始化(一)

一、定义两个简单的Bean对象

  • FirstBean.java,变量需要set方法:
package com.spring1.bean;

public class FirstBean {
private String username;
private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getUserInfo() {
return username + ", " + password;
}
}

FirstBean.java

  • SecondBean.java,变量需要set方法:
package com.spring1.bean;

public class SecondBean {
private String name;
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getUserInfo() {
return name + ", " + age;
}
}

SecondBean.java

二、定义一个管理Bean的工厂类BeanFactory

package com.spring1.factory;

import java.util.HashMap;
import java.util.Map; public class BeanFactory {
Map<String, Object> map = new HashMap<>(); public Object getBean(String key) {
return map.get(key);
} public <T> T getBean(String key, Class<T> tClass) {
return (T) map.get(key);
} public void setBean(String name, Object object) {
map.put(name, object);
}
}

三、定义一个解析xml文件,并把初始化的bean存放到beanFactory的类

package com.spring1.parse;

import com.spring1.SpringUtil;
import com.spring1.factory.BeanFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.List; public class ParseXmlBean {
public static BeanFactory parseXmlBean(String filepath) {
BeanFactory beanFactory = new BeanFactory();
SAXReader reader = new SAXReader();
try {
InputStream stream = ParseXmlBean.class.getClassLoader().getResourceAsStream(filepath);
Document document = reader.read(stream);
Element rootElement = document.getRootElement();
List<Element> entries = rootElement.elements("bean");
for (Element entry : entries) {
String beanId = entry.attributeValue("id");
String beanClass = entry.attributeValue("class");
Class<?> bean = Class.forName(beanClass);
Object beanObject = bean.newInstance();
for (Object subEntry : entry.elements("property")) {
String name = ((Element) subEntry).attributeValue("name");
String value = ((Element) subEntry).attributeValue("value");
String dataType = ((Element) subEntry).attributeValue("dataType");
Method method;
if (dataType == null) {
method = bean.getMethod(getMethodName(name), new Class[]{String.class});
method.invoke(beanObject, new Object[]{value});
} else if (dataType.equals("int")) {
method = bean.getMethod(getMethodName(name), new Class[]{int.class});
method.invoke(beanObject, new Object[]{Integer.valueOf(value)});
}
}
beanFactory.setBean(beanId, beanObject);
}
} catch (Exception e) {
e.printStackTrace();
}
return beanFactory;
} private static String getMethodName(String name) {
return "set" + SpringUtil.firstLetterUpper(name);
}
}

四、一个简单的工具类

package com.spring1;

public class SpringUtil {
public static String firstLetterUpper(String name) {
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
}

五、测试的主体类如下

package com.spring1.main;

import com.spring1.bean.FirstBean;
import com.spring1.bean.SecondBean;
import com.spring1.factory.BeanFactory;
import com.spring1.parse.ParseXmlBean;
import org.junit.Test; public class MainTest {
@Test
public void test_1() {
BeanFactory beanFactory = ParseXmlBean.parseXmlBean("config/bean.xml");
FirstBean firstBean = beanFactory.getBean("firstBean", FirstBean.class);
String userInfo = firstBean.getUserInfo();
System.out.println(userInfo); SecondBean secondBean = (SecondBean) beanFactory.getBean("secondBean");
String userInfo1 = secondBean.getUserInfo();
System.out.println(userInfo1);
}
}

运行的结果如下:

huhx,
liuling,

六、测试的xml文件如下

<beans>
<bean id="firstBean" class="com.spring1.bean.FirstBean">
<property name="username" value="huhx"/>
<property name="password" value="1234"/>
</bean> <bean id="secondBean" class="com.spring1.bean.SecondBean">
<property name="age" value="21" dataType="int"/>
<property name="name" value="liuling"/>
</bean>
</beans>

友情链接

上一篇:Vue.js与angular在数据实现的思考


下一篇:web前端校招笔试题集锦