SSH下使用Spring注解自动注入bean

Spring注解的使用方法详见:http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/,这里在SSH框架下做一个例子。

首先导入相关包: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

Spring公共配置 org.hibernate.dialect.Oracle10gDialect true com.entity

web.xml

mytest contextConfigLocation classpath*:/applicationContext.xml struts2 org.apache.struts2.dispatcher.FilterDispatcher actionPackages com.action struts2 /* org.springframework.web.context.ContextLoaderListener

使用Hibernate JPA定义User类:

package com.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "s_user") public class User { private Long id; private String username; private String password; @Id @GeneratedValue(strategy = GenerationType.AUTO) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name = "name") public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Column(name = "pwd") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

Dao层:

package com.dao; import com.entity.User; public interface UserDao { public void save(User user); } package com.dao.Impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Repository; import com.dao.UserDao; import com.entity.User; @Repository("userDao") public class UserDaoImpl implements UserDao { private HibernateTemplate template; private Log log = LogFactory.getLog(UserDaoImpl.class); //使用构造子注入自动注入sessionFactory @Autowired public UserDaoImpl(SessionFactory sessionFactory) { this.template = new HibernateTemplate(sessionFactory); } public void save(User user) { template.save(user); log.debug("save user:" + user.getUsername()); } }

Service层:

package com.service; import com.entity.User; public interface UserManager { public void add(User user); } package com.service.Impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.dao.UserDao; import com.entity.User; import com.service.UserManager; @Service("userManager") public class UserManagerImpl implements UserManager { //自动注入userDao,也可以使用@Resource @Autowired private UserDao userDao; private Log log = LogFactory.getLog(UserManagerImpl.class); public void add(User user) { userDao.save(user); log.debug("add User:" + user.getUsername()); } }

Action:

package com.action.convention; import org.apache.struts2.convention.annotation.Result; import org.springframework.beans.factory.annotation.Autowired; import com.entity.User; import com.opensymphony.xwork2.ActionSupport; import com.service.UserManager; @Result(name = "success", location = "hello.jsp") public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; @Autowired private UserManager userManager; public String execute() { User user = new User(); user.setUsername("cuihaiyang"); user.setPassword("abcd"); userManager.add(user); return SUCCESS; } }

调试信息如下:

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

你可能感兴趣的:(JAVA)