Spring Framework 2: decoupling and coupling between programs

DAY1
Image-20200217205852847
Image-20200217205852847
Image-20200217232030824
Image-20200217232030824
SPRINGMVC framework presentation layer, MyBatis persistence framework is

Spring is a one-stop EE development framework: EE has developed solutions for each layer. With IOC (Inversion of Control) and AOP (Aspect Oriented Programming) for the kernel, while providing the presentation layer Helsinki Spring MVC Spring JDBC persistence layer technology and other enterprise applications, it is the most used open source framework JAVA EE

WEB:SpringMVC

Service: Bean management, Spring declarative management

DAO layer: Jdbc template, ORM module

Spring advantages:

Lightweight, easy decoupling, simplify the development of
Inversion of Control (Ioc)
section (AOP) for
support declarative transactions
to facilitate the testing program
can integrate other frameworks
to reduce the difficulty of using the Java EE API (encapsulates)
the Spring:

docs: the development of norms and API
libs: SPring .jar package and open source
constraint configuration file: Schema
Image-20200217233554182
Image-20200217233554182
Core Container: spring is part of the IOC, all spring applications should be based on the core container

Couple and decouple between programs
using an example to illustrate the coupling procedure

package com.jiading.jdbc;

import java.sql.*;

{class JDBCDemo1 public
/
using JDBC coupled to explain procedures
cited here is an example of dependencies between programs (dependencies between classes, methods dependencies between)
decoupling: reducing dependencies among program
/
public static void main ( String [] args) throws SQLException, ClassNotFoundException {
. // 1 registration drive
// we must rely on other programs at compile time, its dependence is poor
when the // actual development, should do: compile is not dependent , run only rely
// Solutions:
/
use reflection to create objects, avoid using the new keyword
reads the configuration file to get the object to be created fully qualified class name
/
//DriverManager.registerDriver(new com.mysql.jdbc. Driver ());
the Class.forName ( "com.mysql.jdbc.Driver");
// get a connection 2.
connection the DriverManager.getConnection Conn = ( "JDBC: MySQL: // localhost: 3306 / jd_learning", "the root" , "jd5521880");
.. 3 // Get operation of the database objects pretreatment
PSTM = conn.prepareStatement the PreparedStatement ( "SELECT * from Account");
// performed. 4 SQL, to give the results set.
The ResultSet pstm.executeQuery REST = ();
.. 5 // traverse the result set
while (rest.next ()) {
System.out.println (rest.getString ( "name"));
}
// release resources. 6.
rest.close ();
pstm.close ();
conn.Close ();
}
}
using the factory design pattern decouple
Description here Wallpaper Enter
Enter Description here Wallpaper
which is separate from the standard factory design pattern template interface and implementation class, you can refer to:
IAccountDAO:

com.jiading.dao Package;
/
persistence layer interface account
/
public interface IAccountDAO {
/
analog Save Account
/
void saveAccount ();
}
AccountDAOImpl:

package com.jiading.dao.impl;

import com.jiading.dao.IAccountDAO;

{class AccountDAOImpl the implements IAccountDAO public
/
analog storage
/
public void saveAccount () {
System.out.println ( "save the account");
}
}
IAccountService:

package com.jiading.service;

interface IAccountService {public
/
analog Save Account
/
void saveAccount ();
}
AccountServiceImpl:
Package com.jiading.service.impl;

com.jiading.dao.IAccountDAO Import;
Import com.jiading.dao.impl.AccountDAOImpl;
Import com.jiading.factory.BeanFactory;
Import com.jiading.service.IAccountService;

/
Account business layer implementation class
/
public class AccountServiceImpl the implements IAccountService {
// new new codes that dependent, less independent, it is necessary modify with the modification being dependent objects
// private IAccountDAO accountDAO = new AccountDAOImpl ( );

// can be modified to:
Private IAccountDAO AccountDao = (IAccountDAO) BeanFactory.getBean ( "AccountDao");
public void saveAccount () {
accountDAO.saveAccount ();
}
}

Client:
`` `java
package com.jiading.ui;

import com.jiading.factory.BeanFactory;
import com.jiading.service.IAccountService;
import com.jiading.service.impl.AccountServiceImpl;

/
Simulate a presentation layer, a business layer calls
/
public class Client {
public static void main (String [] args) {
// new new dependencies generated, so that the independence of the difference code
// IAccountService as = new AccountServiceImpl () ;

    //可以改造为:
    IAccountService as= (IAccountService)BeanFactory.getBean("accountService");
    as.saveAccount();
}

}
BeanFactory:

package com.jiading.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/ *
Plant a Bean object
Bean: English in the computer, there is the meaning of reusable components
JavaBean> entity class, javabean is java language reusable components
it is to create our service and dao object

  1. Require a configuration file to configure dao service and
    content configuration: fully qualified class name and unique identifier corresponding
    profiles or properties may be xml
  2. By reading the content of the configuration of the configuration file, creating a reflective bean object
    * /
    public class the BeanFactory {
    // definition of a Properties object
    private static Properties props;

    // define a Map, used to store the objects we want to create, we call it a container
    private static Map <String, Object> beans;

    // use the static code block is assigned to the Properties object
    static {
    // or use the new keyword, but this is dependent on Java modules rather than write our own, and this is progress
    // rely only be reduced but not eliminate, not a new keyword without programming time

     try {
         //1.实例化对象
         props = new Properties();
         //获取properties文件的流对象
         //在resources下的文件在部署时会被放置在根路径下,所以不需要写包名
         InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
         props.load(in);
    
         beans=new HashMap<String, Object>();
         Enumeration keys = props.keys();
         //遍历枚举
         while(keys.hasMoreElements()){
             String key=keys.nextElement().toString();
             //根据key获取value
             String beanPath=props.getProperty(key);
             //反射创建对象(也就是在初始化环节中在容器中分别创建一个对象以供之后调用,也就是单例模式
             Object value=Class.forName(beanPath).newInstance();
             //存入容器,以供调用
             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();
     }

    }

    /
    Get bean object based on the name of the Bean
    /
    public static Object the getBean (String the beanName) {
    / Object bean = null;
    the try {
    String beanPath = props.getProperty (the beanName);
    bean = the Class.forName (beanPath) .newInstance ();
    } the catch (Exception E) {
    e.printStackTrace ();
    }
    /
    // after the container has been, there is a more convenient method of getBean

     return beans.get(beanName);

    }
    }
    bean.properties:

= com.jiading.service.impl.AccountServiceImpl the AccountService
AccountDao = com.jiading.dao.impl.AccountDAOImpl
And now we use spring framework, the framework created by the spring. Here select the xml configuration file

Guess you like

Origin www.cnblogs.com/jiading/p/12368638.html