Spring2.5教程:2、搭建与测试Spring的开发环境

来源:互联网 发布:无敌数据恢复注册码 编辑:程序博客网 时间:2024/05/15 04:10

使用Spring需要的jar

http://www.springsource.org/download下载spring,然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下

dist\spring.jar

lib\jakarta-commons\commons-logging.jar

如果使用了切面编程(AOP),还需要下列jar文件

lib/aspectj/aspectjweaver.jaraspectjrt.jar

lib/cglib/cglib-nodep-2.1_3.jar

如果使用了JSR-250中的注解,@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件

lib\j2ee\common-annotations.jar

 

spring的配置文件模版

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

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  .....

</beans>

该配置模版可以从spring的参考手册或spring的例子中得到。配置文件的取名可以任意,文件可以存放在任何目录下,但考虑到通用性,一般放在类路径(src)下。

 

编写spring配置文件时,不能出现帮助信息

由于springschema文件位于网络上,如果机器不能连接到网络,那么在编写配置信息时候就无法出现提示信息,解决方法有两种:

1让机器上网,eclipse会自动从网络上下载schema文件并缓存在硬盘上。

2手动添加schema文件,方法如下:

windwos->preferences->myeclipse->files and editors->xml->xmlcatalog

"add",在出现的窗口中的Key Type中选择URI,location中选"File system",然后在spring解压目录的dist/resources目录中选择spring-beans-2.5.xsd,回到设置窗口的时候不要急着关闭窗口,应把窗口中的Key Type改为Schema location,Key改为http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

 

实例化spring容器

实例化Spring容器常用的两种方式:

方法一:

在类路径下寻找配置文件来实例化容器

ApplicationContextctx =new ClassPathXmlApplicationContext(new String[]{"beans.xml"});

方法二:

在文件系统路径下寻找配置文件来实例化容器

ApplicationContextctx =new FileSystemXmlApplicationContext(new String[]{“d:\\beans.xml“});

Spring的配置文件可以指定多个,可以通过String数组传入。

 

spring容器中得到bean

spring容器启动后,因为spring容器可以管理bean对象的创建,销毁等生命周期,所以我们只需从容器直接获取Bean对象就行,而不用编写一句代码来创建bean对象。从容器获取bean对象的代码如下:

ApplicationContextctx = newClassPathXmlApplicationContext(“beans.xml”);

OrderService service = (OrderService)ctx.getBean("personService");

 

使用dom4j读取spring配置文件(Spring管理bean原理)

public class ItcastClassPathXmlApplicationContext {

     private List<BeanDefinition>beanDefines = newArrayList<BeanDefinition>();

     publicItcastApplicationContext(String filename){

   init(filename);

     

      private void init(String filename){

       SAXReadersaxReader = newSAXReader();  

        Document document=null;  

        try{

         URL xmlpath =this.getClass().getClassLoader().getResource(filename);

         document = saxReader.read(xmlpath);

         Map<String,String>nsMap = new HashMap<String,String>();

         nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间

         XPathxsub =document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径

         xsub.setNamespaceURIs(nsMap);//设置命名空间

         List<Element> beans =xsub.selectNodes(document);//获取文档下所有bean节点

         for(Elementelement: beans){

            String id = element.attributeValue("id");//获取id属性值

            String clazz = element.attributeValue("class"); //获取class属性值       

            BeanDefinitionbeanDefine =newBeanDefinition(id,clazz);

            beanDefines.add(beanDefine);

         }  

        }catch(Exception e){  

            e.printStackTrace();

        }

    }

 

 

 

 

原创粉丝点击