2012-05-31 33 views
0

在我的Spring MVC +休眠+標註的項目,我有這三類需要了解spring mvc項目中的autowiring bean嗎?

UserServiceImpl.java

@Service("userService") 
public class UserServiceImpl implements UserService { 
@Autowired 
private UserDAO userDAO; 
//other codes 
} 

UserDAOImpl.java

@Repository("userDAO") 
public class UserDAOImpl implements UserDAO { 
@Autowired 
private SessionFactory sessionFactory; 
//other codes 
} 

RegistrationController.java

@Controller 
@RequestMapping("/registration.htm") 
public class RegistrationController { 
@Autowired 
private UserService userService; 
//other codes 
} 

在我調度-servlet.xml中我加入以下

<context:annotation-config /> 
<context:component-scan base-package="com.alw.controllers,com.alw.DAOs,com.alw.services" /> 

<bean id="sessionFactory" 
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 

當我跑我得到了以下的例外項目:

Error creating bean with name 'registrationController': 
Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private com.alw.services.UserService 
    com.alw.controllers.RegistrationController.userService; 

AND

Error creating bean with name 'sessionFactory' defined in ServletContext resource 
    [/WEB-INF/dispatcher-servlet.xml]: 
Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: 
    org/apache/commons/pool/impl/GenericObjectPool 

可能有人指出我在哪裏缺失?
今天已經過去了一整天。

編輯:
我添加了commons.pool但沒有結果。
我有這些例外。

Error creating bean with name 'registrationController': 
Error creating bean with name 'userService': 
Error creating bean with name 'userDAO': 
Error creating bean with name 'sessionFactory' defined in ServletContext 
    resource [/WEB-INF/dispatcher-servlet.xml]: 
Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: 
Could not initialize class org.hibernate.cfg.AnnotationConfiguration 

感謝....

+2

關掉你的CAPS LOCK!它看起來很像你!它被認爲是在互聯網上的RudE! – duffymo

回答

0

正如duffymo指出的那樣,您從類路徑中缺少commons pool。至於另一個問題,當你說你得到錯誤1 錯誤2,你的意思是你在不同時間得到兩個不相關的錯誤,或者你得到錯誤1 錯誤2由於錯誤2.如果你看到他們都在同一時間在日誌中,他們可能是同一件事,其中第二個是第一個的原因。

另一方面,您正在犯的錯誤是將所有服務bean放入屬於DispatcherServlet的上下文中。如果你還在根上下文中聲明bean,那也可能給你帶來問題。看到這個對方的回答和與其相關聯的回答,瞭解在Spring MVC應用程序根和子上下文之間的區別:

Why DispatcherServlet creates another application context?

特別:

Spring-MVC: What are a "context" and "namespace"?

+0

嘿瑞恩我已經完全陷入這個問題,所以只是忘了這個。我曾訪問過的每個站點有關使用註釋自動裝配bean的示例發現代碼存在一些錯誤。所以,請您給我建議任何網站或書籍,我可以清楚瞭解相同的情況。這將非常有用。謝謝... – mukund

+0

Spring以其[參考指南](http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/)的形式提供了極好的文檔。 [Spring發行版](http://www.springsource.org/download)附帶了許多優秀的示例,[SpringSource博客](http://blog.springsource.org/)充滿了大量的代碼示例,太。至於書籍,請特別檢查[Manning](http://www.manning.com/),[O'Reilly](http://oreilly.com/)和[Pragmatic Bookshelf](http:// pragprog的.com /)。 –

0

第二個很簡單:你錯過了包含在CLASSPATH org.apache.commons.pool.impl.GenericObjectPool的JAR。

問題是:哪個類裝入器? Java EE應用服務器具有類加載器的層次結構。我猜你想把JAR添加到你的server/lib目錄下,這樣在連接池設置時就可以使用了。

第一個不清楚。嘗試改變你的基礎包到「com.alw」,看看是否這樣排序。

相關問題