【SpringIOC】程序的耦合度问题以及工厂模式解决
常见的耦合度问题
在没有使用Spring时,我们的service层需要使用dao对象,视图层需要使用到service对象,一旦其中一个对象缺失都会导致编译错误,而不是运行期异常
项目的结构如下,其中代码层层相关

dao层


service层


service实现类中使用dao的实现类,这是程序间的耦合
视图层

使用了service实现类,程序间的耦合
解决思路:一个创建Bean对象的工厂
Bean:在计算机英语中,有可重用组件的含义
JavaBean:用java语言编写的可重用组件。
javabean > 实体类(javabean不等同于实体类,是一个比实体类更大的范畴)
该工厂:创建我们的service对象和dao对象
第一步:需要一个配置文件来配置我们的service和dao
配置的内容:唯一标识符=全限定类名(key=value)
第二步:通过读取配置文件中配置的内容,反射创建对象(Class.forName(全限定类名))
我的配置文件可以是xml也可以使properties
实现
package com.czy.factory;
import java.io.IOException;
import java.util.Properties;
public class BeanFactory {
//保存配置文件的内容
private static Properties props;
static {
props = new Properties();
try {
props.load(BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties"));
} catch (IOException e) {
throw new ExceptionInInitializerError("初始化Properties失败");
}
}
public static Object getBean(String beanName){
Object bean = null;
try{
bean = Class.forName(props.getProperty(beanName)).newInstance();
}catch (Exception e){
e.printStackTrace();
}
return bean;
}
}
解耦


特点
由于调用了newInstance(),表示了每次都会调用默认构造函数创建对象,这种写法创建的对象是多例的

工厂模式改进
通过map存放对象,实现创建单例对象。
改进1:延迟加载,用到对象时,才创建对象
package com.czy.factory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class BeanFactory {
//保存配置文件的内容
private static Properties props;
private static Map<String,Object> map = new HashMap<String, Object>();
static {
props = new Properties();
try {
props.load(BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties"));
} catch (IOException e) {
throw new ExceptionInInitializerError("初始化Properties失败");
}
}
public static Object getBean(String beanName){
if(map.containsKey(beanName))
return map.get(beanName);
Object bean = null;
try{
bean = Class.forName(props.getProperty(beanName)).newInstance();
map.put(beanName,bean);
}catch (Exception e){
e.printStackTrace();
}
return bean;
}
}
改进2:立即加载,读取配置文件后立即创建对象
package com.czy.factory;
import java.io.IOException;
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> map;
static {
props = new Properties();
try {
props.load(BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties"));
map = new HashMap<String, Object>();
Enumeration keys = props.keys();
while(keys.hasMoreElements()){
String key = keys.nextElement().toString();
String value = (String)props.getProperty(key);
Object bean = Class.forName(value).newInstance();
map.put(key,bean);
}
} catch (Exception e) {
throw new ExceptionInInitializerError("初始化Properties失败");
}
}
/*public static Object getBean(String beanName){
if(map.containsKey(beanName))
return map.get(beanName);
Object bean = null;
try{
bean = Class.forName(props.getProperty(beanName)).newInstance();
map.put(beanName,bean);
}catch (Exception e){
e.printStackTrace();
}
return bean;
}*/
public static Object getBean(String beanName){
return map.get(beanName);
}
}
测试
package com.czy.ui;
import com.czy.factory.BeanFactory;
import com.czy.service.AccountService;
import com.czy.service.impl.AccountServiceImpl;
/**
* 模拟保存账户
*/
public class Client {
public static void main(String[] args) throws Exception{
for(int i = 0; i < 5; ++i){
AccountService service = (AccountService) BeanFactory.getBean("accountService");
// service.saveUser();
System.out.println(service);
}
}
}


浙公网安备 33010602011771号