Spring Boot Combat Notes (iii)--Spring common configuration (bean initialization and destruction, profile)

Source: Internet
Author: User

First, the bean initialization and destruction

In our actual development, we often encounter beans before or after the use of the necessary operations, spring on the Bean's life-cycle operations to provide support. There are two ways to use Java Configuration and annotation configurations:

(1) How Java is configured: Use the Initmethod and Destroymethod of the @Bean (equivalent to Init-method and Destroy-method in the XML configuration).

(2) Annotation method: Use JSR-250 's @PostContruct and @PreDestroy.

Demonstrate:

1. Increase JSR-250 support.

<!--JSR-250 Support--    <dependency>      <groupId>javax.annotation</groupId>      < artifactid>jsr250-api</artifactid>      <version>1.0</version>    </dependency>

2. Use a Bean in @Bean form.

Package Com.ecworking.bean;public class Beanwayservice {public    void init () {        System.out.println ( "@Bean-init-method");    }    Public Beanwayservice () {        Super(); System.out.println ("Initialize constructor-beanwayservice"),} private void destroy () {System.out.println ("@ Bean-destroy-method "); }}

3. Use the JSR250 form of the bean.

Package com.ecworking.bean;import javax.annotation.postconstruct;import Javax.annotation.PreDestroy; public class Jsr250wayservice {    @PostConstruct//Execute public    void init () after the constructor has finished executing {        System.out.println ("@JSR250-init-method");} public Jsr250wayservice () {Super(); System.out.println ("Initialize constructor-jsr250wayservice"); @PreDestroy//execute private void Destroy () before the Bean is destroyed () { System.out.println ("@JSR250-destroy-method");}}          

4. Configure the class.

Package Com.ecworking.bean;import org.springframework.context.annotation.bean;import  Org.springframework.context.annotation.componentscan;import  Org.springframework.context.annotation.Configuration, @Configuration @componentscan ("Com.ecworking.bean") The public class Prepostconfig {//Initmethod and Destroymethod specifies that the init and destroy methods of the Beanwayservice class are executed after construction and before the Bean is destroyed @Bean ( Initmethod = "Init", Destroymethod = "Destroy") Beanwayservice Beanwayservice () {return new beanwayservice ();} @B EAN Jsr250wayservice Jsr250wayservice () {return new jsr250wayservice ();}}        

5. Run.

Package Com.ecworking.bean;import  Org.springframework.context.annotation.annotationconfigapplicationcontext;public class Main {    public static void main (string[] args) {        Annotationconfigapplicationcontext context = new Annotationconfigapplicationcontext (Prepostconfig.class);        Beanwayservice Beanwayservice = Context.getbean (Beanwayservice.class); Jsr250wayservice Jsr250wayservice = Context.getbean (Jsr250wayservice.class); Context.close ();}}    

Operation Result:

Second, profile

Prifile provides support for different configurations in different environments (the configuration of the development environment and the production environment must be different, for example, the configuration of the database).

1. Set the configuration environment that the current context needs to use by setting the environment Activeprofile. Use the @profile annotation class or method in the development environment to choose to instantiate different beans in different environments.

2. Set the configuration environment by setting the JVM's spring.profile.active parameter.

The 3.WEB project is set in the context parameter of the servlet.

Servlet2.5 and below:

<servlet>    <servlet-name>dispatcher</servlet-name>    <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>      <init-param>        < Param-name>spring.profiles.active</param-name>        <param-value>production</param-value>    </init-param></servlet>

Servlet3.0 and above

Spring Boot Combat Notes (iii)--Spring common configuration (bean initialization and destruction, profile)

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.