Spring Spring Framework overview and XML-based configuration IOC

Spring Spring Framework overview and XML-based configuration IOC

I. Introduction

  1. Spring's two core: the IOC (the DI) and the AOP , the IOC is the inversion control, DI DI
  2. Features: lightweight dependency injection, aspect-oriented programming, the container frame, stop
  3. Advantage:
    1. Decoupling easy: do not rely compile, run it relies
    2. AOP support
    3. Support for declarative transactions
    4. To facilitate the testing program
    5. To facilitate the integration of various frameworks
    6. The difficulty of reducing the use of JavaEE API
    7. Spring is very powerful source

Decoupling:

  • Coupling comprising: between class and method between

  • Solving ideas:
    1. To create a reflection in the creation of an object, rather than new
    2. 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");

Guess you like

Origin www.cnblogs.com/MarkKobs-blog/p/11332397.html