Use factorybean with spring skills

Source: Internet
Author: User

Spring has two types of beans: common bean and factory bean, namely factorybean. The factory bean is different from the common bean. The returned object is not an instance of the specified class, and the returned object is the object returned by the GetObject method of the factory bean. In the Spring framework, many factory beans are used in AOP-related functions and transaction processing. This article briefly analyzes the usage of factory beans.
First, let's review a common bean. A common bean directly returns an instance of the specified class (the value of the class attribute in the configuration). The following bean1 returns a string:

Next, let's look at a simple factory Bean:
Import java. util. date;
Import org. springframework. Beans. Factory. factorybean;
Public class factorybeandemo implements factorybean {
Private string name;
Public void setname (string name)
{
This. Name = Name;
}
Public object GetObject () throws exception {
If ("date". Equals (name) return new date ();
Else return new string ("this is a string! ");
}
Public class getobjecttype (){
Return "date". Equals (name )? Date. Class: String. Class;
}
Public Boolean issingleton (){
Return false;
}
}

The configuration of the factory bean is as follows:

Below is the testCode:
Resources res = new classpathresource ("springroad/deomo/chap4/demo-bean.xml ");
Beanfactory factory = new xmlbeanfactory (RES );
System. Out. println (factory. getbean ("myfactorybean"). getclass ());
System. Out. println (factory. getbean ("myfactorybean1"). getclass ());

Output result:
Class java. Lang. String
Class java. util. Date

We can see that we can get different types of object instances by using factorybean. This means that in AOP, by setting calss as proxyfactorybean, we can return the principles of different types of business objects. In practical applications, if factorybean can be used flexiblyProgramMany magic functions are added.

TIPS:
Factorybean also contains a trick, that is, what to do when we actually need to return the factorybean instance. That is, we need to get a factorybean through the container in the application. In this case, you need to add a special symbol "&" before the bean name.
For example, the following code:
System. Out. println (factory. getbean ("& myfactorybean"). getclass ());
System. Out. println (factory. getbean ("& myfactorybean1"). getclass ());
The output result will be:
Class springroad. deomo. chap4.factorybeandemo
Class springroad. deomo. chap4.factorybeandemo

Due to the limited level, spring fans are welcome to give me some advice. Please do not join us!

(Note: The author of this article, easyjf open-source team Daxia, reposted the author's statement !)

-- From http://www.54bk.com/user2/14463/archives/2006/2006112491223.html

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.