Spring stage study concluded (xiv) a preliminary understanding of decoupling and thread-safety issues

A, (coupling procedure)

The so-called coupling procedure, when the pair is developed when a person's code structure, it will be influenced by the progress of the development progress of the other members of the code, this will cause great harm.

For example:
when before I was writing plain Java Web program, I will call the Dao layer directly on the Service layer method, then if in the development of a large project, I only Service layer, and others responsible for Dao layer, I direct calls his method, if he did not finish, I have no way to continue to write the code, the program can not run.

 

Second, the decoupling procedure (the BeanFactory Method)

Can be invoked by writing a reflection the BeanFactory, such parameters will be written directly required profile, not only greatly reduces the coupling of the code, but also simplifies the structure of the code.

 

Three, BeanFactory drawbacks

After testing, found that when using the BeanFactory, he is a multi-mode use cases, this will call upon members within a method to create multiple instances, it is not needed (because the inner member access method in a multi-threaded, will not be affected , will reduce the operating efficiency program)

 

Fourth, the security thread

When defining a class member instance, when multithreaded access, will be affected, for example, I was in Servlet within the class definition class member variables a = 1 each time you visit a ++ ,, When I first visited with A browser will display a = 1,

But when I use the browser to access B again, will show a = 2, different browsers may represent different user access, it is clear that when multi-threaded access, define a class member instance will seriously affect the thread safety.

Fifth, above all understand, if you have it wrong, correct me hope to receive, thank you. Attached below is an example of a decoupling BeanFactory

1 public interface StudentDao {
2 
3     public void studentDao();
4 
5 }
1 public class StudentDaoImp implements StudentDao {
2     @Override
3     public void studentDao() {
4         System.out.println("我是StudentDao!!");
5     }
6 }
1 public interface StudentService {
2 
3     public void studentService();
4 
5 }
 1 import BeanFactory_Thread.BeanFactory.Factory;
 2 import BeanFactory_Thread.dao.StudentDao;
 3 
 4 
 5 public class StudentServiceImp implements StudentService {
 6     @Override
 7     public void studentService() {
 8         System.out.println("我是StudentService!!!");
 9         // StudentDao studentDao=new StudentDaoImp();
10         Factory factory = new Factory();
11         StudentDao studentDao = (StudentDao) factory.getBean("StudentDao");
12         studentDao.studentDao();
13     }
14 }

 

. 1  Import the java.util.ResourceBundle;
 2  
. 3  public  class Factory's {
 . 4      // by ResourceBundle reading the configuration file 
. 5      Private  static ResourceBundle ResourceBundle the ResourceBundle.getBundle = ( "beanFactory_Thread" );
 . 6  
. 7      // define the getBean () function to retrieve the desired class 
. 8      public  static Object the getBean (String the beanName) {
 . 9          // to obtain a class path by passing parameters 
10          String beanPath = ResourceBundle.getString (the beanName);
 . 11          Object className = null ;
 12 is          the try{
 13              // read classpath,
 14              // Get an instance of the class the forName () is not generated to load a class instance of a various types,
 15              // the newInstance () is in the loaded class generated on the basis of a examples of classes of 
16              className = the Class.forName (beanPath) .newInstance ();
 . 17          } the catch (Exception E) {
 18 is              e.printStackTrace ();
 . 19          } the finally {
 20 is              // returns an instance of the class 
21 is              return className;
 22 is          }
 23      }
 24  
25 }
. 1  Import BeanFactory_Thread.BeanFactory.Factory;
 2  Import BeanFactory_Thread.service.StudentServiceImp;
 . 3  
. 4  public  class the Main {
 . 5      public  static  void main (String [] args) {
 . 6          Factory's Factory = new new Factory's ();
 . 7          // avoid the direct use of NEW StudentDaoImp () calls this way, there is no dependence 
. 8          studentServiceImp studentServiceImp = (studentServiceImp) factory.getBean ( "StudentService" );
 . 9          studentServiceImp.studentService ();
 10      }
 . 11 }

 

Guess you like

Origin www.cnblogs.com/zhang188660586/p/11600721.html