Use of Spring @Bean annotations

Source: Internet
Author: User

@Bean is a method-level annotation that is used primarily in @configuration annotated classes, or in @component annotated classes. The ID of the added bean is the method name

Defining beans

Here's an example from @configuration.

@Configuration  Public class AppConfig {    @Bean    public  transferservice transferservice () {        return  New  Transferserviceimpl ();    }}

This configuration is equivalent to the previous configuration in XML

< Beans >    <  ID= "Transferservice"  class= "Com.acme.TransferServiceImpl"/ ></beans>
Bean dependency

@bean can also rely on any number of other beans, and if transferservice relies on accountrepository, we can implement this dependency through the method parameters

@Configuration  Public class AppConfig {    @Bean    public  transferservice transferservice (accountrepository Accountrepository) {        returnnew  Transferserviceimpl (accountrepository);    }}
Accept the callback of the life cycle

Any bean defined using @bean can also perform a life-cycle callback function, similar to the @postconstruct and @PreDestroy method. Use the following

 Public classFoo { Public voidinit () {//initialization Logic    }} Public classBar { Public voidCleanup () {//Destruction Logic}} @Configuration Public classAppConfig {@Bean (Initmethod= "Init")     PublicFoo foo () {return NewFoo (); } @Bean (Destroymethod= "Cleanup")     PublicBar Bar () {return NewBar (); }}

By default the bean configured with Javaconfig, if there is a close or shutdown method, the method will be executed automatically when the bean is destroyed, and if you do not want to execute the method, add @bean (destroymethod= "") to prevent the departure destruction method

Specify the scope of the bean using @scope annotations

You can use @scope annotations to specify beans that are defined with @bean

@Configuration  Public class myconfiguration {    @Bean    @Scope ("prototype")    public  encryptor encryptor () {        // ...     }}
@Scope and Scoped-proxy

Spring provides a proxy for scope, you can set the @scope property Proxymode to specify, the default is scopedproxymode.no, You can specify that the default is Scopedproxymode.interfaces or the default is Scopedproxymode.target_class.
Here is a demo, it seems to be used (not understand this piece)

// An HTTP session-scoped Bean exposed as a proxy @Bean @sessionscope  Public userpreferences userpreferences () {    returnnew  userpreferences ();} @Bean public Service UserService () {    new  simpleuserservice ();     // a reference to the proxied userpreferences Bean     service.setuserpreferences (Userpreferences ());     return Service;}
Naming a custom Bean

By default, the Bean name and method name are the same, and you can use the Name property to specify

@Configuration  Public class AppConfig {    = "Myfoo")    public  foo foo () {        returnNew  Foo ();    }}
The Bean Alias

The name of the bean supports aliases, using the following methods

@Configuration  Public class AppConfig {    = {"DataSource", "Subsystema-datasource", "Subsystemb-datasource" })      public DataSource DataSource () {        //  Instantiate, configure and return DataSource beans ...     }}
Description of the Bean

Sometimes it is also useful to provide details about the bean, which can be used @Description to provide

@Configuration  Public class AppConfig {    @Bean    @Description ("provides a basic example of a Bean")    public Foo foo () {        returnnew  Foo ();    }}

Loaded from: https://www.cnblogs.com/feiyu127/p/7700090.html

Use of Spring @Bean annotations

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.