设计模式:23种
常用的由:单例模式,工厂模式,代理模式,模板模式……
工厂模式:相当于是工厂的作用,创建出对象,之后如果需要用到对象,可以直接去工厂拿就行。
当前案例使用工厂模式是因为beans不止一个,所以使用工厂模式。
当前安利的操作是将xml中的数据封装到BeanConfig中,使用工厂模式的概念
1.BeanFactory首先加载xml配置文件 - 静态代码块
2.将每一个bean标签配置内容,封装到BeanConfig中去
2.最后将xml中的内容封装到一个容器中去,便于快速查询,map进行数据存储
<?xml version="1.0" encoding="UTF-8" ?><beans><bean id="userID001" className="Day02_Demo.Demo02.User"><property name = "uid" value = "u001"></property><property name = "userName" value = "jack"></property><property name = "passWord" value = "1234165"></property></bean><bean id = "bookId002" className="Day02_Demo.Demo02.Book"><property name = "bid" value = "u001"></property><property name = "title" value = "java从入门到放弃"></property><property name = "price" value = "88"></property></bean></beans>
package Day02_Demo.Demo02;/*@create 2020--12--23--11:19*/public class Book {private String bid;private String title;private Integer price;public Book() {}public Book(String bid, String title, Integer price) {this.bid = bid;this.title = title;this.price = price;}@Overridepublic String toString() {return "Book{" +"bid='" + bid + '\'' +", title='" + title + '\'' +", price=" + price +'}';}public String getBid() {return bid;}public void setBid(String bid) {this.bid = bid;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public Integer getPrice() {return price;}public void setPrice(Integer price) {this.price = price;}}
package Day02_Demo.Demo02;/*@create 2020--12--23--11:21*/public class User {private String uid;private String userName;private String passWord;@Overridepublic String toString() {return "User{" +"uid='" + uid + '\'' +", userName='" + userName + '\'' +", passWord='" + passWord + '\'' +'}';}public String getUid() {return uid;}public void setUid(String uid) {this.uid = uid;}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 User(String uid, String userName, String passWord) {this.uid = uid;this.userName = userName;this.passWord = passWord;}public User() {}}
package Day02_Demo.Demo02;/*@create 2020--12--23--11:23*/import java.util.Properties;/*** javaBean的配置对象* BeanConfig文件用于将对应的xml的配置项,解析封装到xml对象中* 属性:id,className,properties** 当前实体类的实例化bean.xml文件,相当于是一个属性配置文件的中转站* 后面进行的操作都是通过这个中转站处理*/public class BeanConfig {private String id;private String className;//用于解析当前的这个bean中所有的属性,所以直接创建一个properties对象private Properties props = new Properties();public BeanConfig() {}public BeanConfig(String id, String className, Properties props) {this.id = id;this.className = className;this.props = props;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public Properties getProps() {return props;}public void setProps(Properties props) {this.props = props;}@Overridepublic String toString() {return "BeanConfig{" +"id='" + id + '\'' +", className='" + className + '\'' +", props=" + props +'}';}}
package Day02_Demo.Demo02;/*@create 2020--12--23--14:00*/import org.apache.commons.beanutils.BeanUtils;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;import javax.management.relation.RoleUnresolved;import java.lang.annotation.ElementType;import java.util.HashMap;import java.util.List;import java.util.Map;/*** 设计模式:23种* 常用的由:单例模式,工厂模式,代理模式,模板模式……* <p>* 工厂模式:相当于是工厂的作用,创建出对象,之后如果需要用到对象,可以直接去工厂拿就行。* 当前案例使用工厂模式是因为beans不止一个,所以使用工厂模式。* 当前安利的操作是将xml中的数据封装到BeanConfig中,使用工厂模式的概念* <p>* 1.BeanFactory首先加载xml配置文件 - 静态代码块* 2.将每一个bean标签配置内容,封装到BeanConfig中去* 2.最后将xml中的内容封装到一个容器中去,便于快速查询,map进行数据存储*/public class BeanFactory {//1.提供Map存放beans.xml中的配置文件内容,使用Map,建议使用静态处理 - 好处是只要加载一次private static Map<String, BeanConfig> cache = new HashMap<>();//静态块处理数据添加 - 直接使用静态块一次性加载static{try {//加载xml,获取documentSAXReader saxReader = new SAXReader();Document document = saxReader.read("xml//bean.xml");//获取根元素Element rootElement = document.getRootElement();//获取所有的bean元素,当前子元素不止一个,所以使用listList<Element> allBeanElement = rootElement.elements("bean");//遍历集合for (Element beanElement : allBeanElement) {//属性:id classNameString id = beanElement.attributeValue("id");String className = beanElement.attributeValue("className");//创建beanConfig,并且封装id和classNameBeanConfig beanConfig = new BeanConfig();beanConfig.setId(id);beanConfig.setClassName(className);//获取子标签 - propertyList<Element> allProperty = beanElement.elements("property");for (Element propElement : allProperty) {//获取属性:name valueString name = propElement.attributeValue("name");String value = propElement.attributeValue("value");//将name和value保存到BeanConfigbeanConfig.getProps().setProperty(name, value);}//内层for//将封装好的数据保存到map中cache.put(id, beanConfig);}//外层forSystem.out.println("初始化数据:" + cache);} catch (Exception e) {throw new RuntimeException(e);}}//因为这是一个工厂,得有提供对象的方法 - 外面需要的时候可以通过这个方法来拿对象//通过指定的beanId来获取相对应的BeanConfigpublic static Object getBean(String beanId) {//创建beanId获取相应的BeanConfigBeanConfig beanConfig = cache.get(beanId);//判断这个对象是不是合法if (beanConfig == null) {throw new RuntimeException("获得的对象[" + beanId + "]不存在");}//如果存在 - 通过反射机制进行获取try {String className = beanConfig.getClassName();Class clazz = Class.forName(className);Object obj = clazz.newInstance();//获取实例对象//循环解析 - beanUtils//stringPropertyNames()方法是从Properties的api中提供的,作用是获取到属性名字的字符串类型for (String name : beanConfig.getProps().stringPropertyNames()) {//每遍历到一个name,就返回name对应的valueString value = beanConfig.getProps().getProperty(name);//使用BeanUtils封装数据BeanUtils.setProperty(obj, name, value);}return obj;} catch (Exception e) {throw new RuntimeException(e);}}}
package Day02_Demo.Demo02;/*@create 2020--12--23--14:39*/import org.junit.Test;/*** 测试类*/public class XMLTest {@Testpublic void test() {//通过实例化数据的方法获取User user = (User) BeanFactory.getBean("userID001");System.out.println(user);Book book = (Book) BeanFactory.getBean("bookId002");System.out.println(book);}}
