當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
spring第一个小例子(Spring_xjs1)
生活随笔
收集整理的這篇文章主要介紹了
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 {@Testpublic 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=安陽]
?
轉載于:https://www.cnblogs.com/xjs1874704478/p/11171158.html
總結
以上是生活随笔為你收集整理的spring第一个小例子(Spring_xjs1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Maven项目整合讲义(Eclipse版
- 下一篇: 羊圈