Spring Note--1.spring Introduction

Source: Internet
Author: User

Spring is a lightweight architecture that can help us better manage the dependencies between Java classes (beans) and greatly increase the rate of enterprise application development. We can add spring capacities tools to the project using MyEclipse's own add spring

Function. The User library needs to have all the jars under the spring Lib package, and a jar package named common-logging (additional downloads, Web projects required).


Using Spring to manage beans

We can use the Applicationcontext.xml file to manage the relationship between beans better and without immersion. Spring refers to all objects in the container as beans.

<bean id= "Chinese" class= "Com.cm.Chinese" ><property name= "Axe" ref= "steelaxe"/></bean><bean ID = "Steelaxe" class= "Com.cm.SteelAxe" ><property name= "name" value= "ALVINCCCC"/></bean>

This configuration allows the schema to understand the dependencies between the individual beans, and after the creation of a bean, all the other required beans are created. The schema creates a Java object based on the De class name in the bean element by reflection and puts it into the srping container. Schema whenever a property element is seen, the set method is executed at the bottom, and if value is set, if it is ref, the bean in the container is set, so! When using ref, note that the order must be in the container!

Access Bean use ApplicationContext This interface, common implementation classes have Classpathxmlapplicationcontext and Filesystemxmlapplicationcontext, the first commonly used.

ApplicationContext ctx=newclasspathxmlapplicationcontext ("Beans.xml"); Chinese C=ctx.getbean ("Chinese", person.class);

This will give you an example!


Spring's Dependency Injection

The value of a member variable is an object, not an active setting, but a schema auto-complete. If it is a value, the direct automatic set. The injection method is divided into set value injection and structure injection. The difference is whether a set or a constructor is used. PS: We recommend using interface-oriented programming here.

Because the coupling in XML is all interface-level, we can arbitrarily change the implementation class and reconfigure the XML without having to modify the code. For example:

<bean id= "Person" class= "Com.cm.Person" ><property name= "Axe" ref= "Stoneaxe"/></bean><bean id= " Stoneaxe "class=" Com.cm.StoneAxe "/>

We used the Stoneaxe method in the person implementation class to output a "stone axe". When we don't want a stone axe, we just need to modify it as follows:

<bean id= "Person" class= "Com.cm.Person" ><property name= "Axe" ref= "Steelaxe"/></bean><bean id= " Steelaxe "class=" Com.cm.SteelAxe "/>

In this example, the benefits of interface-oriented programming and interface-level loose coupling are evident! Of course, the member variable in person should be the interface name, not the specific implementation class name.

Construct injection refers to the way a dependency is set through the constructor. Because the bean element default spring uses the parameterless constructor to create the object, we need to use the Constructor-arg child element to drive spring to use the parameterized constructor to create the object. In this way, the instance is built, and the dependency is done (and the setting is, first an instance, then set). In this way, the member variables of the object type do not need a set method, but are specified in the construct. XML, use Construcotr-arg ref under the bean to pass in the arguments in the constructor, paying attention to the number of orders. It also has a TPYE element for specifying the parameter type.

Two ways to compare: they can exist at the same time, not absolutely good or bad. The settings are easier to understand and intuitive to handle when complex dependencies and member variables can be variable. Constructs, you can determine the injection order of dependencies in the constructor, and prioritize the injection.

This article from "Fingertip Light Fly" blog, declined reprint!

Spring Note--1.spring Introduction

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.