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;
10 }
11
12 public void sayHello() {
13 System.out.println(helloworld);
14 }
15 }
2、定义 beans.xml
2 private String helloworld;
3
4 public String getHelloworld() {
5 return helloworld;
6 }
7
8 public void setHelloworld(String helloworld) {
9 this .helloworld = helloworld;
10 }
11
12 public void sayHello() {
13 System.out.println(helloworld);
14 }
15 }
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 >
10 </ bean >
11 </ beans >
3、Bean 调用
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 >
10 </ bean >
11 </ beans >
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
10 public class HelloBeanTest {
11 public static void main(String[] args) {
12 /**
13 * 当普通 javaBean 使用
14 * */
15 HelloBean hellobean = new HelloBean();
16 hellobean.setHelloworld( " Hello World! " );
17 System.out.println( " new对象直接调用 " );
18 hellobean.sayHello();
19
20 /**
21 * 通过Spring访问JavaBean组件
22 * 1、BeanFactory 方式
23 * */
24 // 1.1用 ClassPathResource 时 beans.xml 可以从classpath中读取XML文件
25 Resource resource = new ClassPathResource( " beans.xml " );
26 BeanFactory factory = new XmlBeanFactory(resource);
27 hellobean = (HelloBean) factory.getBean( " helloBean " );
28 System.out.println( " ClassPathResource + XmlBeanFactory 调用 " );
29 hellobean.sayHello();
30
31 // 1.2用FileSystemResources 时 beans.xml 可以指定XML定义文件的路径来读取定义文件
32 resource = new FileSystemResource( " beans.xml " );
33 factory = new XmlBeanFactory(resource);
34 hellobean = (HelloBean) factory.getBean( " helloBean " );
35 System.out.println( " FileSystemResource + XmlBeanFactory 调用 " );
36 hellobean.sayHello();
37
38 /**
39 * 2、ApplicationContext 方式
40 * 采用BeanFactory对简单的应用程序来说就够了,可以获得对象管理上的便利性。
41 * 但是要获取一些特色和高级的容器功能,可以采用ApplicationContext。
42 * ApplicationContext提供了一些高级的功能,比如:
43 * 2.提供文字消息解析的方法
44 * 3.支持国际化(i18n)消息
45 * 4.可以发布事件,对事件感兴趣的Bean可以接收这些事件
46 *
47 * Spring创始者建议采用ApplicationContext取代BeanFactory。
48 * */
49 // 2.1 FileSystemXmlApplicationContext 可以指定XML定义文件的路径来读取定义文件
50 ApplicationContext context = new FileSystemXmlApplicationContext( " beans.xml " );
51 hellobean = (HelloBean)context.getBean( " helloBean " );
52 System.out.println( " FileSystemXmlApplicationContext 调用 " );
53 hellobean.sayHello();
54 // 2.2 ClassPathXmlApplicationContext 可以从classpath中读取XML文件
55 context = new ClassPathXmlApplicationContext( " beans.xml " );
56 hellobean = (HelloBean)context.getBean( " helloBean " );
57 System.out.println( " ClassPathXmlApplicationContext 调用 " );
58 hellobean.sayHello();
59 // 2.3 XmlWebApplicationContext 从Web应用程序的文件架构中,指定相对位置来读取定义文件
60 // 略
61 }
62 }
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
10 public class HelloBeanTest {
11 public static void main(String[] args) {
12 /**
13 * 当普通 javaBean 使用
14 * */
15 HelloBean hellobean = new HelloBean();
16 hellobean.setHelloworld( " Hello World! " );
17 System.out.println( " new对象直接调用 " );
18 hellobean.sayHello();
19
20 /**
21 * 通过Spring访问JavaBean组件
22 * 1、BeanFactory 方式
23 * */
24 // 1.1用 ClassPathResource 时 beans.xml 可以从classpath中读取XML文件
25 Resource resource = new ClassPathResource( " beans.xml " );
26 BeanFactory factory = new XmlBeanFactory(resource);
27 hellobean = (HelloBean) factory.getBean( " helloBean " );
28 System.out.println( " ClassPathResource + XmlBeanFactory 调用 " );
29 hellobean.sayHello();
30
31 // 1.2用FileSystemResources 时 beans.xml 可以指定XML定义文件的路径来读取定义文件
32 resource = new FileSystemResource( " beans.xml " );
33 factory = new XmlBeanFactory(resource);
34 hellobean = (HelloBean) factory.getBean( " helloBean " );
35 System.out.println( " FileSystemResource + XmlBeanFactory 调用 " );
36 hellobean.sayHello();
37
38 /**
39 * 2、ApplicationContext 方式
40 * 采用BeanFactory对简单的应用程序来说就够了,可以获得对象管理上的便利性。
41 * 但是要获取一些特色和高级的容器功能,可以采用ApplicationContext。
42 * ApplicationContext提供了一些高级的功能,比如:
43 * 2.提供文字消息解析的方法
44 * 3.支持国际化(i18n)消息
45 * 4.可以发布事件,对事件感兴趣的Bean可以接收这些事件
46 *
47 * Spring创始者建议采用ApplicationContext取代BeanFactory。
48 * */
49 // 2.1 FileSystemXmlApplicationContext 可以指定XML定义文件的路径来读取定义文件
50 ApplicationContext context = new FileSystemXmlApplicationContext( " beans.xml " );
51 hellobean = (HelloBean)context.getBean( " helloBean " );
52 System.out.println( " FileSystemXmlApplicationContext 调用 " );
53 hellobean.sayHello();
54 // 2.2 ClassPathXmlApplicationContext 可以从classpath中读取XML文件
55 context = new ClassPathXmlApplicationContext( " beans.xml " );
56 hellobean = (HelloBean)context.getBean( " helloBean " );
57 System.out.println( " ClassPathXmlApplicationContext 调用 " );
58 hellobean.sayHello();
59 // 2.3 XmlWebApplicationContext 从Web应用程序的文件架构中,指定相对位置来读取定义文件
60 // 略
61 }
62 }