相關資訊
本類常用軟件
-
福建農(nóng)村信用社手機銀行客戶端下載下載量:584204
-
Windows優(yōu)化大師下載量:416898
-
90美女秀(視頻聊天軟件)下載量:366961
-
廣西農(nóng)村信用社手機銀行客戶端下載下載量:365699
-
快播手機版下載量:325855
- public class SimpleFactoryBean implements FactoryBean {
- private boolean flag;
- public Object getObject() throws Exception {
- if (flag) {
- return new Date();
- }
- return new String("false");
- }
- @SuppressWarnings("unchecked")
- public Class getObjectType() {
- return flag ? Date.class : String.class;
- }
- public boolean isSingleton() {
- return false;
- }
- public void setFlag(boolean flag) {
- this.flag = flag;
- }
- }
- <bean id="factoryBeanOne" class="com.study.demo.factorybean.SimpleFactoryBean" >
- <property name="flag">
- <value>true</value>
- </property>
- </bean>
- <bean id="factoryBeanTwo" class="com.study.demo.factorybean.SimpleFactoryBean" >
- <property name="flag">
- <value>false</value>
- </property>
- </bean>
- public class MainTest {
- public static void main(String[] args) {
- Resource res = new ClassPathResource("bean.xml");
- BeanFactory factory = new XmlBeanFactory(res);
- System.out.println(factory.getBean("factoryBeanOne").getClass());
- System.out.println(factory.getBean("factoryBeanTwo").getClass());
- }
- }
通過簡單的測試可知,該類輸出如下:
class java.util.Date
class java.lang.String
也就是說,容器通過getBean方法返回的不是FactoryBean本身,而是FactoryBean實現(xiàn)類中getObject()方法所返回的對象。
熱門評論
最新評論