Spring对Bean的管理

(1)用BeanWrapper管理Bean 示例:

                  Class object=Class.forName("com.wl.Helloworld").newInstance();

                        BeanWrapper bw=new BeanWrapperImpl(object);

                       bw.setPropertyValue("msg","Helloworld");

                        bw.setPropertyValue("date",new Date());

(2)用BeanFactory管理Bean 实际上BeanFactory是实例化、配置和管理众多Bean的“容器”

                 BeanFactory是一个接口,有多个实现,但是最常用的是XmlBeanFactory

          示例:             ClassPathResource res=new ClassPathResource("config.xml");

                          XmlBeanFactory factory=new XmlBeanFactory(res);

                           HelloWorld helloworld=(HelloWorld)facotry.getBean("HelloWorld");

(3)使用ApplictionContext管理Bean ApplicationContext是建立在BeanFactory基础之上的,

          增加了其它的功能:国际化、资源访问、事件传递等。

           示例:ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

                      HelloWorld helloworld=actx.getBean("HelloWorld");

------------------------------------------------------------------------------------------------------------------------------------

  ApplictionContext的国际化支持

示例:   在config.xml文件中定义如下的Bean  

         <bean id="messageSource"<!--名字必须为这个,不可以改-->            class="org.springframework.context.support.ResourceBundleMessageSource">

            <property name="basename">
<!--国际化支持的文件定义在名为messages的文件中-->message.properties或者是message.class
              <value>messages</value>
            </property>
        </bean>
<!--国际化支持的文件定义在名为messages的文件中-->

            message.properties中 HelloWorld=问候语:{0} 时间{1}

       测试代码: ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

                         Object[] objs=new Object{"helloworld",Calendar.getInstance().getTime()};

                          String msg=actx.getMessage("HelloWorld",objs,Locale.CHINA);

  <!--可能会出现问题,尽管指定了Locale.CHINA。此时可以用命令进行转换:native2ascii messages.properties messages.txt     然后把messages.txt中的内容复制到原来messages.properties文件中,再次输出结果就会正常显示中文-->

<!--可能会出现问题,尽管指定了Locale.CHINA。此时可以用命令进行转换:native2ascii messages.properties messages.txt 然后把messages.txt中的内容复制到原来messages.properties文件中,再次输出结果就会正常显示中文-->

ApplictionContext的资源访问

 ApplicationContext接口继承了ResourceLoader接口,开发人员可以使用getResourece()来指定资源文件的URL来存取

ApplicationContext对资源文件的存取在设定“资源文件的路径”上有3中方式:

        (1)通过虚拟路径来存取,如果资源文件位于CLASSPATH下,

              示例:ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

                        Resource  resource=actx.getResource("classpath: messages.properties");

                       注意:这里的classpath是spring约定的URL虚拟路径

         (2)通过实际路径来存取,指定标准的URL 。如:file:或者http:

               示例:ApplicationContext actx=new FileSystemXmlApplicationContext("conf.xml");

                  Resource resource=actx.getResource("file:d:/eclipse/workspace/me.properties");

           (3)通过相对路径来存取        

                      ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

                       Resource resource=actx.getResource("WEB-INF/src/messages.properties"):

     取得一个Resource后,便可用getFile()来存取文件,exist()判断是否存在等等。

 ------------------------------------------------------------------------------------------------------------------------------------

ApplictionContext的事件传递

ApplicationContext中的事件处理是通过ApplicationEventApplicationListener接口来提供的,通过ApplicationEventpublisEvent来通知ApplicationListener

        示例: 一  定义一个LogEvent  extends ApplicationEvent ,然后这个类被ApplicationContext发布出去

                    二    LogListener  implements ApplicationListener    那么ApplicationContext会在发布LogEvent事件时,通知LogListener

                     三     Log implements ApplicationContextAware通过publisEvent(),带入LogEvent,来通知LogListener

                 

具体代码在spring从入门到精通3的第10页  附件中有相应的代码截图

猜你喜欢

转载自wang-liang.iteye.com/blog/1995206