spring第一个小例子(Spring_xjs1)
第一个spring小例子(使用Spring4.0版本)
Spring-HelloWord
**导入核心jar包:
spring-beans
spring-context
spring-core
spring-aop
spring-expression
spring-logging
步骤:1
在项目名上右键选择properties-->Project Facets/Runtimes进行配置。
pom.xml文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.oracle.dwp</groupId> <artifactId>Spring_first</artifactId> <version>1.0.0</version> <dependencies> <!-- aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.0.0.RELEASE</version> </dependency> <!-- 上下文 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.0.RELEASE</version> </dependency> <!-- junit单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> </project>
Students.java实体类:
package com.entity; import java.io.Serializable; import java.util.Date; public class Students implements Serializable{ private String sid;//学号 private String name;//姓名 private String gender;//性别 private Date birthday;//生日 private String address;//住址 public Students(String sid, String name, String gender, Date birthday, String address) { this.sid = sid; this.name = name; this.gender = gender; this.birthday = birthday; this.address = address; } public Students() { } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "Students [sid=" + sid + ", name=" + name + ", gender=" + gender + ", birthday=" + birthday + ", address=" + address + "]"; } }
applicationContext.xml配置文件---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-4.0.xsd"> <bean id="dateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd"/> </bean> <bean id="stu" class="com.entity.Students"> <property name="sid" value="s0001"></property> <property name="name" value="谢军帅"></property> <property name="gender" value="男"></property> <property name="birthday"> <bean factory-bean="dateFormat" factory-method="parse"> <constructor-arg value="1998-07-15"></constructor-arg> </bean> </property> <property name="address" value="安阳"></property> </bean> </beans>
测试类StudentsTest.java:
package com.entity; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class StudentsTest { @Test public void fun1(){ //获得上下文对象 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); //获得名字叫stu的学生对象。通过IOC容器获得 //这个学生对象是通过IOC容器注入给你的,无需用new.---利用反射 //两大核心技术:1.XML解析2.反射 Students s = (Students) ctx.getBean("stu"); System.out.println(s); } }
测试结果:
Students [sid=s0001, name=谢军帅, gender=男, birthday=Wed Jul 15 00:00:00 GMT+08:00 1998, address=安阳]