Spring Spring Framework overview and XML-based configuration IOC
I. Introduction
- Spring's two core: the IOC (the DI) and the AOP , the IOC is the inversion control, DI DI
- Features: lightweight dependency injection, aspect-oriented programming, the container frame, stop
- Advantage:
- Decoupling easy: do not rely compile, run it relies
- AOP support
- Support for declarative transactions
- To facilitate the testing program
- To facilitate the integration of various frameworks
- The difficulty of reducing the use of JavaEE API
- Spring is very powerful source
Decoupling:
Coupling comprising: between class and method between
- Solving ideas:
- To create a reflection in the creation of an object, rather than new
- Reads the configuration file to get the object to be created fully qualified class name
- Bean: meaningful reusable components in a computer in English
JavaBean (prepared by the java language reusable components)> entity class
Decoupling factory class
/**
* Bean:可重用组件
*/
public class BeanFactory {
private static Properties props;
//静态代码块
static{
try {
//1.实例化Properties对象
props=new Properties();
//2.获取Properties文件的流对象
InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
props.load(in);
}
catch (Exception e){
throw new ExceptionInInitializerError("初始化properties失败");
}
}
}
/**
* 根据bean的名称获取bean对象
* @param beanName
* @return
*/
public static Object getBean(String beanName){
Object bean = null;
try {
String beanPath = props.getProperty(beanName);
bean = Class.forName(beanPath).newInstance();
}catch (Exception e){
e.printStackTrace();
}
return bean;
}
// IAccountDao accountDao=new AccountDaoImpl();
IAccountDao accountDao = (IAccountDao) BeanFactory.getBean("accountDao");