在Maven的基础上通过Spring使用WEB

Spring在WEB和JAVA的区别(maven的基础上):
  1. 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容器的创建:

  1. 非WEB应用在main方法中直接创建
  2. WEB 应用在WEB 容器(就是服务器)加载时,创建ICO容器:也就是在Listener(监听器)的contextInitialized(ServletContextEvent servletContextEvent)方法中创建IOC容器

3 .IOC容器的访问:

  1. 在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快捷对象:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值