Spring Elementary Knowledge (i) Assembly bean

Source: Internet
Author: User

Spring's Dependency Injection is the basis for learning spring. The IOC is a control reversal, meaning that when needed, it is generated by spring, not by the re-use of the man.

Write in front

Spring provides interface-oriented programming, interface-oriented programming and dependency injection collaboration for loose coupling.

Spring.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring- Aop-3.0.xsd http://www.springframework.org/schema/tx/spring-tx-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SC Hema/context ">       <!--Beans Statement -</Beans>

Get beans from Spring

 Packagesingle.spring;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classTestspringxml { Public Static voidMain (string[] args)throwsperformanceexception {//assembling the Spring.xml configuration fileApplicationContext CTX =NewClasspathxmlapplicationcontext ("Spring.xml"); //The bean that gets the claim from the spring containerPerformer performer = (performer) Ctx.getbean ("Duke"); //methods for executing beansPerformer.perform (); }}

first, the default constructor injection

Requires that this class have a default constructor

class= "Single.spring.Juggler" ></bean>

Second, through the constructor injection

1 with parameter constructors, parameters are basic data types

 Public Juggler (int  beanbags) {  this. beanbags = beanbags;}

Instantiating a bean using the pass-parameter constructor

 <  bean  id  = "Duke"   class  = "Single.spring.Juggler"     >  <!--     -->  <  constructor-arg  value  = "" ></ constructor-arg     >  </ bean  >  

2 with parameter constructor, parameter is reference type

Performer Poeticduke = (performer) Ctx.getbean ("Poeticduke");
Poeticduke.perform ();

<BeanID= "Poeticduke"class= "Single.spring.PoeticJuggler">        <Constructor-argvalue= " the"></Constructor-arg>        <!--parameter types are reference types with ref -        <Constructor-argref= "Sonnet29"></Constructor-arg>    </Bean>    <BeanID= "Sonnet29"class= "Single.spring.Sonnet29">    </Bean>

Iii. creating beans through factory methods

Sometimes the static factory method is the only way to instantiate an object, and for thread safety, getinstance () uses a technique known as "initialization on demand holder" to create an instance of a singleton class.

Initialization on Demand holder mode, this method uses an internal class to do lazy loading of objects, and in the initialization of this inner class, JLS (Java Language sepcification) guarantees the thread safety of this class (the Class initialization phase is guaranteed by the JLS-be serial), the greatest beauty of this notation is that it is completely using the mechanism of the Java Virtual Machine for synchronization assurance without a synchronized keyword.

 Packagesingle.spring; Public classStage {PrivateStage () {}//Lazy Loading Instances    Private Static classStagesingletonholder {StaticStage instance =NewStage (); }    //return Time Instance     Public StaticStage getinstance () {returnstagesingletonholder.instance; }}

Spring.xml configuration, Factory-method property configuration static method

<id= "Thestage"  class= "Single.spring.Stage " Factory-method= "getinstance"></bean>

Iv. Scope of the bean

All spring beans are singleton by default, but spring provides a scope property to each of the different instances

Singleton: In each spring container, a bean defines only one instance of the object (default)

Prototype: Allows the definition of a bean to be instantiated any time (each invocation creates an instance)

Request: In an HTTP request, each bean definition corresponds to an instance that is only valid in the context of the web-based spring, such as spring MVC.

Session: In an HTTP session, each bean definition corresponds to an instance. The scope is valid only in a Web-based spring context, such as spring MVC.

Global-session: In a global HTTP session, each bean definition corresponds to an instance. The scope is valid only in the context of the portlet.

V. Initialization and destruction of beans

To satisfy the need to initialize and destroy beans, Spring provides a hook method for the bean declaration cycle.

Init-method: Invoking method when providing bean initialization
Destroy-method: A method to invoke when a bean is destroyed
<id= "Auditorium"  class= "Single.spring.Auditorium " Init-method= "Turnonlights"  destroy-method= "Turnofflights"></ Bean >

Six, inject bean properties

1 Setter Method Injection

2

VII. Assembly Set

Spring Elementary Knowledge (i) Assembly bean

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.