The Bean factory method registered with spring bean

Source: Internet
Author: User

In general, we register a bean in spring and directly

<id= "WebSocket"  class= " Com.sdyy.common.spring.websocket.WebsocketEndPoint "/>

This creates an object bean for us, using the Websocketendpoint construction method (which is no parameter).

———————————————————————————— Dividing line —————————————————————————————————————————————

But there is another way to register beans with the Bean factory, there are three kinds of writing, we first look at the first kind:

The first method of writing and example parsing:

Take the famous Ehcahce, which is written in spring by the way it is registered in spring:

<id= "Realcache"  class= " Org.springframework.cache.ehcache.EhCacheManagerFactoryBean ">        <  name = "Configlocation" value = "Classpath:cache/ehcache.xml" />    </ Bean >

But in the end you will find that the last bean generated is a net.sf.ehcache. The object of the CacheManager class, not the Org.springframework.cache.ehcache. Ehcachemanagerfactorybean

This is because the Ehcachemanagerfactorybean implements the spring Factorybean interface

Factorybean has three methods that require the implementation of

where The result returned by the GetObject method is returned to spring as a true registered result Bean, let's take a lookat

Org.springframework.cache.ehcache. What is returned by the Ehcachemanagerfactorybean GetObject method:

Yes, that's Net.sf.ehcache.CacheManager.

The second to third type of notation:

Through the factory bean can also write:

  syntax: 
< bean id =" Factorybean " class = "Com.demo.BaseBeanFactory" ></ bean > < bean id = "Bean" Factory-bean = "Factorybean" Factory-method = "Createbasedao" ></ bean >

Syntax two:
<bean id= "Bean" class= "com.demo.BaseBeanFactory" factory-method= "Createbasedao" ></bean>
Factory-method refers to which method is used to create the bean, and there is no requirement to inherit Factorybean.



Complete!

The Bean factory method registered with 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.