"Spring Development"--spring Core

Source: Internet
Author: User


 Recently, due to some of the needs of the work, but also the lack of their own knowledge of the spring opened again. Just sort out the relevant knowledge and make up for some misconceptions about spring. This time, I learned a deeper understanding of IOC and AOP, and a further understanding of its architecture module.

Just started to look at Spring Technology Insider, although there is a bit of experience in the source code, but directly to see such a deep source, or very headache. Spring because of the expansion of the business, as well as the increase of the user base, for some modules of the class encapsulation is very deep! Therefore, tracing the source code is a very headache, and directly read this book, is also the pressure of Alexander.

So go back to the basic knowledge of spring, learn to walk, learning to run.

Let's talk about spring's core module-related concepts, IOC and AOP.

The IoC (inversion of control) controls inversion, that is, control is transferred by the object, and the object itself becomes passively accepted.

DI (Dependency injection) Dependency Injection

AOP (aspect oriented programming) for tangent programming

Let's talk about this AOP in a nutshell.

In AOP, the aspect-oriented programming theory is used to treat each method or function as a facet, and the different methods can be executed before and after the slice.

1 <BeanID= "Service">2     < Propertyname= "Interceptornames"value= "Advisor"></ Property>3     < Propertyname= "Target">Goal</ Property>4 </Bean>

This defines an interceptor, a target bean.

1 <BeanID= "Advisor"class="...">2     < Propertyname= "Advice">//Interceptor Object3     </ Property>4     < Propertyname= "Mappedname">//Methods of interception5     </ Property>6 </Bean>

When the target Bean executes, it will determine the slice according to the configuration, to the exact method. The interception method is determined by the mappedname above, either by name or by regular expression. The Interceptor object determines the function that is triggered when blocking.

Methods can be intercepted before the method, interception, and abnormal interception.


You can see that spring core is the module of the lowest layer core in the spring framework. Provides the most basic framework of IOC thought.

Let's explain how to use it!

Since spring is a class that is configured in an XML configuration file, there are some dependencies or injections involved, and so on. Then this configuration file must be localized for storage and read.

So before we use it, we need to take three steps:

Read the spring configuration file

Bean definition

Injected into the IOC container

Then we can get the specified bean through the getbean of the Bean factory.

 1 The most common three ways to read a configuration file:

1 Use Classpathresource to read the resource file and pass in the Xmlbeanfactory

1 New Classpathresource ("Application.xml")2new  xmlbeanfactory (res); 3 Factory.getbean ("Testbean");

2 using the input stream to pass in the Xmlbeanfactory

1 New FileInputStream ("C:\\applicationcontext.xml");2new xmlbeanfactory (IS);

3 Read resource files directly from Classpathxmlapplicationcontext

New Classpathxmlapplicationcontext ("Aaplicationcontext.xml");

4 is automatically read by specifying the directory of the configuration file in Web. Xml. The path to the configuration file is written in many ways, either by specifying CLASSPATH.

1 <Listener>2         <Listener-class>3 Org.springframework.web.context.ContextLoaderListener4         </Listener-class>5     </Listener>6 7 <Context-param>8   <Param-name>Contextconfiglocation</Param-name>9              <Param-value>Ten /web-inf/classes/bean.xml One              </Param-value> A  </Context-param>


2 Bean definition This part is nothing to say, is to read the configuration file, parse the inside of the file definition, extracted into a data model.

3 and injected into the IOC container is to read this data model into a total beanfactory, unified management.

We can do some things with this beanfactory, such as Getbean () or Containsbean () and so on.


The following is a simple list of some of the configuration content:

1 Factory mode
<id= "examples." Myfactorybean "  factory-method=" CreateInstance "/>

2 constructors

<BeanID= "Testbean"class= "examples." Examplebean ">    <Constructor-arg><refBean= "TESTARGS1"/></Construtor-arg>    <Constructor-arg><refBean= "TestArgs2"/></Construtor-arg></Bean>equivalent to class testbean{public Testbean (TESTARGS1,TESTARGS2);}

3 single-Case mode

<id= "Testbean"  class= "Test.testbean"  Singleton  = "false"> each request generates the same bean

4 <idef> is the same as <ref>, but idef checks for the existence of the bean at XML load time

Destroy-method destroy, usually when configuring the data source, JDBC, input input stream, need to destroy the object on close depends-on sequence instantiation Init-method Initialization call method
8 The Java class can also be manipulated by the specified interface:
Beannameaware let the class know its own bean ID name in XML beanfactoryaware get Beanfactoryinitializingbean Interface Execution initialization method Disposablebean Execute Destroy method

9 Property overrides, this is usually configured with some properties, but there are other files that modify this property when used.

For example, we configure information about the data source in the project through the spring configuration file, but there is an additional xxx.properties to rewrite the information. When you have this file, you can let the customer modify their connection information themselves without having to modify the cumbersome spring configuration file every time.




"Spring Development"--spring Core

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.