java web的 怎么加载spring

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java web的 怎么加载spring相关的知识,希望对你有一定的参考价值。

spring在web环境中,java代码里需要得到ApplicationContext; 根据前期配置的不同,有两种方式:
方式一,spring加载放到web.xml中配置:

[html] view plain copy print?
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

则得到ApplicationContext用如下方式:
[java] view plain copy print?
//在struts中需要实现ServletRequestAware类,便可得到request对象
ServletContext s = ServletActionContext.getRequest().getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(s);
java代码中调用 ctx.getBean(String beanId);

方式二,直接声明:

[java] view plain copy print?
public class BeanManager
private static ApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml") ;

public static Object getBean(String beanId)
return context.getBean(beanId);



在web.xml中写一个servlet,自动启动,init方法中调用一下BeanManager,为的是在Web应用启动的时候就让Spring加载bean配置文件,否则会在第一次调用BeanManager的时候加载,影响第一次访问速度。
[java] view plain copy print?
init() throws ServletException

BeanManager bm = new BeanManager();



在java代码中使用 BeanManager.getBean(String beanId); 来获得bean实例。
参考技术A

使用监听器来注册spring

<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener>    
  <!-- 默认配置在WEB-INF目录下 -->  
  <context-param>  
      <param-name>contextConfigLocation</param-name>  
      <param-value>classpath:/applicationContext.xml</param-value>   <!-- <param-value>/WEB-INF/spring*.xml</param-value> -->  
 </context-param>

本回答被提问者采纳
参考技术B 请直接使用 springmvc 。。。。。。。。官网有包、文档

spring中怎么实现过滤器和监听器?

麻烦说具体点,最好有例子,谢谢大家~~

1、延迟加载过滤器

    Hibernate 允许对关联对象、属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Session 范围之内进行。如果 Service 层返回一个启用了延迟加载功能的领域对象给 Web 层,当 Web 层访问到那些需要延迟加载的数据时,由于加载领域对象的 Hibernate Session 已经关闭,这些导致延迟加载数据的访问异常。

    Spring 为此专门提供了一个 OpenSessionInViewFilter 过滤器,它的主要功能是使每个请求过程绑定一个 Hibernate Session,即使最初的事务已经完成了,也可以在 Web 层进行延迟加载的操作。

<filter> 
    <filter-name>hibernateFilter</filter-name> 
    <filter-class> 
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 
    </filter-class> 
 </filter> 
 <filter-mapping> 
    <filter-name>hibernateFilter</filter-name> 
    <url-pattern>*.html</url-pattern> 
 </filter-mapping>

2、乱码过滤器

对post乱码的处理,如下

<filter> 
    <filter-name>encodingFilter</filter-name> 
    <filter-class> 
        org.springframework.web.filter.CharacterEncodingFilter ① Spring 编辑过滤器
    </filter-class> 
    <init-param> ② 编码方式
        <param-name>encoding</param-name> 
        <param-value>UTF-8</param-value> 
    </init-param> 
    <init-param> ③ 强制进行编码转换
        <param-name>forceEncoding</param-name> 
        <param-value>true</param-value> 
    </init-param> 
    </filter> 
    <filter-mapping> ② 过滤器的匹配 URL 
        <filter-name>encodingFilter</filter-name> 
        <url-pattern>*.html</url-pattern> 
    </filter-mapping>

3、请求跟踪日志过滤器

    程度调试者可以详细地查看到有哪些请求被调用,请求的参数是什么,请求是否正确返回等信息,需要将log4j设为debug

    org.springframework.web.filter.ServletContextRequestLoggingFilter: 该过滤器将请求的 URI 记录到 Common 日志中


4、WebAppRootListener

    可以将 Web 应用根目录添加到系统参数中,对应的属性名可以通过名为“webAppRootKey”的 Servlet 上下文参数指定,默认为“webapp.root”,配置如下

<context-param> 
    <param-name>webAppRootKey</param-name> 
    <param-value>baobaotao.root</param-value> ① Web 应用根目录以该属性名添加到系统参数中
 </context-param> 

② 负责将 Web 应用根目录以 webAppRootKey 上下文参数指定的属性名添加到系统参数中
 <listener> 
    <listener-class> 
    org.springframework.web.util.WebAppRootListener 
    </listener-class> 
 </listener>

5、Log4jConfigListener 监听器

    包括了 WebAppRootListener 的功能,也就是说,Log4jConfigListener 会自动完成将 Web 应用根目录以 webAppRootKey 上下文参数指定的属性名添加到系统参数中,在log4j.xml可以直接使用


6、Introspector 缓存清除监听器

    负责处理由 JavaBean Introspector 功能而引起的缓存泄露。IntrospectorCleanupListener 监听器在 Web 应用关闭的时会负责清除 JavaBean Introspector 的缓存,在 web.xml 中注册这个监听器可以保证在 Web 应用关闭的时候释放与其相关的 ClassLoader 的缓存和类引用。

参考技术A <?xml version="1.0" encoding="UTF-8" ?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/spring-dao.xml /WEB-INF/classes/spring/spring-service.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>本回答被提问者采纳

以上是关于java web的 怎么加载spring的主要内容,如果未能解决你的问题,请参考以下文章

java web开发,关于ajax提交验证通过后 window.open无法加载页面的问题

用java写个web,想要从数据库查找并修改内容。要怎么实现

java 下拉列表怎么从数据库中读取数据

java怎么连接mysql

JAVA Web.xml 加载顺序

未能加载文件或程序集“Microsoft.VisualStudio.Web.Runtime 怎么解决?