您的位置:首页 > 编程语言 > Java开发

Spring Bean Xml映射方式

2009-08-12 11:12 155 查看
Spring Bean和普通的javaBean没有什么区别,Spring 是个容器,只是为了通过bean.xml来管理(或者说映射)javaBean。

1、定义Spring Bean

 1 public class HelloBean {

 2     private String helloworld;

 3 

 4     public String getHelloworld() {

 5         return helloworld;

 6     }

 7 

 8     public void setHelloworld(String helloworld) {

 9         this.helloworld = helloworld;

     }

 

     public void sayHello() {

         System.out.println(helloworld);

     }

 }
2、定义 beans.xml

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 

 3     "http://www.springframework.org/dtd/spring-beans.dtd">

 4 

 5 <beans>

 6     <bean id="helloBean" class="com.spring.ioc.demo.HelloBean">

 7         <property name="helloworld">

 8             <value>Hello!World!</value>

 9         </property>

     </bean>

 </beans>
3、Bean 调用
Bean 调用我们最常用的是 new 对象,而Spring 则是通过 xml 来映射(反射、亦即Ioc)实现 Bean 的调用的,并且Spring 有多种映射的方法,如下:

 1 import org.springframework.beans.factory.BeanFactory;

 2 import org.springframework.beans.factory.xml.XmlBeanFactory;

 3 import org.springframework.context.ApplicationContext;

 4 import org.springframework.context.support.ClassPathXmlApplicationContext;

 5 import org.springframework.context.support.FileSystemXmlApplicationContext;

 6 import org.springframework.core.io.ClassPathResource;

 7 import org.springframework.core.io.FileSystemResource;

 8 import org.springframework.core.io.Resource;

 9 

 public class HelloBeanTest {

     public static void main(String[] args) {

         /**

          * 当普通 javaBean 使用

          * */

         HelloBean hellobean = new HelloBean();

         hellobean.setHelloworld("Hello World!");

         System.out.println("new对象直接调用");

         hellobean.sayHello();

 

         /**

          * 通过Spring访问JavaBean组件

          * 1、BeanFactory 方式

          * */

         //1.1用 ClassPathResource 时 beans.xml 可以从classpath中读取XML文件

         Resource resource = new ClassPathResource("beans.xml");

         BeanFactory factory = new XmlBeanFactory(resource);

         hellobean = (HelloBean) factory.getBean("helloBean");

         System.out.println("ClassPathResource + XmlBeanFactory 调用");

         hellobean.sayHello();

         

         //1.2用FileSystemResources 时 beans.xml 可以指定XML定义文件的路径来读取定义文件

         resource = new FileSystemResource("beans.xml");

         factory = new XmlBeanFactory(resource);

         hellobean = (HelloBean) factory.getBean("helloBean");

         System.out.println("FileSystemResource + XmlBeanFactory 调用");

         hellobean.sayHello();

         

         /**

          * 2、ApplicationContext 方式

          * 采用BeanFactory对简单的应用程序来说就够了,可以获得对象管理上的便利性。

          * 但是要获取一些特色和高级的容器功能,可以采用ApplicationContext。

          * ApplicationContext提供了一些高级的功能,比如:

          * 2.提供文字消息解析的方法

          * 3.支持国际化(i18n)消息

          * 4.可以发布事件,对事件感兴趣的Bean可以接收这些事件

          * 

          * Spring创始者建议采用ApplicationContext取代BeanFactory。

          * */

         //2.1 FileSystemXmlApplicationContext 可以指定XML定义文件的路径来读取定义文件

         ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");        

         hellobean = (HelloBean)context.getBean("helloBean");

         System.out.println("FileSystemXmlApplicationContext 调用");

         hellobean.sayHello();

         //2.2 ClassPathXmlApplicationContext 可以从classpath中读取XML文件

         context = new ClassPathXmlApplicationContext("beans.xml");        

         hellobean = (HelloBean)context.getBean("helloBean");

         System.out.println("ClassPathXmlApplicationContext 调用");

         hellobean.sayHello();

         //2.3 XmlWebApplicationContext 从Web应用程序的文件架构中,指定相对位置来读取定义文件

         //略

     }

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐