Go Spring Annotations-@Configuration annotations, @Bean annotations, and configuring automatic scanning, bean scopes

Source: Internet
Author: User

1, @Configuration marked on the class, equivalent to the class as a spring XML configuration file, the <beans> role of: Configure the Spring container (application context)

Package Com.test.spring.support.configuration, @Configurationpublic class Testconfiguration {public    Testconfiguration () {        System.out.println ("Spring Container start initialization ... ");    }}

Equivalent:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc" xmlns:jee= "Http://www.springframework.org/schema/jee" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:util= "Http://www.springframework.org/schema/util" xmlns:task= "Http://www.springframework.org/schema/task" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/ Schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/JDBC http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd/HTTP Www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd/http Www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd/HTTP Www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd " Default-lazy-init= "false" ></beans>

The Main method is tested:

Package Com.test.spring.support.configuration;public class Testmain {public    static void Main (string[] args) {        @Configuration Annotated spring container load mode, replace Classpathxmlapplicationcontext        with Annotationconfigapplicationcontext ApplicationContext context = new Annotationconfigapplicationcontext (testconfiguration.class);        If the Spring-context.xml file is loaded:        //applicationcontext context = new Classpathxmlapplicationcontext (" Spring-context.xml ");}    }

As you can see from the results of running the main method, the spring container has started:

August11, :11 pm org. springframework. Contextannotation. Annotationconfigapplicationcontext preparerefresh Info: refreshing org. springframework. Context. Annotation. Annotationconfigapplicationcontext@203e25d3:startup Date [Thu : Each CST 2016] ; root of context hierarchyspring container start initialization ... 


2. The @Bean is labeled on the method (the method that returns an instance), equivalent to the spring XML configuration file <bean> , which functions as: registering the Bean object

Bean class:

Package Com.test.spring.support.configuration;public class Testbean {public    void SayHello () {        System.out.println ("Testbean sayHello ...");    Public String toString () {        return "username:" +this.username+ ", url:" +this.url+ ", Password:" +this.password;    }    public void Start () {        System.out.println ("Testbean initialization ... ");    }    public void CleanUp () {        System.out.println ("Testbean destroyed ... ");    }}

Configuration class:

Package Com.test.spring.support.configuration, @Configurationpublic class Testconfiguration {public        Testconfiguration () {            System.out.println ("Spring Container start initialization ... ");        }    @Bean Note Register Bean, you can specify initialization and destruction method    //@Bean (name= "Testnean", initmethod= "Start", destroymethod= "cleanUp")    @ Bean    @Scope ("prototype") public    Testbean Testbean () {        return new Testbean ();}    }

Main method Test class:

Package Com.test.spring.support.configuration;public class Testmain {public    static void Main (string[] args) {        ApplicationContext context = new Annotationconfigapplicationcontext (testconfiguration.class);        Get Bean        Testbean TB = Context.getbean ("Testbean");        Tb.sayhello ();    }}

Note:
(1), @Bean annotations on the method that returns the instance, if the name of the bean is not specified by @bean, the default is the same as the method name of the callout;

(2), @Bean annotations The default scope is a singleton singleton scope, which can be set as a prototype scope by @scope ("prototype");

(3), since the role of @bean is to register the bean object, you can fully use @component, @Controller, @Service, @Ripository and other annotations to register the bean, Of course, you need to configure @componentscan annotations for automatic scanning.

Bean class:

Package com.test.spring.support.configuration;//Add registered Bean annotations @componentpublic class Testbean {public    void SayHello () {        System.out.println ("Testbean sayHello ...");    }    Public String toString () {        return "username:" +this.username+ ", url:" +this.url+ ", Password:" +this.password;    }}

Configuration class:

    • @Configuration//Add automatic scan annotations, basepackages for Testbean package path @componentscan (basepackages = " Com.test.spring.support.configuration ") public class Testconfiguration {public    testconfiguration () {        System.out.println ("Spring Container start initialization ... ");    }    How to cancel @bean annotation registration Bean/    /@Bean    //@Scope ("prototype")    //public Testbean Testbean () {    //  return New Testbean ();    //}}

The Main method test gets the Bean object:

public class Testmain {public    static void Main (string[] args) {        ApplicationContext context = new annotationconf Igapplicationcontext (testconfiguration.class);        Get Bean        Testbean TB = Context.getbean ("Testbean");        Tb.sayhello ();    }}

The SayHello () method is called normally.

(Original address: http://www.cnblogs.com/ilinuxer/p/6503161.html)

Go Spring annotations [email protected] annotations, @Bean annotations, and configuring automatic scanning, bean scopes

Related Article

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.