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应用程序的文件架构中,指定相对位置来读取定义文件
//略
}
}
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应用程序的文件架构中,指定相对位置来读取定义文件
//略
}
}
相关文章推荐
- web.xml中配置spring的几种方式 以及 Spring获取Bean的几种方式
- spring中读取xml配置文件、获取bean的几种方式
- spring对于bean的javaconfig,xml,自动装配三种装配方式实现
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- Spring的Bean管理(XML配置文件方式)
- SpringXML方式配置bean的自动装配autowire
- 先码后看 spring配置bean的几种方式:xml直接配置、静态工厂、实例工厂、factory bean、注解 侵立删
- spring利用扫描方式对bean的处理(对任何版本如何获取xml配置信息的处理)
- 重温Spring之旅3——装配Bean(手工装配:使用xml的方式)
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
- spring中读取xml配置文件、获取bean的几种方式
- SSM框架项目搭建系列(四)— Spring之bean的XML注入方式
- 前几天用AnnotationSessionFactoryBean 注解方式代替*.hbm.xml时扫描映射实休文件后报错
- Spring(三):IoC容器装配Bean(xml配置方式和注解方式)
- spring中读取xml配置文件、获取bean的几种方式
- spring框架的学习(二)——Spring的bean管理(xml配置文件方式)
- LocalSessionFactoryBean加载Hibernate 映射文件(hbm.xml)的三种方式及区别
- spring利用扫描方式对bean的处理(对任何版本如何获取xml配置信息的处理)
- Spring学习(二十二) Bean配置的三种方式(XML、注解、Java类)介绍与对比
- Spring学习-06:IOC容器装配Bean(xml配置方式)详解