/**
* 使用Spring需要的jar 到http://www.springsource.org/download下载
*
* dist/spring.jar lib/jakarta-commons/commons-logging.jar
* 如果使用了切面编程,还需要下面jar文件
* lib/aspecttj/aspect.jweaver.jar和aspect.jrt.jar
* lib/cglib/cglib-nodep-2.1.3.jar
* 如果是是使用了JSR-250中的注解,如@Resource/@postConstuct/@PreDestroy,还需要下列jar文件
* lib/j2ee/common-annotarions.jar
*
*
*
* spring配置文件模板 到这找
* spring-framework-2.5.6/docs/reference/html_single/index.html
*
* 搜
*
*
* xmlns="
* xmlns:xsi="
* xsi:schemaLocation="
http://www.springframework.org/schema/beans
*
>
*
*
*
*
*
*
*
*
*
*
* 该配置模板可以从参考手册或者例子当中获取,该配置文件名称随意,可以放在任意目录下,但考虑到通用性一般放在类路径下
*
*
实例化Spring容器常用的两种方式:
*
* 方法一:
* 在类路径下寻找配置文件来实例化容器
*
* ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
*
* 方法二:
* 在文件系统路径下寻找配置文件来实例化容器
*
* --一般不用这个,因为通用性不好,变了操作系统的话,还得变。
* ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"d://beans.xml"});
*
* 配置文件可以有多个,可以通过String数组传入。
*
*
编写spring配置文件时,不能出现帮助信息
*
* 由于spring的schema文件位于网络上,如果机器不能连接到网络,那么在编写配置信息时就无法出现提示信息,解决方法有两种:
*
* 一.让机器上网,eclipse 会自动从网络上下载schema文件并缓存到硬盘上。
* 二.手动添加schema文件,方法如下:
* window->prdferences->myeclipse->files and editors->xml0>xmlcatalog
* 点"add",在出现的窗口中的key Type中选择URL,在location中选“File system”,然后在spring解压目录的dist/resources目录中选择spring-beans-2.5.xsd,回
* 到设置窗口的时候不要急着关闭窗口,应把窗口中的key Type 改为Scehma Location,Key 改为Http;//www.springframeword.org/schema/beans/spring-beans-2.5.xsd
*
实例:
*/
//beans.xml 1.0" encoding="UTF-8"?> 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 "> personService" class=" myk.PersionServiceBean">
//业务bean接口 public interface PersionService { public abstract void save(); }
//业务bean public class PersionServiceBean implements PersionService { /* (non-Javadoc) * @see myk.PersionService#save() */ public void save(){ System.out.println("我是save()方法"); } }
//测试 import myk.PersionService; import myk.PersionServiceBean; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void instanceSpring(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersionService PersionService = (PersionServiceBean)ctx.getBean("personService"); PersionService.save(); } }