Spring整合web项目

1.加载Spring核心配置文件

加载spring配置文件,根据创建对象

ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

2.实现思想:把加载配置文件和创建对象过程,在服务器启动时候完成;

 

3.实现原理

(1)ServletContext对象

(2)监听器

(3)具体使用

该监听器是servletContext(jsp application)的生命周期监听器

Tomcat启动时contextInitialzed用创建spring的容器webapplcationcontext

tomcat关闭时contextDestroued调用容器close方法关闭

maven中配置web

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.12.RELEASE</version>
 </dependency>
在web.xml中配置监听器:
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架提供了多种方式来整合Web开发。以下是一种常见的方式: 1. 添加依赖:在你的项目中,添加Spring Web的依赖。你可以使用Maven或Gradle来管理依赖。 Maven: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` Gradle: ```groovy implementation 'org.springframework.boot:spring-boot-starter-web' ``` 2. 创建控制器:创建一个控制器类来处理请求和响应。在这个类上使用`@Controller`注解,并在方法上使用`@RequestMapping`注解来映射URL和处理方法。 ```java @Controller public class MyController { @RequestMapping("/hello") public String hello() { return "Hello, World!"; } } ``` 3. 配置Spring MVC:创建一个配置类,使用`@Configuration`注解,并继承`WebMvcConfigurerAdapter`类。在这个类中,可以配置视图解析器、静态资源处理等。 ```java @Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/WEB-INF/views/", ".jsp"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("/static/"); } } ``` 4. 启动应用程序:使用Spring Boot或其他方式启动你的应用程序,Spring会自动扫描并注册控制器。 这只是一个简单的示例,你可以根据你的需求进行更多的配置和定制。希望对你有所帮助!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值