AL Spring Framework Learning Notes

Source: Internet
Author: User

1. Opening and closing principle

Object-Oriented Programming:

(1) Close the modification (try not to modify the source code)
(2) Open to expansion

2. Container

Something that can be removed and put into objects

3. Bean

What is placed in the container

4. Spring XML
<bean id="Boy1" class="Com.alipay.demo.CalabashBoy">(instantiated)< property name="Name"(Find set method) value="Dava"/> (injected attribute)< property name="HP" value="/> "    < property name="Color" value="RED"/>    < property name="Skill" ref="Strengthskill"/> </Bean>
5, Spring's shortcomings

Is slightly more cumbersome than direct write program debugging, which becomes a run-time exception from a compilation exception. Because XML is only going to be parsed when it runs.

6. IOC and DI

IoC: Control reversal, originally as an entity class, own things (attributes) should worry about themselves, but now only provide a get/set group, by others to tube, oneself become a pojo. This reverses the resource control.

DI: Entity classes rely on containers to inject.

7, Dependency injection common knowledge points

8. How Bean is instantiated

9. Bean Injection Method

Spring injection is divided into two ways:
(1) Set Value injection
(2) Construction injection

For a Java constructor, here's a little bit more to add:

(1) Each class in Java has a default parameterless construction method.

(2) can also be displayed to write out a non-parametric construction method, and can do some operations in the method

(3) If the non-parametric construction method is not displayed, and the construction method with parameters is written out, the default parameterless construction method is overwritten.

10. Special Injection method

The first one is an internal bean, which is only used by itself, and if it is used by everyone, it is constructed outside and can be quoted by everyone.

11. Automatic Binding

The default is byname, preferably also explicitly specified byname

12. Scope of Bean

The beans in spring are all singleton

Do not put the process of processing data into the object's private properties, different threads, the same object to provide services, prone to problems, will overwrite each other. But the method body class variable does not matter, this function stack is a single thread exclusive.

13. Bean Life Cycle

Spring provides hooks for many containers: a single object should have no knowledge of the container, but sometimes the bean wants to do some necessary interaction with the container. such as: Tell yourself when destroying, or get container information.

Beannameaware will be able to get the name of spring, such as to get the following boy1.

<bean id="Boy1" class="Com.alipay.demo.CalabashBoy">    < property name="name" value="Dava"/>    < property name="HP" value="/> "    < property name="Color" value="RED"/>       < property name="Skill" ref="Strengthskill"/></Bean>

But the spring principle should be non-invasive, try not to use spring's stuff, try to be Pojo

Many are constructed methods (no parameters) and destroy are relatively small. such as (two methods), preferably the first, not coupled spring:

Custom destruction is not used much:

14. Bean Dependency

It is generally used to inject automatic implementation with ref

15. Delay Initialization

16. Inheritance of Bean definition

Define a parent class bean

17. An example of how to monitor the movements of a gourd doll

Let the object not perceive itself to be monitored

Note Several points:
(1) The proxy class is inherited gourd doll, which means that he is a gourd doll class, because to put in the container for others to see.
(2) The inside of this gourd doll contains a gourd baby object.

18. Dynamic Agent for Java

Spring provides agent factory

<bean id= "boy2" class=" Org.springframework.aop.framework.ProxyFactoryBean ">    < property name="target" ref="Boy2target"/>    < property name="Interceptornames">        <list>            <value>Monitorinterceptor</value>        </list>    </Property ></Bean>

Intercept the action through the interceptor.

You don't have to specify the proxy type.

private CalabashBoy target;

Interceptor Class Code:

/** * interceptors used for monitoring. * * @author hui.shih * @version $Id: Monitorintrceptor.java, v 0.1 2014-4-27 pm 02:55:50 hui.shih Exp $ */ Public  class monitorinterceptor implements methodinterceptor{    /** Mirror Client * /    PrivateMagicmirrorclient magicmirrorclient;/** * @see Org.aopalliance.intercept.methodinterceptor#invoke ( org.aopalliance.intercept.MethodInvocation) * /    @Override     PublicObjectInvoke(Methodinvocation invocation)throwsThrowable {Object target = Invocation.getthis ();//Here only the Calabashboy.useskill to deal with, the other directly let go        if(TargetinstanceofCalabashboy | |"Useskill". Equals (Invocation.getmethod (). GetName ())) {Calabashboy calabashboy= (calabashboy) target;//1. Before the target method executes            if(Calabashboy.getskill ()! =NULL) {Magicmirrorclient.send (Calabashboy.getname () +"Let's go, everybody hide!" "); }//2. Implementing a true target approachObject result = Invocation.proceed ();//3. After the target method is executed            if(Calabashboy.getskill ()! =NULL) {Magicmirrorclient.send (Calabashboy.getname () +"The skill is cool, everybody can come out!" "); } System.out.println ();returnResult }Else{returnInvocation.proceed (); }    }/** * Setter method for property <tt>magicmirrorclient</tt>. * * @param magicmirrorclient value to being assigned to property magicmirrorclient */     Public void setmagicmirrorclient(Magicmirrorclient magicmirrorclient) { This. magicmirrorclient = magicmirrorclient; }}

In theory, all methods are intercepted, but only where specific needs are addressed.

But more is the use of Beannameautoproxycreator:

<bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">    <property name="interceptorNames">        <list>            <value>monitorInterceptor</value>            <value>timerInterceptor</value>方法调用时间        </list>    </property>    <property name="beanNames">        <list>            <value>boy1</value>            <value>boy3</value>        </list>    </property></bean>

Interceptors are nested in sequence

19. AOP Related Concepts

Interceptors are the combination of common operations, and business methods do not know

To intercept with an expression:

<aop:config> <aop:pointcut id="PointCut"expression="Execution (* Com.alipay.demo.CalabashBoy.useSkill (..))"/> <aop:aspect ref="Timeraspect"> <aop:around method="Timingaround"pointcut-ref="PointCut"/> </aop:aspect> <aop:aspect ref="Monitoraspect"> <aop:before method="Monitorbefore"pointcut-ref="PointCut"/> <aop:after method="Monitorafter"pointcut-ref="PointCut"/> <aop:after-returning method="Monitorafterreturning"returning="Success"pointcut-ref="PointCut"/> </aop:aspect> </aop:config>

Can before, after

Every interceptor in the medium is not the real target, it's the object of deception.

Why do we have to pack one layer at a level?

Because each layer is tightly coupled, tight coupling is put together. And each layer of interceptors is decoupled.

20. Supplement

AL Spring Framework Learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.