Three ways to create a Spring bean

Source: Internet
Author: User
Tags stub

1. Create Spring Project

One simple way to create a spring project with Eclipse is to create a Java project and then put the Spring jar package into the project to start writing a spring-based program. Spring's jar package can be obtained from the source code on GitHub:https://github.com/spring-projects/spring-framework.

Here is a spring project that was created when learning Spring combat, with the following directory structure:


In spring, the behavior of creating collaborative relationships between application objects is often referred to as assembly (wiring), which is also the essence of dependency injection, while dependency injection and AOP are at the heart of the spring framework. The following is the assembly of the beans in spring, starting with Spring3.0, the spring container provides two types of bean assembly: XML files and Java annotations, first from the XML file configuration.

2. Creating beans

The XML configuration file for spring is as follows:


Note:

The XML configuration file in spring uses the same network path as the XSD file path, but you can also use the local class library path, because spring-beans-3 in the introduced spring package. *. There are spring-beans-3.0.xsd files in the Release.jar.

The parameter order of the constructors in the bean configuration file must be in accordance with the order of the parameters in the class definition by default, or you can use the index (starting at 0) property to specify the order of the parameters. For non-basic types, you need to use the ref attribute to reference other beans.

There are three main ways to create a bean:

http://my.oschina.net/itblog/blog/205172

1. Constructor Injection Creation

You can use constructor injection to create a class that has a public constructor method, whether it is a default or a constructor that displays a declaration, as a bean

 PackageLucien PublicClass Poeticjugglerextendsjuggler {PrivatePoem Poem; PublicPoeticjuggler (Poem Poem) { This. poem = poem;} PublicPoeticjuggler (intbeanbags, Poem Poem) {Super(beanbags); This. poem = poem;System. OUT.PRINTLN ("constructor injection create Poeticjuggler");} Publicvoid Perform () {Super. perform ();System. OUT.PRINTLN ("Reciting ...");p oem.recite ();}}

2. Factory Method Creation

For classes that do not have a public constructor method, you can use one of its static methods (whether the method is private or publicly, as long as it is static) to create the bean.

 PackageLucien PublicClass Stage {//Private method of constructionPrivateStage () {}//Static internal classPrivate StaticClass Stagesingletonholder {StaticStage instance =NewStage ();}//static method Public StaticStage getinstance () {System. OUT.PRINTLN ("Factory method Creation Stage");returnStagesingletonholder.instance;} Publicvoid Perform () {System. OUT.PRINTLN ("Stage Performance");}}

3. Factory class Creation

Use non-static methods of the factory class (private and public, but must be nonstatic)

 PackageLucien PublicClass JugglerImplementsPerformer {Private intbeanbags = 3; PublicJuggler () {} PublicJuggler (intbeanbags) { This. beanbags = beanbags;} @Override Publicvoid Perform () {//TODO auto-generated method stubSystem. OUT.PRINTLN ("Juggling"+ beanbags +"Beanbags");}}
package  Lucien; public  class jugglerfactory {public  Juggler Getjuggler () {System . Out.println ( " Factory class Create Juggler "); return  new  juggler ();}} 


 PackageLucienImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; PublicClass Test {/** * @param args */ Public Staticvoid Main (String[] args) {//TODO auto-generated method stubApplicationContext CTX =NewClasspathxmlapplicationcontext ("Configfile.xml"); Performer performer = (performer) Ctx.getbean ("Poeticduke");p Erformer.perform (); Stage stage = (stage) Ctx.getbean ("Stage"); Stage.perform (); Juggler juggler = (juggler) Ctx.getbean ("Duke2"); Juggler.perform ();}}

Three ways to create a Spring 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.