Spring Core Concepts

Source: Internet
Author: User
Tags aop constructor error handling exception handling file upload reflection to domain tomcat
Learning knowledge to learn what the concept of the knowledge point is, what the benefits are, where it is applied, and what the difference is between it and something like it. first knowledge of spring Enterprise applications: refers to the solutions and applications that are created and deployed for business organizations and large enterprises. These large enterprise-level applications are complex in structure, involving many external resources, transaction-intensive, large data size, many users, strong security considerations and high performance requirements. Spring is a lightweight Java open source framework that was created in 2003 by Rod Johnson. In short, spring is a layered javase/ee full-stack (one-stop) lightweight open-source framework.
Spring "Green Grass" Spring does give people an exceptionally fresh, hearty feeling, as if the green grass after the rain, pleasing people like there is a vitality. Spring is a lightweight, open-source framework that greatly simplifies Java enterprise-level development and provides powerful and stable functionality. There is no extra burden, so people have a sense of decency and elegance when it comes to using it for every thing. Spring has two main goals: one is to make existing technologies easier to use, and two to promote good programming practices (or best practices) Spring is a comprehensive solution, but it adheres to one principle: without reinventing the wheel, there is already a better solution for the field, never to do a repetitive implementation. For example, object persistence orm,spring only supports existing JDBC, hibernate, and other technologies, making it easier to use rather than re-implementing it.
Spring Core Concepts 1: Control inversion (inversion of CONTROL,IOC), also known as Dependency injection (Dependency injection,di) to reduce the degree of coupling between program code. The so-called control reversal, that is, control is reversed, userbiz no longer relies on its own code to obtain the specific DAO object to rely on, but the work to the third party---userdaofactory
In contrast to "control inversion", "dependency injection" is easier to understand, that is, the container, such as spring, is responsible for "injecting" (assigning) the specific object that the component depends on to the component, thus avoiding the hard-coded coupling of the components spring automatically takes over the creation of the bean in the configuration file and the assignment of a value to the property when spring assigns a value to a bean property, it is implemented by invoking the setter method of the property, which is called "Set Value injection"
<bean id= "" class= "" > <property name= "corresponding setter method name" > <value>Spring</value> </property> <!--can also be written like this---<property name = "Corresponding setter method name" value= "Spring"/> <!--The Value property is used to inject the base data type and the string type The value of the ref attribute is used to inject an already defined bean--</bean>
ApplicationContext ac=new classpathxmlapplicationcontext ("Applicationcontext.xml"); Object Obj=ac.getbean ("id value");//can be strongly converted to the type you want/* ApplicationContext is an interface that is responsible for reading the spring configuration file, managing the loading, generation, and maintenance of the relationship between the bean objects. Responsible for the life cycle of the Bean object in addition to Classpathxmlapplicationcontext, Filesystemxml ApplicationContext is also a subclass of the ApplicationContext interface. can also be used to load spring configuration files */

the core mechanism of spring is the dependency injection the core mechanism of struts is the interceptor
1. Spring principle

The core of the internal is the IOC, dynamic injection, so that an object is created without new, can be automated production, which is actually the use of Java reflection, reflection is actually in the runtime to create, invoke the object, spring is at runtime, with the XML The spring configuration file is used to dynamically create objects and invoke methods in the object.
One of the core aspects of spring is AOP, which is the aspect-oriented programming that can be supervised and controlled for a class of objects (that is, calling the module you specify before and after invoking the specific method of such an object) to extend the functionality of a module. These are achieved through the configuration class.
Spring purpose: The relationship between objects and objects (modules and modules) is not associated with code, but is managed through the Configuration class description (Spring uses reflection to dynamically assemble objects within these configurations)
Keep in mind that spring is a container in which the objects in the container will have these services and functions provided by spring.
One of the most classic design patterns used in spring is the template method pattern. (Here I do not introduce, is a very common design pattern), spring configuration is a lot of, it is difficult to remember, but the essence of spring is nothing more than the above two points, the above two points with understanding of the basic master of spring.

Spring AOP and IOC
I. IoC (Inversion of control): Inversion of controls
1. IoC:
Concept: Control is shifted from the object itself to the container; The container creates an instance from the configuration file and creates a dependency between the individual instances
Core: Bean factory; In spring, each instance created by the Bean factory is called a bean
Second, AOP (aspect-oriented programming): Aspect-oriented programming
1, the agent of two ways:
Static Proxy:
 Write the proxy class for each specific class;
 Write a proxy class for an interface;
Dynamic Agent:
Write a invocationhandler for one aspect, then use the proxy class in the JDK reflector package to dynamically generate the corresponding proxy class for various interfaces

2. Dynamic Agent:

Instead of writing the proxy class, the virtual machine generates a class based on the interface implemented by the real object, instantiates a dynamic proxy through the class, injects the real object and equipment into the dynamic agent when the dynamic agent is instantiated, exposes the dynamic agent to the client, and when the client invokes the dynamic proxy method, The dynamic proxy obtains the real object's method according to the reflection of the class, invokes the call method of the equipment, passes the dynamic agent, method, parameter and the Invoke method of the equipment, and the Invoke method does some processing before or after invoking the method.

1, the generation of dynamic proxy class:

Java.lang.refect.Proxy

2, equipment must implement Invocationhandler interface implementation Invoke method 3, Reflection

What is a class's flyback?

The class description allows you to get the parent class, the implemented interface, the inner class, the constructor, the method, the property, and the constructor to instantiate an object, invoke a method, take the property value, and change the value of the property. How to get a class description:

Class cls= classes.

Class cls= object. GetClass ();

Class.forName ("Classpath");

How to get a method and evoke it?

Class cls= classes.

Constructor cons=cls.getconstructor (New Class[]{string.class});

Object obj=cons.newinstance (New object[]{"AAA"});

Method Method=cls.getmethod ("Methods name", New Class[]{string.class,integer.class});

Method.invoke (obj,new object[]{"AA", New Integer (1)}); 4. What are the three ways to inject spring?

Setter

Interface

Constructor 5, what is the core interface and kernel configuration file of spring?

Factorybean: Factory Bean mainly implements Ioc/di

ApplicationContext ac=new filexmlapplicationcontext ("Applicationcontext.xml");

Object Obj=ac.getbean ("id value"); 6, Spring Framework 20 Functions 7 modules, respectively, data access/integration (Access integration), Web, AOP, Instrumentation (device), Messaging (message sending), Core Container (Core container), Test

4.1 Core Container

The core container contains Spring-core, Spring-beans, Spring-context, Spring-context-support, and spring-expression (spring expression Language,spring expression Voice) these modules.

Spring-core and Spring-beans form the most fundamental part of the framework, including control inversion and dependency injection. Beanfactory is a very mature implementation of the factory model.

Spring-context is built on Spring-core and Spring-beans, which provides a way to access objects in a framework style, similar to Jndi registration. The ApplicationContext interface is the focus of the spring-context. Spring-context-support provides support for integrating third-party libraries, such as timer quartz. Spring-expression provides a powerful expression language that can query and manipulate objects at run-time.

4.2 AOP and devices

The SPRING-AOP module provides an AOP-oriented implementation of tangent-plane programming. For example, developers are allowed to define method interceptors and pointcuts to decouple business logic from system services.

The Spring-aspects module provides integration with the ASPECTJ.

The Spring-instrument module provides class device support and loader implementations that are used in common application servers. The Spring-instrument-tomcat module contains the spring Tomcat device agent.

4.3 Message sent

Spring 4 contains a spring-messaging module that provides an important abstraction of the Spring Integration Project message, Messagechannel, and MessageHandler, which is based on the messaging application.

4.4 Data Access/integration

The data access/Integration layer contains JDBC (SPRING-JDBC), ORM (Spring-orm), OXM (SPRING-OXM), JMS (SPRING-JMS), and Transaction (SPRING-TX) modules.

4.5 Web

The Web tier contains Spring-web, SPRING-WEBMVC, Spring-websocket, and Spring-webmvc-portlet modules. Among them, Spring-web provides basic features for Web integration, such as file upload functionality. The SPRING-WEBMVC module contains spring's MVC and Rest Web service implementations. The Spring-webmvc-portlet module provides an MVC implementation in a portlet environment.

4.6 Testing

The Spring-test module supports spring to build unit and integration tests for JUnit and testng.

5 Why use Spring?

Spring's two core functions of IOC and AOP can greatly reduce the coupling of application systems and simplify the development process.

Spring Framework technology can work at different levels, such as IOC managing common Pojo objects, and AOP enhances system services and other components (transactional, MVC, JDBC, ORM, and remote invocation, etc.). One of the main features of spring is that it is a non-intrusive service based on the interface. The client binding interface uses Java EE services rather than directly binding services, and the application can use different services (Hibernate, MyBatis, etc.). We can use part of the spring service to our own needs without having to use the full Spring Series project.


The Spring framework is a layered architecture consisting of 7 well-defined modules. The spring module is built on top of the core container, and the core container defines how the beans are created, configured, and managed, and each module (or component) that makes up the spring framework can exist separately or be implemented in conjunction with one or more modules. The functions of each module are as follows:

Core container: The core container provides the basic functionality of the Spring framework. The main component of the core container is Beanfactory, which is the implementation of the factory pattern. The beanfactory uses the inversion of control (IOC) pattern to separate the application's configuration and dependency specifications from the actual application code.

Spring Context: The spring context is a configuration file that provides contextual information to the spring framework. The Spring context includes enterprise services such as JNDI, EJB, e-mail, internationalization, checksum scheduling.

Spring AOP: With the configuration management feature, the Spring AOP module directly integrates aspect-oriented programming capabilities into the spring framework. Therefore, it is easy to enable any object managed by the Spring framework to support AOP. The spring AOP module provides transaction management services for objects in spring-based applications. By using Spring AOP, you can integrate declarative transaction management into your application without relying on EJB components.

The Spring dao:jdbc DAO Abstraction Layer provides a meaningful exception hierarchy that can be used to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the number of exception codes that need to be written (such as opening and closing connections). Spring DAO's JDBC-oriented exception conforms to the common DAO exception hierarchy.

The Spring orm:spring Framework inserts several ORM frameworks, providing ORM object-relational tools, including JDO, Hibernate, and IBatis SQL Map. All of these conform to Spring's common transaction and DAO exception hierarchies.

Spring Web module: The Web context module is built on top of the application context module and provides the context for Web-based applications. Therefore, the Spring framework supports integration with Jakarta Struts. The Web module also simplifies the process of working with multipart requests and binding request parameters to domain objects.

Spring MVC framework: The MVC Framework is a full-featured MVC implementation of building WEB applications. With the policy interface, the MVC framework becomes highly configurable, and MVC accommodates a large number of view technologies, including JSP, Velocity, Tiles, IText, and POI.

The functionality of the Spring framework can be used in any Java EE server, and most features are also available in an out-of-management environment. The core point of Spring is to support reusable business and data access objects that are not tied to a particular Java EE service. There is no doubt that such objects can be reused across different Java EE environments (Web or EJB), standalone applications, and test environments.


2:spring AOP Aspect-oriented programming: (Aspect Oriented Programming) is object-oriented programming

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.