第一个Spring例子

手动添加sechame文件:

windows-->preferences-->myeclipse-->files and editors-->xml-->xmlcatalog--->点add--->Key Type中选择URL-->File system-->在dist/resources/spring-beans-2.5.xsd-->Sechema location;

下载Spring的包

1.倒入所需要的Spring的jar包

  必须要的两个jar包:dist\spring.jar,lib\jakarta-commons-logging.jar

  如果使用了切面变成,还需要下列jar文件:lib\aspectj\aspectjwear.jar和aspectjrt.jar,lib\cglib\cglib-nodep-2.1_3.jar

  如果使用了JSR-250中的注解:lib\j2ee\common-annotations.jar

 

2.建立beans.xml文件

  

<?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">

<bean id="personService" class="com.cn.service.impl.PersonServiceBean"></bean>
</beans>

 

3.定义一个借口,并写出其实现层:

package com.cn.service;

public interface PersonService {

public abstract void save();

}

 

package com.cn.service.impl;

import com.cn.service.PersonService;

public class PersonServiceBean implements PersonService {
public void save(){
System.out.println("my name is save method");
}
}

 

4.通过Spring框架来调用bean中的方法 

ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
PersonService psersonService=(PersonService) ctx.getBean("personService");
psersonService.save();

  

posted @ 2011-12-09 11:18  Chen20121202  阅读(263)  评论(0编辑  收藏  举报