转贴处 http://www.blogjava.net/improviser/archive/2007/09/24/147650.html
初探Spring程序,程序如下:
1
BeanNaming.java
2 package spring.beantest;
3
4 import org.springframework.beans.factory.BeanFactory;
5 import org.springframework.beans.factory.xml.XmlBeanFactory;
6 import org.springframework.core.io.FileSystemResource;
7
8 public class BeanNaming {
9 public static void main(String []args)
10 {
11 BeanFactory factory = new XmlBeanFactory( new FileSystemResource( " src/applicationContext.xml " ));
12
13 String s1 = (String)factory.getBean( " name1 " );
14 String s2 = (String)factory.getBean( " name2 " );
15 String s3 = (String)factory.getBean( " name3 " );
16 String s4 = (String)factory.getBean( " name4 " );
17
18 System.out.println((s1 == s2));
19 System.out.println((s2 == s3));
20 System.out.println((s3 == s4));
21
22 String[] x = factory.getAliases( " name3 " );
23 for (String str:x)
24 {
25 System.out.println(str);
26 }
27
28 }
29 }
30
31 applicationContext.xml
32 <? xml version = " 1.0 " encoding = " UTF-8 " ?>
33 <! DOCTYPE beans PUBLIC " -//SPRING//DTD BEAN//EN " " http://www.springframework.org/dtd/spring-beans.dtd " >
34 < beans >
35 <!-- aliasing examples -->
36 < bean id = " name1 " name = " name2,name3,name4 " class = " java.lang.String " />
37 </ beans >
2 package spring.beantest;
3
4 import org.springframework.beans.factory.BeanFactory;
5 import org.springframework.beans.factory.xml.XmlBeanFactory;
6 import org.springframework.core.io.FileSystemResource;
7
8 public class BeanNaming {
9 public static void main(String []args)
10 {
11 BeanFactory factory = new XmlBeanFactory( new FileSystemResource( " src/applicationContext.xml " ));
12
13 String s1 = (String)factory.getBean( " name1 " );
14 String s2 = (String)factory.getBean( " name2 " );
15 String s3 = (String)factory.getBean( " name3 " );
16 String s4 = (String)factory.getBean( " name4 " );
17
18 System.out.println((s1 == s2));
19 System.out.println((s2 == s3));
20 System.out.println((s3 == s4));
21
22 String[] x = factory.getAliases( " name3 " );
23 for (String str:x)
24 {
25 System.out.println(str);
26 }
27
28 }
29 }
30
31 applicationContext.xml
32 <? xml version = " 1.0 " encoding = " UTF-8 " ?>
33 <! DOCTYPE beans PUBLIC " -//SPRING//DTD BEAN//EN " " http://www.springframework.org/dtd/spring-beans.dtd " >
34 < beans >
35 <!-- aliasing examples -->
36 < bean id = " name1 " name = " name2,name3,name4 " class = " java.lang.String " />
37 </ beans >
输出结果为:
true
true
true
name1
name4
name2
可以看出,使用中id属性和name属性几乎没有任何区别。调用beanfactory.getAliases(string)的方法时,传入的参数可以是任意一个bean名字,输出的别名则是除去作为参数本身之外的所有 bean名。
另: 在写 applicationContxt.xml文件时,如果没有定义<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">,则会抛出 Cannot find the declaration of element 'beans' 异常。
以上使用BeanFactory对配置文件进行加载,BeanFactory,是根据配置文件负责创建Bean的实例,并负责Bean的生命周期的管理- --,包括Bean的生成与销毁、Bean的创建方式的识别(是否为singleton)、Bean的各个属性的设定、依赖关系的建立等。
ApplicationContext接口,提供了国际化、事件处理及beans在context中的自查能力。它也可创建具有层次结构context环境,将bean的作用域和可访问区域限制在应用程序的一个特定部分中。下面演示如何使用ApplicationContext接口。
1
<!
DOCTYPE beans PUBLIC
"
-//SPRING//DTD BEAN//EN
"
"
http://www.springframework.org/dtd/spring- beans.dtd
"
>
2 < beans >
3 < bean id = " bean_1 " class = " BeanTest " />
4 < bean id = " bean_2 " class = " BeanTest " />
5 </ beans >
6
7
8 public class BeanTest {
9 public void test() {
10 System.out.println( " test.. " );
11 }
12 }
13
14
15 import org.springframework.context.ApplicationContext;
16 import org.springframework.context.support.FileSystemXmlApplicationContext;
17
18 public class BeanNameExample {
19
20 public static void main(String[] args) {
21 ApplicationContext ctx = new FileSystemXmlApplicationContext(
22 " build/applicationContext.xml " );
23
24 BeanTest beanOne = (BeanTest)ctx.getBean( " bean_1 " );
25 BeanTest beanTwo = (BeanTest)ctx.getBean( " bean_2 " );
26
27 beanOne.test();
28 beanTwo.test();
29 }
30 }
31
综上,ApplicationContext与BeanFactory的不同点2 < beans >
3 < bean id = " bean_1 " class = " BeanTest " />
4 < bean id = " bean_2 " class = " BeanTest " />
5 </ beans >
6
7
8 public class BeanTest {
9 public void test() {
10 System.out.println( " test.. " );
11 }
12 }
13
14
15 import org.springframework.context.ApplicationContext;
16 import org.springframework.context.support.FileSystemXmlApplicationContext;
17
18 public class BeanNameExample {
19
20 public static void main(String[] args) {
21 ApplicationContext ctx = new FileSystemXmlApplicationContext(
22 " build/applicationContext.xml " );
23
24 BeanTest beanOne = (BeanTest)ctx.getBean( " bean_1 " );
25 BeanTest beanTwo = (BeanTest)ctx.getBean( " bean_2 " );
26
27 beanOne.test();
28 beanTwo.test();
29 }
30 }
31
BeanFactory提供了针对JavaBean的管理功能,而ApplicationContext提供了一个更为框架化的实现(从上面的示例中可以看出,BeanFactory的使用方式更加类似一个API,而非Framework style)。ApplicationContext覆盖了BeanFactory的所有功能,并提供了更多的特性。此外, ApplicationContext为与现有应用框架相整合,提供了更为开放式的实现(如对于Web应用,我们可以在web.xml中对 ApplicationContext进行配置)。