Some summaries in java

1. Is interface-oriented programming the same as abstract-oriented programming?
    Interface-oriented programming is abstract-oriented programming, for
    example:
    A is an interface (the interface is abstract), and B is a class (the generated class object is concrete).
        A s = new B();
    s has all the methods specified by the A interface (which must be implemented in class B).
    In this way, the C, D, E, F classes can also implement the A interface
    . If you want to replace them with C, D, E, F, it is very simple, and the methods specified by the A interface are still used when calling.
    (Interface-oriented programming means that the interaction between all classes or modules in an object-oriented system is completed by the interface. The
    explanation is as follows:
    A s = new B();
    Create a B object in the memory heap, in the memory A reference s of A is created in the stack to point to B, and the s.print() method in A is used to call
    the method of the same name in B (the method in A is implemented in B).
    The so-called interface-oriented programming can reduce the amount of time between programs. The coupling degree of , refers to the interface at the specific call site, and does not depend on a specific class. In
    A s = new B();, B can be replaced by any other class that implements the A interface. .
    Interface-oriented programming reduces the coupling between classes in Sping's IOC, which is well reflected.
    )
2. How does Spring manage transactions?
    It intercepts the configured methods through AOP, and then calls transaction management. The preset code in the server manages things, and then delegates the intercepted method to complete the original task.
How does Spring control things under the hood?
    The bottom layer of spring's control of things is the reflection mechanism. The bottom layer of spring is to use the reflection mechanism to achieve injection and control of things through declarative methods, that is, AOP in spring.
    The bottom layer of AOP is implemented. It is the dynamic proxy in the JAVA design pattern.
How does Spring implement dependency injection?
    In ordinary java development, when programmers need to rely on the methods of other classes in a certain class, they usually use a new dependent class and then call the method of the class instance. The problem in this development is that the new class instance is not easy to manage uniformly. Spring The idea of ​​dependency injection is put forward, that is, the dependent class is not instantiated by the programmer, but the spring container helps us to
    newly specify the instance and inject the instance into the class that needs the object. Another way of saying dependency injection is "inversion of control". The popular understanding is: usually we create a new instance, the control of this instance is our programmer, and inversion of control means that the work of the new instance is not done by our programmers Instead, it is handed over to the spring container to do it.
   
    Set injection:

    This is the simplest injection method. Suppose there is a SpringAction and a SpringDao object needs to be instantiated in the class, then you can define a private SpringDao member variable, and then create a SpringDao set method (this is the injection entry of ioc) ;
   
    public class SpringAction { 
        //Injection object springDao 
    private SpringDao springDao; 
        //Be sure to write the set method of the injected object 
        public void setSpringDao(SpringDao springDao) { 
        this.springDao = springDao; 
     } 
 
        public void ok(){ 
        springDao.ok(); 
     } 
    } 
    Then write the spring xml file, an alias of the name attribute in <benan>z, and the class attribute refers to the full name of the class, because in There is a public property Springdao in SpringAction, so create a <property> tag in the <bean> tag to specify SpringDao.
    The name in the <property> tag is the SpringDao property name in the SpringAction class, and ref refers to the following <bean name = "SpringDao "...>z In fact, spring instantiates the SpringDaoImpl object and calls the SetSpringDao method of SpringAction to inject SpringDao;
   
    <!--Configure the bean, which is managed by spring after configuration--> 
     <bean name="springAction" class ="com.bless.springdemo.action.SpringAction"> 
        <!--(1) Dependency injection, configure the corresponding properties in the current class --> 
        <property name="springDao" ref="springDao"></property>   
     </bean> 
    <bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>

    Constructor injection:
   
    This method of injection refers to constructor injection with parameters, see the following example , I created two member variables SpringDao and User, but did not set the set method of the object, so the first injection method cannot be supported. The injection method here is the injection in the SpringAction constructor, that is, when the
    SpringAction object is created To pass in the two parameter values ​​of SpringDao and User:

    public class SpringAction {
    //Injection object springDao
    private SpringDao springDao;
    private User user;

    public SpringAction(SpringDao springDao,User user){
    this.springDao = springDao;
    this.user = user ;
    System.out.println("The constructor calls springDao and user");
    }

    public void save(){
    user.setName("Kaka");
    springDao.save(user);
    }
    }
    In the xml file, the form of <property> is also not used, but the <constructor-arg> tag is used. The ref attribute also refers to the name attribute of other <bean> tags:
     
         <!--Configure bean, after configuration, this class is managed by spring- -> 
        <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 
            <!--(2) Create constructor injection, if the main class has a constructor with parameters, you need to add this configuration- -> 
            <constructor-arg ref="springDao"></constructor-arg> 
            <constructor-arg ref="user"></constructor-arg> 
        </bean> 
            <bean name="springDao" class="com. bless.springdemo.dao.impl.SpringDaoImpl"></bean> 
             <bean name="user" class="com.bless.springdemo.vo.User"></bean> 
   
    To solve the uncertainty of the parameters of the construction method, you may encounter that the two parameters passed in by the construction method are of the same type. In order to distinguish which value should be assigned, you need to do some small processing:
    the following is to set the index, which is the parameter position :

        <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 
            <constructor-arg index="0" ref="springDao"></constructor-arg> 
            <constructor-arg index="1 " ref="user"></constructor-arg> 
        </bean> 
   
    The other is to set the parameter type:
    <constructor-arg type="java.lang.String" ref=""/>

    Interface injection:
   
    in the interface Define the information to be injected and complete the injection through the interface. (Spring does not support this injection method -- the reason for not supporting it is -- Spring claims that it is a non-intrusive "live without this framework". If you use interface injection, it violates this principle.)
   

    Also note: through Objects created by Spring are singleton by default. If you need to create multi-instance objects, you can add an attribute after the <bean> tag:
        <bean name="..." class="..." scope="prototype"> 

Difference between AOP and OOP?

    AOP is aspect-oriented programming. Aop focuses on a certain step or stage of the business processing process, and emphasizes reducing the coupling between modules, which means that the code has better portability.
    Object-oriented programming is to encapsulate the methods and attributes of entities extracted in business analysis.
    A very important feature of Aop is the independence of source code, that is to say, if AOP component is introduced into our system, even if we remove the component, the system code should be able to compile and pass. To achieve this, dynamic proxy can be used. Mode, this
    has been well implemented in spring, and there are a lot of AOP in the management of things in spring.





   

   

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326692478&siteId=291194637