Spring開發常見異常及原因列表

異常:Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springweb.service.impl.HelloServiceImpl.setTypeMap(java.util.Map); nested exception is org.springframework.beans.FatalBeanException: Key type [class java.lang.Integer] of map [java.util.Map] must be assignable to [java.lang.String]  


原因:spring使用Autowired進入注入時,map的key類型只能爲string

 

異常:子類注入配置沒有加上abstract="true"的話,最終objList會有三個對象,包括baseObj  sonObj1   sonObj2

<bean id="baseObj" abstract="true" class="BaseObj" />
<bean id="sonObj1" class="SonObj1" parent="baseObj/>
<bean id="sonObj2" class="SonObj2" parent="baseObj/>
<bean id="objList" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="sonObj1"/>
<ref bean="sonObj2"/>
</list>
</constructor-arg>
</bean>

 

異常:org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2:

測試XML中配置了多個數據源,需要指定注入那個數據源。

public class BaseSpringContextTestSupport extends AbstractTransactionalJUnit38SpringContextTests{
 @Override
 @Resource(name = "dataSourceCol")
 public void setDataSource(DataSource dataSource) {
   super.setDataSource(dataSource);
 }
}

 

 異常:Service層new Thread().start();訪問Hibernate DaO層,Hibernate報找不到Session。

Hibernate的Session是以ThreadLocal的方式在會話期間保存的,每一個線程關聯一個Session,在Service層啓動的異步線程關聯不到Hibernate Session。


JUnit測試的時候,一個Test Class下面有多個test方法的同時,有構造函數和@Before setup()方法。每次執行test方法前都會執行初始化和setup()方法。造成不同test方法是由不同的Test實例運行的,test()方法之間無法共享測試進度數據。


異常:Jackson ObjectMapper將JSON轉換爲範型對象異常:

ClassCastException: java.util.LinkedHashMap cannot be cast 

解決:List<Object> batchProtocolList = objectMapper.readValue(requestBody, new TypeReference<List<Object>>(){});

原因:不明。


異常:Jackson ObjectMapper將JSON轉換爲範型對象異常:

ClassCastException: java.util.LinkedHashMap cannot be cast 

解決:List<Object> batchProtocolList = objectMapper.readValue(requestBody, new TypeReference<List<Object>>(){});

原因:不明。

 

異常:Service接口和Service實現在同一個package下,Spring AOP doBefore方法被執行了兩次。
解決:將@Pointcut("execution(* org.aop.service.*.*(..))") 修改成@Pointcut("execution(* org.aop.service.*ServiceImpl.*(..))") ;
這樣只匹配Service實現。
原因:AOP代理同時攔截了接口中方法的調用和實現中的調用,具體原因不明。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章