1. 程式人生 > >SSH下使用Spring註解自動注入bean

SSH下使用Spring註解自動注入bean

首先匯入相關包:spring-beans-3.0.4.RELEASE.jar(org.springframework.beans.factory.annotation.Autowired用來注入bean)、spring-context-3.0.4.RELEASE.jar(org.springframework.stereotype.Componet 、Service、Repository等用來定義bean)。

其次需要新增相關配置:applicationContext.xml

web.xml

使用Hibernate JPA定義User類:

Dao層:

Service層:

Action:

除錯資訊如下:

Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into s_user (pwd, name, id) values (?, ?, ?)
2011-03-10 19:44:25,296 [http-8080-1] DEBUG [com.dao.Impl.UserDaoImpl] - save user:cuihaiyang
2011-03-10 19:44:25,296 [http-8080-1] DEBUG [com.service.Impl.UserManagerImpl] - add User:cuihaiyang