<spring combat >2: Assembling Beans

Source: Internet
Author: User

1 declaring beans

1.1 Creating a Spring configuration

The Spring container provides two ways to configure beans: XML configuration and annotation-based configuration.

Spring configuration file:

<? XML version= "1.0" encoding= "UTF-8" ?> <  xmlns= "Http://www.springframework.org/schema/beans"       xmlns:xsi= " Http://www.w3.org/2001/XMLSchema-instance "       xsi:schemalocation="/http/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "  ></beans>

The Spring framework comes with 10 namespace configurations:

    • AOP: Provides configuration elements for life facets and class proxies that @aspectj annotations to spring facets
    • Beans: Support for declaring beans and assembly beans, which is the most central and primitive namespace of spring
    • Context: Provides configuration elements for configuring the spring application context, including automatic detection and automatic assembly of beans, injection of objects not directly managed by spring
    • JEE: Provides integration of the Java EE API
    • JMS: Provides configuration elements for declaring message-driven Pojo
    • Lang: Support for configuring beans implemented by scripts such as groovy, JRuby, or BeanShell
    • MVC: The ability to enable spring MVC, such as annotation-oriented controllers, view controllers, and interceptors
    • OXM: Supports spring Object-to-XML mapping configuration
    • TX: Provides declarative things configuration
    • Util: Provides a variety of tool class elements, including configuring collections as beans, supporting attribute placeholder elements

1.2 Declaring a simple Bean

 PackageCom.hundsun.idol.impl;ImportCom.hundsun.idol.Performer;//a circus artist . Public classJugglerImplementsPerformer {Private intbeanbags = 3;  Publicjuggler () {} PublicJuggler (intbeanbags) {         This. beanbags =beanbags; } @Override Public voidperform () {System.out.println ("Juggler" + beanbags + "beanbags"); }}

To declare in the Spring configuration file:

<id= "Duke"  class= "Com.hundsun.idol.impl.Juggler"/> 

The <bean> element creates a bean named Duke that is managed by the Spring container. Load the Spring context:

New Classpathxmlapplicationcontext ("Com/hundsun/idol/idol.xml"= (performer) Context.getbean ("Duke"); Duke.perform ();

1.3 Through the constructor

To declare a Bean using the parameter constructor:

<id= "Duke"  class= "Com.hundsun.idol.impl.Juggler">     <value= "/>"</Bean  >

Use <constructor-arg> to invoke the Juggler constructor. You can also configure a reference to inject an object through the constructor:

 PackageCom.hundsun.idol.impl;ImportCom.hundsun.idol.Poem;//an acrobat who will recite Public classPoeticjugglerextendsjuggler {//Recitation of poetry    PrivatePoem Poem;  PublicPoeticjuggler (Poem Poem) {Super();  This. Poem =poem; }     PublicPoeticjuggler (intbeanbags, Poem Poem) {        Super(beanbags);  This. Poem =poem; } @Override Public voidperform () {Super. Perform (); System.out.println ("While reciting ...");    Poem.recite (); }}

First declare a Poem object as a Bean:

<id= "Apoem"  class= "Com.hundsun.idol.impl.APoem"/> 

Use the <constructor-arg> ref property to pass a Bean reference with ID Apoem to the constructor:

 <  bean  id  = "Poeticduke"   class  = "Com.hundsun.idol.impl.PoeticJuggler"     >  <  constructor-arg  value  = ""    />  <  constructor-arg  ref  = "Apoem"  />  </ bean  >  

Sometimes the static factory method is the only way to instantiate an object, and Spring supports assembling a factory-created bean with the Factory-method property of the <bean> element.

 PackageCom.hundsun.idol.impl;//stage Singleton class Public classStage {PrivateStage () {}Private Static classStagesingletonholder {StaticStage instance =NewStage (); }     Public StaticStage getinstance () {returnstagesingletonholder.instance; }}

The Stage does not have a public construction method, and the static method getinstance () returns the same instance each time it is called. In the configuration file, you can use Factory-method to configure:

<id= "Thestage"  class= "Com.hundsun.idol.impl.Stage " Factory-method= "getinstance"/>

Scope of the 1.4 Bean

All Spring beans are singleton by default, and when a container allocates a bean, it always returns the same instance of the bean. The scope property can configure the scope of the Bean:

    • 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 one HTTP request, each Bean definition corresponds to an instance (Web-based Spring context only)
    • Session: In an HTTP session, each Bean definition corresponds to an instance (Web-based Spring context only)
    • Global-session: In a global HTTP session, each Bean definition corresponds to an instance (valid only in the context of the Portlet)

1.5 initialization and destruction of beans

<bean> Init-method and Destroy-method can configure the method to be called before initialization and when the container is removed from the object. If many beans defined in the context have initialization and destruction methods of the same name, the Default-init-method and Default-destroy-method properties of the <beans> element can be used.

2 Inject Bean Properties

<spring combat >2: Assembling Beans

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.