该示例记录spring框架的简单应用,首先需要引入spring框架jar包,推介下载地址
http://repo.springsource.org/libs-release-local/org/springframework/spring/
主要使用的jar有
spring-beans-4.3.8.RELEASE.jar
spring-context-4.3.8.RELEASE.jar
spring-context-support-4.3.8.RELEASE.jar
spring-core-4.3.8.RELEASE.jar
spring-expression-4.3.8.RELEASE.jar
commons-logging-1.2.jar
其中最后一个jar包commons-logging-1.2.jar不在spring下载文件夹中需要以下地址下载
http://commons.apache.org/proper/commons-logging/download_logging.cgi
new一个包midspring,下面定义一个接口,便于于引用
package midspring;
import java.util.List;
public interface BusiStudentImpl {
public void saveStudent(Student student);
public List<Student> queryStudent();
}
--下面具体的类实现该接口
package midspring;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BusiOfStudent implements BusiStudentImpl{
@Override
public void saveStudent(Student student) {
// TODO Auto-generated method stub
//理论上由数据访问层的方法保存数据,这里简化
System.out.println("数据保存成功");
}
@Override
public List<Student> queryStudent() {
// TODO Auto-generated method stub
//理论上由数据访问层的方法查询数据,这里简化
ApplicationContext context = new ClassPathXmlApplicationContext("midspring/Student.xml");
Student student1 =context.getBean("student1", Student.class);
Student student2 =context.getBean("student2", Student.class);
List<Student> list=new ArrayList<Student>();
list.add(student1);
list.add(student2);
System.out.println("查询返回list");
return list;
}
}
---定义一个javabean
package midspring;
public class Student {
protected String sno;
protected String sname;
protected String ssex;
protected String sbirthday;
protected String sclass;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public String getSbirthday() {
return sbirthday;
}
public void setSbirthday(String sbirthday) {
this.sbirthday = sbirthday;
}
public String getSclass() {
return sclass;
}
public void setSclass(String sclass) {
this.sclass = sclass;
}
}
--在包中定义配置bean.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="student1" class="midspring.Student">
<property name="sno" value="500"/> <!-- 仅为默认值 -->
<property name="ssex" value="男"/>
<property name="sname" value="jenkin"/>
<property name="sbirthday" value="1990-12-31" />
<!--
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="1990-12-31" />
</bean>
-->
<property name="sclass" value="12000"/>
</bean>
<bean id="student2" class="midspring.Student">
<property name="sno" value="501"/>
<property name="ssex" value="女"/>
<property name="sname" value="joen"/>
<property name="sbirthday" value="1965-12-31" />
<!--
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="1965-12-31" />
</bean>
-->
<property name="sclass" value="13010"/>
</bean>
<bean id="OfStudent" class="midspring.BusiOfStudent" />
</beans>
--最后使用spring的loC特性调用方法
package midspring;
import java.util.Iterator;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestImpl {
public static void main(String args[]){
ApplicationContext context = new ClassPathXmlApplicationContext("midspring/Student.xml");
//student1区分大小写
Student stu1=context.getBean("student1",Student.class);
stu1.setSno("111");
stu1.setSname("solgle");
stu1.setSbirthday("2015-1-1");
stu1.setSsex("女");
stu1.setSclass("2299");
BusiStudentImpl of=context.getBean("OfStudent",BusiOfStudent.class);
of.saveStudent(stu1);
List<Student> list=of.queryStudent();
Iterator<Student> it = list.iterator();
while(it.hasNext()){
Student stu=(Student)it.next();
System.out.println("sno:"+stu.getSno());
System.out.println("sname:"+stu.getSname());
System.out.println("ssex:"+stu.getSsex());
System.out.println("sbirthday:"+stu.getSbirthday());
System.out.println("sclass:"+stu.getSclass());
System.out.println("");
}
}
}
运行结果:
数据保存成功
查询返回list
sno:500
sname:jenkin
ssex:男
sbirthday:1990-12-31
sclass:12000
sno:501
sname:joen
ssex:女
sbirthday:1965-12-31
sclass:13010