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的区别

<!--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>


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

浙公网安备 33010602011771号