Spring Basics-top-concept article

Source: Internet
Author: User

* What is IOC (inversion of Control)?

That is control reversal. The idea is to control the direction of resource acquisition. The traditional way to obtain a resource is to initiate a request to the container, and then the container returns the resource, and after the IOC is applied, the container proactively sends the resource back to the component it manages, and the component simply chooses an appropriate way to receive the resource.

Example of buying vegetables.

* What is di (Dependency injection)?

That is, dependency injection. Generally speaking, a complex application requires multiple classes to work together, such as Class A to implement function A requires the collaboration of Class B, then class A is dependent on class B, the traditional practice is to create a B object directly inside Class A, and then use the function of B, and after applying dependency injection, Class A can receive the injection of Class B through some sort of convention, which can be by setter method or something.

In short: The component is in a well-agreed way (for example: Attribute injection: Property (the most common injection method in development), constructor injection: constructor-arg(when using the constructor injection, Receives an injection from a container resource by specifying the location of the injected attribute to differentiate the overloaded constructor)

* Why use IOC or di?

To understand decoupling, in order to improve the reusability of components

the scope of the *bean?

Use the scope property of the bean node to set

The default scope is: Singleton

Typically, the bean created in the IOC container is singleton, that is, for a configured BEAN,IOC container, a singleton object is created for the bean, that is, each time the response bean is fetched through getbean, it gets the same bean

You can set the scope: prototype (prototype)

That is, the configured Bean is a prototype, and each fetch of the bean is a newly created

*bean the declaration period?

The declaration period of a bean configured in an IOC container is managed by the container

The process of container-to-bean declaration cycle management is as follows:

To create an instance of a bean using a constructor or factory method

Setting the properties of a bean

Invoke the initialization method of the bean (the initialization method can be specified when the bean is configured, and the action of the response can be defined when the bean is defined)

The bean is ready to use

The method of destroying the bean is called before the container is closed (the configuration and definition of the destruction method is the same as the initialization method) (the so-called Post processor, which is the processor that operates the bean after the bean is instantiated)

For more detailed processing of the bean's life cycle, you can use the Bean's back processor beanpostprocessor

Obviously, what does it mean? In general, the part that we can participate in in the Bean's declaration cycle is: setting properties, setting the initialization method, setting the Destroy method, that's all, but, sometimes, before the bean is created, it needs to do some testing, not in the initialization method, because the logic is wrong, Initialization instructions This object will be created. Just think that can be done before the initialization, OK, demand came, there is a solution

For different beans may require a different post-processor, then what to do with their own write, Ok,spring provides a post-processor interface called Beanpostprocessor, you can write your own post-processor, As long as the two methods in this interface are implemented: Initialize the previous method and the post-initialization method. Then configure it in XML.

In fact, the previous use of a propertyplaceholdercongurer is also a bean after the processor, just spring comes with it

*spring automatic scanning when using annotations?

Component scanning (component scanning) is capable of automatically scanning, detecting, and instantiating components with specific annotations from classpath . These annotations include:

@Component basic annotations that identify a component that is managed by spring

@Respository identifies the persistence layer component

@Service represents the service layer (business layer) component

@Controller identifies the presentation layer component

(although this is the case, spring does not know what the annotated class is for, so spring only knows that the class labeled by the above tag is a component, and as for what the component does, I don't know, so these so-called tokens can actually be mixed)

For scanned components, spring has its own default naming policy: Use unqualified class names, first letter lowercase, or the Value property to identify the component's name, what does it mean? Since the annotations are used, the annotations do not configure the Bean in XML, what is the annotated bean's ID after instantiation, which is the preceding rule: the first letter is lowercase, the other remains unchanged, if you want to specify the name yourself? Use the Value property of the above annotations to specify

*spring generic Dependency injection?

First of all, this feature was added after Spring4

A reference to a member variable of a generic type that can be injected into a subclass in spring4.x. A good taste of this sentence, the core is: a pair of interfaces have dependencies, and both interfaces are generic classes, and the generic parameter types are interrelated, that for the subclass of these two interfaces, if the corresponding generic parameter type is the same, then these two subclasses will automatically Generate dependencies

* What is AOP (Aspect oriented programming)?

That is, tangent-oriented programming

First of all, it is due to two considerations:

1. There are some common functions, such as logs or transactions, which are all done, but for a specific business function, if you are responsible for writing the log, the code is much bloated, and the code is not very specific

2. For some common functions, if you are responsible for writing, the use of the place to write, said the bloated is on the one hand, but on the other hand it is cumbersome to modify, that the common functions encapsulated in a class and then call, it can improve the 2nd, but the 1th, the code bloat is inevitable

So how can you make business components more focused on your business, and those universal features that you don't know about? That is, using AOP, the core of AOP is dynamic proxy

Facets: A special object that has been modularized after a crosscutting concern (a function that spans multiple modules of the application)

Notice: The work to be done on the slice

Target: The bean being notified

Proxy: Applies the object after the notification to the target object

Connection point: A location where the program executes, the place where the notification can be inserted, is actually a place where the program executes: for example, before a method executes, or after a method executes, throws an exception, etc., is where the code actually exists, the connection point is the code itself.

Tangency: AOP connects to a connection point through a pointcut. There are many connection points in a class, and how do you apply notifications? At which connection points should the notification be applied? A tangent is a description of the point at which an AOP cut-in notification object is used to describe the location of an access connection point application notification

*spring jdbc Template (jdbc templates)?

In order for JDBC to be easier to use in spring, Spring has a persistent layer of encapsulation on the basis of JDBC, what does it mean? JDBC is usually used to use Java-defined official APIs directly, but there is a downside to this: there will be a lot of duplicate code, such as starting a transaction, then writing the content, then either committing the transaction, or rolling back the transaction, the entire code, the core code is to manipulate the database of the sentence, The rest is nothing to use for the code, that spring according to

This feature, JDBC is encapsulated, and a JDBC access framework is established.

As the core of the spring JDBC Framework, the purpose of the JDBC template design is to provide a template approach for different types of JDBC operations, each of which can control the entire process and minimize access to the database.

* constructor, static code fast, non-static code fast execution order?

The static code is faster than the non-static code sooner than the constructor.

Where the static code is quickly executed when the class is loaded, what does it mean, if there is a main method, the static code block is executed before the main method executes.

transaction management in the *spring?

Transaction management is an essential technology in enterprise-level development to ensure data integrity and consistency

Four key properties of a transaction (ACID):

Atomicity (Atomic): A transaction is an atomic operation, an action in a transaction is either all executed, or all is not executed

Consistency (consist): Once a transaction is executed correctly, the data is in a consistent state that satisfies the business requirements

Isolation (Isolated): Multiple transactions may handle mutually exclusive resources, requiring transactions to be isolated from other transactions to prevent data corruption

Persistence (Durable): Once the transaction is complete, its effect is persistent, usually, the impact of the transaction is directly written to the database, that is, if the transaction is executed correctly, then the database will be modified

With native JDBC transactions, there will be a lot of template code to write: Open the connection, start the transaction, start working, the transaction completes correctly, commit the transaction, close the connection, otherwise, roll back the transaction, close the connection. Obviously, this is only the beginning of the work of this part of the code is really useful, the other is the template code, the question is, can it be simpler? OK, use spring's declarative transaction.

What is a declarative transaction?

Declarative transactions are relative to ordinary transactions. The usual transaction is the transaction referred to above, called "hard-coded" transactions, meaning that the operation of the transaction is used in the same way that the transaction is used to write code.

That declarative transaction is not hard-coded. Spring's AOP framework supports declarative transaction management.

In most cases, declarative transaction management is more useful than hard-coded transaction management

The propagation properties of a transaction?

When a transaction method is called by another transaction method, the propagation property of the transaction must be specified. For example: The method might continue to be in an existing transaction, or it might open a new transaction and run it in

The propagation properties of a transaction can be determined by the propagation properties of the transaction

Spring Basics-top-concept article

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.