First of all we need to know the way to get java objects there are four:
1. new statement instantiate an object.
2. Create objects through reflection.
3. () method to create an object by clone
3. Create an object by deserializing way
In the framework of the spring, in order to reduce coupling, XML may be used, loading profile properties, then the object is obtained by reflection, to talk to the following is obtained by loading a proxy object properties profile
first step
Create a profile
accountService=com.itheima.service.impl.AccountServiceImpl
accountDao=com.itheima.dao.impl.AccountDaoImpl
Profile explain:
accountService is key,
com.itheima.service.impl.AccountServiceImpl is value, which is the fully qualified name of the class (why should fully qualified class name, as reflected by the object to be obtained)
The second step
Next is an example of the properties object using the object's methods to load the configuration file is read
Finish reading the configuration file will get the fully qualified class name, with the fully qualified name of the class to create objects easier to handle
package com.itheima.factory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class BeanFactory {
private static Properties props;
static {
InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
//实例化对象
props = new Properties();
try {
//读取配置文件
props.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Object getBean(String beanName){
Object bean = null;
//getProperty方法 官方文档解释
/*
使用此属性列表中指定的键搜索属性。
*/
String beanPath = props.getProperty(beanName);
try {
//获得对象
bean = Class.forName(beanPath).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
//返回对象,谁调用我我返回给谁
return bean;
}
}
third step
Reconstruction in
The above code is still a problem, the problem as shown below
public static void main(String[] args) {
//IAccountService as = new AccountServiceImpl();
for(int i=0;i<5;i++){
IAccountService as = (IAccountService)BeanFactory.getBean("accountService");
as.saveAccount();
System.out.println(as);
}
}
The above code, I let him cycle five times, and each time the results of his output are not the same, as shown in Figure
Not called once, it will generate a new object, this will cause inefficiencies
So we came to the fourth step, take a look at (the concept of the fourth step corresponding to a lead spring container) how to solve this kind of problem
the fourth step
We must first know why every time you create a new object will call getBean method, the reason is that here newInstance ()
The solution was to create finished objects he took with Map save up, so the static code block, not only to get the object, but also an example of the container Map, but also add objects to the container inside.
package com.itheima.factory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class BeanFactory {
private static Properties props;
private static Map<String,Object> beans;
static {
InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
//实例化对象
props = new Properties();
try {
//读取配置文件
props.load(in);
//实例化容器
beans = new HashMap();
//查看API文档发现
//keys()继承自Hashtable<Object,Object>
//使用keys()可以获取配置文件的所有键
Enumeration keys = props.keys();
//遍历枚举
while(keys.hasMoreElements()){
String key = keys.nextElement().toString();
//根据Key获取value
String beanPath = props.getProperty(key);
//反射创建对象
Object value = Class.forName(beanPath).newInstance();
//把key和value存入map集合
beans.put(key,value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//对象已经在初始化时就创建了,所以获取对象就没有那么麻烦了
public static Object getBean(String beanName) {
return beans.get(beanName);
}
}
last step
Now, no matter how many times getBean call, the object is only one, why only one, because the object in the static code block initialization is created, but also added to the container, you get through getBean method to get from the container, not again newInstance () to obtain
After writing the article:
I remember the first time to learn spring, time is 20 days ago
Today once again learn the spring, the water can suddenly feel an article, ha ha.
Passing chiefs, to see my badly written, it is a lot of advice.