首页 新闻 会员 周边

spring 通过3种不同的方式 得到了3个bean 为什么无论怎么都只能输出一个值

0
[已关闭问题] 关闭于 2016-11-29 11:38
public class TestHelloWorld {
public static void main(String[] args) throws FileNotFoundException {
    
    //bean的使用 3种方式 
    //第一种    使用BeanWrapper
    HelloWorld  hello=new HelloWorld();
    BeanWrapper bw=new BeanWrapperImpl(hello);
    bw.setPropertyValue("msg","HelloWorld");
    System.out.println(bw.getPropertyValue("msg"));
    //第二种    使用BeanFactory
        FileSystemResource is=new FileSystemResource("D:/work/NewWork/spring/src/config.xml");
    XmlBeanFactory factory=new XmlBeanFactory(is);
    HelloWorld helloWorld=(HelloWorld)factory.getBean("HelloWorld");    
    System.out.println(helloWorld.getMsg());
    //第三种     使用ApplicationContext      
    //通过ApplicationContext 来获取Spring的配置文件
    ApplicationContext actx=new FileSystemXmlApplicationContext("D:/work/NewWork/spring/src/config.xml");
    //通过Bean 的id来获取Bean
    HelloWorld HelloWorld=(HelloWorld) actx.getBean("HelloWorld");
    System.out.println(HelloWorld.getMsg());
}
}

这里我有3个System.out  可是 输出结果为一个 HelloWorld

然后 随便注释掉哪两个  剩下的那一个都能成功输出HelloWorld

一开始我以为 是因为 默认的singtelon    然后 把singtelon 关掉 

还是不行。 希望有大神指教一下。

下面是我的 config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://WWW.springfamework.org/dtd/spring-beans.dtd">
<beans>
    <!--定义一个Bean-->
    <bean id="HelloWorld" class="com.gc.action.HelloWorld" singleton="true">
        <!--将其变量msg 通过依赖注入-->
        <property name="msg">
            <value>HelloWorld</value>
        </property>
        <property name="date">
            <ref bean="date" />
        </property>
    </bean>
    
    <bean id="date" class="java.util.Date" />

</beans>
情不知所起一往而深的主页 情不知所起一往而深 | 初学一级 | 园豆:87
提问于:2016-11-29 11:37
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册