Spring Framework Learning Notes (ii)--two core ideas __java

Source: Internet
Author: User
Tags aop
1.IoC (Control reversal)

The so-called control reversal, as long as a class of its internal state control to other mechanisms to complete. is to not create objects in the program, only in the configuration file, to describe how to create them. In code, you are not directly connected to objects and services, and you are describing in the configuration file which service is required for which component.

The IoC (inversion of control) is the purpose, DI (injection-dependent) to achieve the means of control reversal.

Dependency injection means that the creation and service of dependent objects are given to the configuration file and the spring framework (Programs need to create objects and corresponding services without using the Spring framework), and dependent classes need only declare the required member variables. That is, the dependent class was supposed to take the initiative to get the object, but after the dependency injection the object was provided by a third party, and it was only necessary to declare what object was needed.

Give me a chestnut:

When you do not use the Spring framework:

  Using the traditional method, do not consider using the Spring framework
  userservice userservice = new UserService ();
  Userservice.setname ("Dr_xiao");
  Userservice.sayhello ();

When using the Spring framework:

  Use the Spring Framework method
  //1. The introduction of Spring's development package (idea will automatically download)
  //2. Create the spring core file Spring-config.xml
  //3. Create a Spring IOC container object
  ApplicationContext context = new Classpathxmlapplicationcontext ("Spring-config.xml");
  4. Get the bean instance from the IOC container
  userservice userservice = (userservice) context.getbean ("UserService");
  5. Call the corresponding method
  Userservice.sayhello ();

2.AOP (aspect-oriented programming)

The so-called aspect-oriented programming is to program all objects or a class of objects (security, log, everything belongs to the slice). That is, instead of focusing on object programming, you can create more non behavioral concerns and insert them into your application. The goal is to allow programs to dynamically add new services without changing the code.

Aspect-oriented programming is a complement to object-oriented programming, which dynamically plunges code into the specified method and location of the class.

Object-oriented programming (OOP) distinguishes a class from the landscape, while aspect-oriented programming (AOP) is the vertical addition of specific code to the object.

Method steps for using AOP:

Defining components: Business components, notification components (front, back, wrapping ...) Configures components in a container, as well as dependency injection between components using an AOP injection notification to instantiate the container, taking the component out of the container for use

Give me a chestnut:

//1. Define interface//2. Write the object (the proxy object = target object)//3. Write the notification (called before the target method call)//4. Configuring in an XML file//4.1 configuring the Proxy object = target object//4.2 configuration notification 4.3 The configuration proxy object is a Proxyfactorybean object instance//4.3.1 <!--Proxy interface set-->//4.3.2 weaving notification//4.3.3 Configuring the proxy object 

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.