Spring在WEB和JAVA的区别(maven的基础上):
- jar包不同:
- 普通java项目:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
- WEB:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
2.IOC容器的创建:
- 非WEB应用在main方法中直接创建
- WEB 应用在WEB 容器(就是服务器)加载时,创建ICO容器:也就是在Listener(监听器)的contextInitialized(ServletContextEvent servletContextEvent)方法中创建IOC容器
3 .IOC容器的访问:
- 在WEB中,IOC在contextInitialized(ServletContextEvent servletContextEvent)中创建后,可以把其放在ServletContext(application域或者是web的全局对象(我的理解))
4.Spring配置文件的名字和位置应该也是可配置的。将其配置到当前的web应用初始化参数中较为合适
步骤:
1.配置pom.xml:
<dependencies>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!--spring在web的配置-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
</dependencies>
2.为了方便,通过web.xml配置
web.xml:
<!--配置Spring配置文件的名称和位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:application.xml</param-value>
</context-param>
<listener>
<listener-class>Com.Listener.SpringServletListener</listener-class>
</listener>
application.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="testBean" class="Com.Beans.testBean">
<property name="a" value="2"></property>
</bean>
</beans>
Listner:
public class SpringServletListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
//0.获取Spring配置文件
ServletContext servletContext = servletContextEvent.getServletContext();
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
//1.在服务器开始servletContextEvent被创建的时候,创建IOC
//2.创建IOC
ApplicationContext ctx=new ClassPathXmlApplicationContext(contextConfigLocation);
servletContext.setAttribute("ApplicationContext", ctx);
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
然后就可以获取了.
通过Spring提供的Listener:
web.xml:获取通过快捷的方法getWebApplicationContext(我这里在Jsp的域中获取的)
Jsp快捷对象: