spring问题总结和学习

写测试用例的时候要注意在scan包扫描,不然扫不到Controller;

 

Controller返回页面莫名其妙404的时候,要检查ModelView的包是否引入正确;

 

 

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

1.没有在spring的ApplicationContext.xml中开启注解事务

<!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="txManager"/>

2.没有在方法上挂注解事务标签

@Transactional

3.方法或者父类方法中有@Asyn异步标签

 

spring-mvc中不能访问jsp:

不能设置为 /* 要设置为 /

 1.serlvet的匹配规则:
  1).路径精确匹配
  2).最长路径匹配
  3).扩展匹配
  4).如果容器定义了一个default servelt(即匹配路径为“/”的servlet),则会将请求交给default servlet
  注意:/*.action:错误的匹配规则,不能即是路径匹配,也是扩展匹配

2.spring mvc中dispatcherServlet配置为/*访问请求转发*.jsp和访问jsp页面的时候访问不到的原因:
     *默认在%TOMCAT_HOME%/conf/web.xml中配置了*.jsp由JspServelt来处理,当我们将spring mvc的拦截规则配置为/*的时候,按照servlet的匹配规则,则路径匹配会优先于扩展匹配,导致对jsp的请求会被拦截掉。
*当spring mvc配置为/,表示未默认servelt,只有当请求没有对应的servlet处理时,才交给它处理,当我们请求jsp时,刚好有从%TOMCAT_HOME%/conf/web.xml中继承过来的JspServlet会处理对jsp请求的处理,所以会访问到jsp页面。

3.filter的顺序为filter-mapping在web.xml中声明路径的顺序,即filter-mapping的顺序

 

ContextLoaderListener的作用

ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。

在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。看看它的API说明。
第一段说明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。如果查看ContextLoaderServlet的API,可以看到它也关联了ContextLoader这个类而且它实现了HttpServlet这个接口。
 第二段,ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->BeanFactory这样一来spring中的所有bean都由这个类来创建
第三段,讲如何部署applicationContext的xml文件。
如果在web.xml中不写任何参数配置信息,默认的路径是/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml;
如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/classes/applicationContext-*.xml  
        </param-value>
    </context-param> 

 

DispatcherServlet和 contextConfigLocation的区别

值得注意的是, DispatcherServlet的上下文仅仅是Spring MVC的上下文, 而ContextLoaderListener的上下文则对整个Spring都有效
 
我们先看下这两个东东的配置方法:

 
对于contextConfigLocation参数,有2个地方可以配置:
1)context-param 是全局性配置
2)servlet下的init-param 是局部性配置
若以上两处都设置了一个相同的bean配置文件路径,那么该文件内配置的bean会被初始化2次,所以一个配置文件只能选择一种配置位置
 
项目中使用spring框架有2种方式:
1)listener下的ContextLoaderListener 是一种引入方式,默认读取/WEB-INF/applicationContext.xml
2)若是spring-web项目,DispatcherServlet 也是一种引入方式,默认读取/WEB-INF/${servlet-name}-servlet.xml
倘若以上2种配置都引入了,那么全局性的bean配置文件会被加载2次;而且2种方式的各自配置文件里的配置项在某种意义上并不是合并互补,而是各成一个体系(虽然普通的bean看似是都加载到全局上下文来了,但还是有一些特殊bean和配置项没有按预期的那样工作);
比如/WEB-INF/applicationContext.xml文件下里的AOP声明式配置:
<!--aop 行为-->
<bean id="himvn" class="com.tangbao.hellomvn.Himvn" />
<!--aop 注释方式-->
<bean id="hiaspect" class="com.tangbao.hellomvn.Hiaspect" />
<!--aop config-->
<aop:aspectj-autoproxy />
<aop:config>
<aop:aspect id="aoplianxi" ref="himvn">
<aop:pointcut id="test1" expression="execution(* com.tangbao.controller.RestlessController.RestlessController(..))"></aop:pointcut>
<aop:before method="sayHi" pointcut-ref="test1"></aop:before>
<aop:after method="sayHi" pointcut-ref="test1"></aop:after>
</aop:aspect>
</aop:config>
若只是在全局配置项中,而没有在DispatcherServlet 中加载,那么此aop会无效。
 
所以,在web项目中,就不要使用ContextLoaderListener 和全局配置contextConfigLocation参数了,统一在DispatcherServlet 下配置,应该就不那么混乱了。如下:
这样结果就如我们的预期:多bean配置文件不会出现重复加载,所有aop配置也都生效。

 

sping中xml配置静态变量,遇到的问题:

<bean value="test" class="x.x.x.Config">
    <property value="FLAG" ref="true"/>
</bean>

Config类中:

public static String FLAG=false;

在Constants类中

public static String FLAG=Config.FLAG;

出现Constants.FLAG并未被XML覆盖;原因是:

 xml中,Config的配置前面有sessionFactory的配置导致的

 

关于两次加载Bean的问题:

mvc对应的配置文件 *-servlet.xml 和 spring容器对应的配置文件 applicationContext.xml中有相同的注解导致的;

例如都配置了dataSource;都配置了包扫描;

合理的是mvc中只扫描Controller注解;其他的交由容器扫描

 

 

No bean named 'springSecurityFilterChain' is defined

一般是由于spring的context启动失败,检查application.xml和mvc.xml;

component-scan的相关扫描的包是否存在,比如写成com,实际是cn.com

 

其他原因:http://www.cnblogs.com/chenying99/archive/2012/08/19/2646350.html

根源是spring容器启动失败

 

springSecurityFilterChain'. Check to ensure the Filter is only configured onc

http://blog.csdn.net/junandjun/article/details/51582261

 

spring事务失败的问题排查:

http://blog.csdn.net/szwangdf/article/details/41516239

 

spring中使用拦截器

https://i.cnblogs.com/EditArticles.aspx?pg=3&catid=872484

 

spring mvc加载两次的问题:

http://blog.csdn.net/d8111/article/details/45249845

 

sping mvc接受List

http://www.cnblogs.com/wsw0515/p/3582627.html

 

@Controle和@RestContro的区别:

http://blog.csdn.net/gg12365gg/article/details/51345601

 

@Async用法和注意事务

https://blog.csdn.net/chenaini119/article/details/51850526

 

posted @ 2017-05-08 17:26  malcome  阅读(65)  评论(0)    收藏  举报