-
配置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中定义的各种参数,如果不存在名为contextConfigLocation的参数,容器会自动默认加载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
网友评论