-
配置jar包
使用idea建立spring項目羹饰,項目會自動下載所需要的jar包 - web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring_config.xml,classpath*:beans.xml</param-value>
</context-param>
</web-app>
原理:
當Tomcat容器啟動的時候啤挎,會加載web.xml中定義的各種參數(shù)憎乙,如果不存在名為contextConfigLocation的參數(shù)屁倔,容器會自動默認加載applicationContext.xml脑又,所以不需要定義,即可以使用applicationContext.xml配置文件,如果需要更多的配置文件问麸,可通過自定義往衷,使用逗號分隔開各個配置文件名,格式如“classpath:configFileName.xml”严卖,其中"*"為通配符席舍。
- model模型
package javaSource.com.freTiger.spring;
import java.util.Date;
public class HelloMessage {
public Date date;
public String content;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "HelloMessage{" + "content='" + content + '\'' + '}';
}
- 測試類
package javaSource.com.freTiger.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloMsgTest {
public static void main(String[] args) {
ApplicationContext alc = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloMessage h = (HelloMessage)alc.getBean("hello");
System.out.println("輸出的信息 : "+h.getContent());
}
}
- 配置文件applicationContext.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.xsd">
<bean id="hello" class="javaSource.com.freTiger.spring.HelloMessage">
<property name="content" value="welcome to china"/>
</bean>
</beans>
- 輸出結果
"D:\Program Files\JAVA\jdk1.8.0_112\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.1\lib\idea_rt.jar=60915:D:\Program Files\JetBrains\IntelliJ IDEA 2018.1.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\charsets.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\deploy.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\ext\access-bridge-64.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\ext\cldrdata.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\ext\dnsns.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\ext\jaccess.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\ext\jfxrt.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\ext\localedata.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\ext\nashorn.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\ext\sunec.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\ext\sunjce_provider.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\ext\sunmscapi.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\ext\sunpkcs11.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\ext\zipfs.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\javaws.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\jce.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\jfr.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\jfxswt.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\jsse.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\management-agent.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\plugin.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\resources.jar;D:\Program Files\JAVA\jdk1.8.0_112\jre\lib\rt.jar;D:\Program Files\JetBrains\workSpace\spring\out\production\spring;D:\Program Files\JetBrains\workSpace\spring\lib\spring-aspects-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-context-support-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-aop-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-context-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-instrument-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-instrument-tomcat-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-beans-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-core-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-expression-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-jdbc-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-jms-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-oxm-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-messaging-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-tx-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\commons-logging-1.2.jar;D:\Program Files\JetBrains\workSpace\spring\lib\aopalliance-1.0.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-orm-4.3.18.RELEASE.jar;D:\Program Files\JetBrains\workSpace\spring\lib\spring-test-4.3.18.RELEASE.jar" javaSource.com.freTiger.spring.HelloMsgTest
三月 21, 2019 6:24:35 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@27fa135a: startup date [Thu Mar 21 18:24:35 CST 2019]; root of context hierarchy
三月 21, 2019 6:24:36 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
輸出的信息 : welcome to china
Process finished with exit code 0