Two main frame: Spring (1) Spring outlined, coupling and decoupling procedures

A, Spring Overview

(. 1) Spring concept
Spring hierarchical Java SE / EE applications full-stack (full stack) lightweight open source framework.

Here Insert Picture Description
With IoC (Inverse Of Control: Inversion of Control) and AOP (Aspect Oriented Programming: Aspect Oriented Programming) provides for the core presentation layer SpringMVC and persistence layer Spring JDBC and business layer transaction management and other enterprise applications technology.Here Insert Picture Description

But also the integration of the open source world many well-known third-party frameworks and libraries , has become the largest enterprise applications using Java EE open-source framework. For example SSM: an integrated Spring, SpringMVC, Mybatis of

(2) Spring advantage
1. facilitate decoupling, simplify development:
by IoC container provided dependency relationships between objects may be referred Spring controlled to avoid excessive coupling hardcoded program caused. Users no longer have singleton class attributes file analyzing these and other needs of the underlying code is written, can concentrate on the upper application. Followed by a complicated underlying principles.

2.AOP programming support

3. Support for declarative transactions
can be freed from our monotonous boredom of transaction management code, pass through flexible to manage affairs of declarative manner,
improve development efficiency and quality.

4.方便程序的测试
可以用非容器依赖的编程方式进行几乎所有的测试工作,测试不再是昂贵的操作,而是随手可
做的事情。

5.方便集成各种优秀框架
Spring可以降低各种框架的使用难度,提供了对各种优秀框架(Struts、Hibernate、Hessian、Quartz等)的直接支持。

6.降低 JavaEE API 的使用难度
Spring对 JavaEE API(如 JDBC、JavaMail、远程调用等)进行了薄薄的封装层,使这些 API 的
使用难度大为降低。

7.Java 源码是经典学习范例
Spring的源代码设计精妙、结构清晰、匠心独用,处处体现着大师对Java 设计模式灵活运用以
及对 Java技术的高深造诣。它的源代码无意是 Java 技术的最佳实践的范例。

(3)Spring的体系结构
Here Insert Picture Description

二、程序的耦合和解耦

程序的耦合:程序之间的依赖关系。包括:类之间的依赖、方法间的依赖

解耦:降低程序间的依赖关系。所以实际开发中:应该做到的是、编译期不依赖、运行时才依赖。
我们在开发中,有些依赖关系是必须的,有些依赖关系可以通过优化代码来解除的。

(1)对于类的解耦:

       (1)使用反射来创建而不是需要依赖导包来new
       (2)通过配置文件()来获取参数,避免写死
Class.forName("com.mysql.jdbc.Driver");//此处只是一个字符串

此时的好处是,我们的类中不再依赖具体的驱动类,此时就算删除 mysql 的驱动 jar 包,依然可以编译(运行就不要想了,没有驱动不可能运行成功的)。

同时,也产生了一个新的问题,mysql 驱动的全限定类名字符串是在 java 类中写死的,一旦要改还是要修改源码。

解决这个问题也很简单,使用配置文件配置。Properties props.getProperty();

(2) factory mode decoupling

In the actual development, we can put three layers of objects with a profile configured together, so that the method in a class by reading the configuration file, to create out of these objects coexist together. When the next use directly take over with a fine.

So, this reads the configuration file and create an object of class is acquiring three factories.

1、Bean

These objects are called Bean object.

Bean: reusable components
JavaBean: written in java language reusable components. (Impl, dao, User classes and other entities can be said Bean
JavaBean> entity class

2, Factory, beans storage

It is the creation of our service and dao objects.

The first: the need for a configuration file to configure our service and dao
profile which put: = uniquely identifies the fully qualified class name (key = value), the definition of a Map, to store the object we created, called the container
second : to create an object reflected by the contents of the configuration file
configuration file can be: XML can also be properties

/**
 * @author Mango
 * 一个创建Bean对象的工厂
 */
public class BeanFactory {
    /**
     * 1.定义一个静态的 配置文件对象
     */
    private static Properties props;

    /**
     * 定义一个Map,来存放我们创建的对象,称之为容器
     */
    private static Map<String,Object> beans;

    //静态代码块为Properties赋值
    static {
        try {
            props = new Properties();
            //获取properties文件的流对象
            InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean/properties");
            props.load(in);
            //实例化容器
            beans = new HashMap<String, Object>();
            //取出配置文件中所有的key
            Enumeration keys =props.keys();
            //遍历枚举
            while(keys.hasMoreElements()) {
                //取出每个key
                String key = keys.nextElement().toString();
                //根据key来获取value
                String beanPath = props.getProperty(key);
                //反射来创建对象
                Object value = Class.forName(beanPath).newInstance();
                //将key和value再返回到容器中
                beans.put(key,value);
            }
        } catch (IOException e) {
            throw new ExceptionInInitializerError("初始化properties失败");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

The last profile information in beans, the Web tier and three-tier Service calls, use the factory getBean (beanName) to get the object, and then call the method . And reduce the degree of couplingHere Insert Picture Description

/**
 * @author Mango
 * 账户的业务层接口的实现类
 */
public class AccountServiceImpl implements IAccountService {

    //private IAccountDao accountDao = new AccountDaoImpl();

    private IAccountDao accountDao = (IAccountDao) BeanFactory.getBean("accountDao");

    public void saveAccount() {
        accountDao.saveAccount();
    }
}
/**
 * @author Mango
 * 模拟一个表现层,用于调用业务层的方法
 */
public class Client {

    public static void main(String[] args) {
        //IAccountService service =  new AccountServiceImpl();
        IAccountService service = (IAccountService) BeanFactory.getBean("accountService");
        service.saveAccount();
    }
}
Published 47 original articles · won praise 18 · views 4875

Guess you like

Origin blog.csdn.net/qq_43605085/article/details/97808994