idea tomcat整合Spring

首先maven配置 导入jar包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.3.RELEASE</version>
        </dependency>

写一个整合帮助类 


 
 
import org.springframework.context.ApplicationContext;
 
import javax.servlet.ServletContext;
 
/**
 * 将spring的上下文整合进tomcat的容器中
 */
public class SpringwebUtil {
public static  String SPRING_CONTEXT_KEY="spring-context-key";
public  static  void setapplicationContext(ApplicationContext applicationContext, ServletContext servletContext){
servletContext.setAttribute(SPRING_CONTEXT_KEY,applicationContext);
}
 
public  static  ApplicationContext getApplicationContext(ServletContext servletContext){
    return (ApplicationContext) servletContext.getAttribute(SPRING_CONTEXT_KEY);
}
 
}

使用监听启动


 
import com.ywj.web.SpringwebUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
 
public class SpringloaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("   初始化  ");
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
        SpringwebUtil.setapplicationContext(applicationContext,servletContextEvent.getServletContext());
    }
 
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
 
    }
}

用servlet测试能不能获取到Spring上下文


 
import org.springframework.context.ApplicationContext;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
public class Userservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext applicationContext=SpringwebUtil.getApplicationContext(req.getServletContext());
        UserAction userAction = (UserAction) applicationContext.getBean("userAction");
        userAction.upload();
    }
}

你使用了servlet肯定得去Web.xml配置,监听器也得配置上

<listener>
  <listener-class>com.hhh.listener.SpringloaderListener</listener-class>
</listener>
<servlet>
  <servlet-name>userservlet</servlet-name>
  <servlet-class>com.hhh.web.Userservlet</servlet-class>
</servlet>
  <servlet-mapping>
    <servlet-name>userservlet</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

 

Spring的配置文件

 

    <bean class="com.hhh.biz.impl.UserBizImpl" name="userBiz"></bean>
    <bean class="com.hhh.biz.impl.UserBizImpl2" name="userBiz2"></bean>
    <bean class="com.hhh.web.UserAction" name="userAction">
        <!--name是com.hhh.web.UserAction里的属性名 ref是容器中的bean-->
        <property name="userBiz" ref="userBiz"></property>
    </bean>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值