关于BeanFactory(模仿Spring的Ioc)
- 创建bean.properties。给出两个service的全类名,根据反射生成实例对象,存入spring容器BeanMap中。
accountService = com.ruixun.service.impl.AccountServiceImpl
accountDao = com.ruixun.dao.impl.AccountDaoImpl
- 代码
public interface AccountDao {
void addAccount();
}
public class AccountDaoImpl implements AccountDao {
public void addAccount() {
System.out.println("AccountDao account ...........");
}
}
public interface AccountService {
void addAccount();
}
public class AccountServiceImpl implements AccountService {
public void addAccount() {
System.out.println("AccountService addAccount.....");
}
}
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class BeanFactory {
// bean.properties
// accountService = com.ruixun.service.impl.AccountServiceImpl
// accountDao = com.ruixun.dao.impl.AccountDaoImpl
private static Properties properties; /*用来解析bean.properties*/
private static Map<String,Object> beans; /*相当于spring容器*/
static {
try {
properties = new Properties();
/*拿到配置文件bean.properties字节流*/
InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
/*加载配置文件*/
properties.load(in);
beans = new HashMap<String, Object>();
Enumeration<Object> keys = properties.keys(); /*包含accountService和accountDao*/
while (keys.hasMoreElements()){
String key = keys.nextElement().toString(); /*bean.properties每一行*/
String beanPath = properties.getProperty(key); /*假如拿到了key:accountService,返回com.ruixun.service.impl.AccountServiceImpl*/
Object value = Class.forName(beanPath).newInstance(); /*com.ruixun.service.impl.AccountServiceImpl 反射实例化*/
beans.put(key,value);/*控制反转成功,存入容器*/
}
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError("初始化properties错误");
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
public static Object getBean(String beanName){
return beans.get(beanName);
}
public static void main(String[] args) {
AccountService accountService = (AccountService)BeanFactory.getBean("accountService");
accountService.addAccount();
AccountDao accountDao = (AccountDao)BeanFactory.getBean("accountDao");
accountDao.addAccount();
}
}
控制反转完成
上面是原理,下面看spring怎么做的
- 创建bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--默认构造函数,没有它,spring创建不了对象-->
<bean id="accountService" class="com.ruixun.service.impl.AccountServiceImpl"/>
<bean id="accountDao" class="com.ruixun.dao.impl.AccountDaoImpl"/>
</beans>
接口和实现类不变
import com.ruixun.dao.AccountDao.AccountDao;
import com.ruixun.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
AccountService accountService = (AccountService) applicationContext.getBean("accountService");
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
accountService.addAccount();
accountDao.addAccount();
}
}
结果