public
interface
FactoryBean {
Object getObject()
throws
Exception;
Class getObjectType();
boolean
isSingleton();
}
|
public class
FengShan1 {
public void descrip(){
System.out.println("
鳳山是個美麗的小縣城!
");
}
}
|
public class
TianE implements FactoryBean {
public Object getObject() throws Exception {
return new FengShan1();
}
public Class getObjectType() {
return FengShan1.class;
}
public boolean isSingleton() {
return true;
}
}
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
beans
PUBLIC
"-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd"
>
<
beans
default-autowire
=
"byName"
>
<!—FactoryBean
配置
-->
<
bean
id
=
"tiane"
class
=
"com.dvnchina.test.TianE"
/>
</
beans
>
|
public class
Test extends TestCase {
public void test(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
FengShan1 fengShan1 = (FengShan1) ctx.getBean("tiane");
fengShan1.descrip();
FengShan1 fengShan2 = (FengShan1) ctx.getBean("tiane");
System.out.println(fengShan1==fengShan2);
}
}
|
鳳山是個美麗的小縣城!
true
|