Job interview: Spring core principles and working methods __java

Source: Internet
Author: User
Tags aop error handling reflection to domain

Foreword: The most interview is the SPRINGMVC principle, database optimization, JVM tuning, multithreading Security, reflection, serialization and deserialization. This blog is collected from the Internet. Let's look at how spring works and how things work.       Spring Principle One of the core aspects of spring is AOP, which is the aspect-oriented programming that can be used to monitor and control a class of objects (that is, to invoke the module you specify before and after the specific method of calling the object) to extend the function of a module. These are all achieved through the configuration class.
The spring objective is to make the relationship between objects and objects (modules and modules) not connected by code, and is managed by the Configuration class description (Spring is based on these configurations to dynamically assemble objects internally through reflection)
Remember that spring is a container in which the objects in the container have the services and functionality that spring provides.
One of the most classic design patterns used in spring is the template-method pattern. (I do not introduce here, is a very common design pattern), spring in the configuration is a lot of, it is difficult to remember, but spring's essence is nothing more than two points above, the above two points and understand the basic grasp of spring.
Spring AOP and IOC IoC (Inversion of control): Inversion of controls
IoC:
Concept: Control is diverted from the object itself to the container; The container creates the instance based on the configuration file and creates dependencies between the instances
Core: Bean factory; In spring, the individual instances created by the Bean factory are called beans


AOP (aspect-oriented programming): Aspect-oriented programming
1, the agent of two ways: static agent:
 for each specific class to write the agent class respectively;
Write a proxy class for an interface;
Dynamic Agent:
Write a invocationhandler for one aspect and then use the proxy class in the JDK reflection pack to dynamically generate the corresponding proxy class dynamic proxy for various interfaces

Without writing proxy class, the virtual machine generates a class based on the interface of the real object, a dynamic agent is instantiated by class, the real object and equipment are injected into the dynamic proxy when the dynamic agent is instantiated, and the dynamic proxy is exposed to the client, and when the client invokes the dynamic proxy method, The dynamic proxy obtains the real object's method based on the reflection of the class, invokes the equipment's invoke methods, and passes the dynamic proxy, method, parameter and the device invoke method, the Invoke method to do some processing before or after the method is aroused.

1, the generation of dynamic proxy class:

Java.lang.refect.Proxy


2, the equipment must realize the Invocationhandler interface realization Invoke method reflection

What is the reflection of the class.

The class description can get the class's parent class, implemented interface, inner class, constructor, method, property and can instantiate an object according to the constructor, arouse a method, take the attribute value, and change the property value.

How to get a class description:

Class cls= classes.

Class cls= object. GetClass ();

Class.forName ("Class path");

How do I get a method and evoke it?

Class cls= classes.

Constructor Cons=cls.getconstructor (new Class[]{string.class});

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

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

Setter

Interface

Constructor

Factorybean: The main implementation of the factory bean Ioc/di

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

Object Obj=ac.getbean ("id value");

7 Modules of the Spring framework


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 bean is created, configured, and managed, and each module (or component) that makes up the spring framework can exist separately or be implemented jointly with one or more other 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. Beanfactory uses control reversal (IOC) mode 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, checksums, and scheduling capabilities.
Spring AOP:By configuring the management features, the Spring AOP module integrates aspect-oriented programming capabilities directly into the spring framework. Therefore, it is easy to make any object managed by the Spring framework support AOP. The spring AOP module provides transaction management services for objects in a spring-based application. By using Spring AOP, you can integrate declarative transaction management into your application without relying on EJB components.
Spring DAO:The 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 (for example, opening and closing connections). The Spring DAO's JDBC-oriented exception conforms to the common DAO exception hierarchy.
Spring ORM:The Spring framework inserts several ORM frameworks, providing an ORM object-relational tool, including JDO, Hibernate, and IBatis SQL maps. All of these conform to Spring's common transaction and DAO exception hierarchy.
Spring Web module:The Web context module is built on top of the application context module, providing a context for web-based applications. So, the Spring framework supports integration with Jakarta Struts. The Web module also simplifies the work of processing multiple-part requests and binding request parameters to domain objects.
Spring MVC Framework:The MVC framework is a fully functional MVC implementation that builds 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 Spring framework features can be used in any Java EE server, and most of the functionality also applies to an environment that is not managed. The central point of Spring is to support reusable business and data access objects that are not bound to a specific Java EE service. There is no doubt that such objects can be reused in different Java EE environments (Web or EJB), stand-alone applications, and test environments.

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.