1.Spring需要用到的jar包为Spring-mvc 3.2.8.RELEASE
配置文件applicationContext.xml放在src/main/resource下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- id:属性:设置bean的名称,要求唯一
class属性:类名(要求包含包名);
-->
<!--此处写配置文件相关的bean属性 比如:
<bean id="stu1" class="day01.Student"></bean>
<bean id="time" class="java.util.Date"/>
<bean id="sdf" class="java.text.SimpleDateFormat"></bean>
-->
</beans>
2.在src/main/java中写Java代码
包名为:day01
包中包含两个文件:
FirstSpring.java
package day01; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class FirstSpring { public static void main(String[] args) { /** * 用于启动spring容器 * ApplicationContext:接口 * ClassPathXmlApplicationContext是 * ApplicationContext接口的一个实现类; */ ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml"); // System.out.println(ac); //通过spring容器获得对象;避免做强制转换; Student stu1=ac.getBean("stu1",Student.class); System.out.println(stu1); Date date=ac.getBean("time",Date.class); SimpleDateFormat sdf=ac.getBean("sdf",SimpleDateFormat.class); String time=sdf.format(date); System.out.println(date); System.out.println(time); Calendar cal1=ac.getBean("cal1",Calendar.class); System.out.println("cal1:"+cal1); Date date2=ac.getBean("time1",Date.class); System.out.println("date2:"+date2); } }
Student.java
package day01; public class Student { public Student(){ System.out.println("这是一个学生student()"); } }
3.运行FirstSpring文件
控制台输出:
这是一个学生student() day01.Student@100a15d Fri May 12 16:26:14 CST 2017 17-5-12 下午4:26 cal1:java.util.GregorianCalendar[time=1494577574487,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=4,WEEK_OF_YEAR=19,WEEK_OF_MONTH=2,DAY_OF_MONTH=12,DAY_OF_YEAR=132,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=26,SECOND=14,MILLISECOND=487,ZONE_OFFSET=28800000,DST_OFFSET=0] date2:Fri May 12 16:26:14 CST 2017