Java Tour-Simplifying Java development

Source: Internet
Author: User

To reduce the complexity of Java development, Spring has adopted the following 4 key strategies

    1. Pojo-based lightweight and minimal intrusion line programming

To keep the app from being tied to the framework, a bean will not implement, inherit or import anything related to the spring API, just a plain Java object.

2. Loose coupling via dependency injection and face interface

Each object is responsible for managing references to objects that it interacts with (that is, the objects it relies on), which leads to highly coupled and hard-to-test code.

Highly coupled means that a class wants to manage its own objects,

 Public class Implements perform{    private  star star;      Public performshow () {        thisnew  SuperStar ();    }}

This is a typical example, which will make

      • Internal object instances can be changed poorly and functionally too single.
      • Unit testing can be difficult to do because it is not easy to remove the internal object star.

The transformation method is to use dependency injection to achieve loose coupling. It is divided into two kinds: constructor injection and setter injection.

 public  class  performshow implements   perform{ private  Span style= "color: #000000;"        > star star;  // constructor  public   Performshow (Star Star) { this.star = star;  // for Setter property  public   Setstar (Star Star) {    this . Star = star; }}

By injecting, this is actually the way it is, but there are a few to be aware of.

      • This unit test is good because we have the start object.
      • The objects and execution methods inside the class are changeable, previously restricted to the superstar class. Now what star can only implement the star interface. This is also the meaning of loose coupling, is the interface to achieve transmission, reduce the coupling relationship.
      • In fact, XML-based and annotated injection methods are similar to this, is the same way.
        New SuperStar ();         New Performshow (Star);

DI: The dependency of an object is set at creation time by the third-party component that is responsible for coordinating the objects in the system. Objects do not have to create or manage their dependencies on their own.

The following is the syntax for using XML.

       <BeanID= "Performconstructor"class= "Spring.in.action.bean.beanInjectionInitDestroy.PerformShow">              <!--constructor injection, simple value with value, object with re-declaration of a Bean,ref -              <Constructor-argref= "Star"/>       </Bean>       <BeanID= "Star"class= "Spring.in.action.bean.beanInjectionInitDestroy.SuperStar">              < Propertyname= "Name"value= "Andy Lau"/>       </Bean>

Use the context to load it in, so that the perform interface does not know the implementation of the class is who, and do not know its internal star interface is how star, to achieve loose coupling.

        New Classpathxmlapplicationcontext ("Spring-injection-init-destroy.xml");         = (Perform) context.getbean ("Performconstructor");

3. Declarative programming (AOP) based on facets and conventions

Dependency Injection keeps the collaborative software components loosely coupled, and AOP becomes a reusable component that allows the separation of functionality throughout the application.

System services such as logs, transaction management, and security are often integrated into the components of their core business logic, which are usually crosscutting concerns and do not belong to any business component, which requires AOP programming.

This can be a component that is more concerned about its own business and has high cohesion.

    

 Public classFamilycuiImplementsFamily {PrivateMember Member;  Publicfamilycui (Member Member) { This. Member =member; }     Public voiddosomething () {member.dojob (); }} Public classFamilylog { Public voidBeforfun () {System.out.println ("Want to know what the children are doing!" "); }     Public voidAfterfun () {System.out.println ("Oh, I know."); }}
       <!--family's Bean -       <BeanID= "Family"class= "Spring.in.action.diAopSimple.di.FamilyCui">              <Constructor-argref= "Member" />       </Bean>       <BeanID= "Member"class= "Spring.in.action.diAopSimple.di.MemberSon"></Bean>       <!--the classes that the AOP aspect to use -       <BeanID= "Familylog"class= "Spring.in.action.diAopSimple.aop.FamilyLog"></Bean>       <!--Configuration of AOP -       <Aop:config>              <!--using Familylog to do AOP -              <Aop:aspectref= "Familylog">                      <!--Defining Pointcuts -                      <Aop:pointcutID= "Dosomthing"expression= "Execution (* *.dosomething (..))"/>                      <!--Associate Pointcuts, and specify methods -                      <Aop:beforePointcut-ref= "Dosomthing"Method= "Beforfun"/>                      <Aop:afterPointcut-ref= "Dosomthing"Method= "Afterfun"/>              </Aop:aspect>       </Aop:config>

First we have a family class, we want to take his approach as a starting point, then we need a class familylog, then define our slicing method Beforefun and Afterfun, and declare when the pointcut is used.

4. Reduce boilerplate code with facets and templates

This is a good understanding, in the case of JDBC, there are a lot of open and close actions before the actual code, which can be done with spring.

Java Tour-Simplifying Java development

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.