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
?
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <description>Spring公共配置 </description> <!-- 配置数据源 --> <bean id="dataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:cui"/> <property name="username" value="cui"/> <property name="password" value="cui"/> </bean> <!-- 配置sessionFactory --> <bean id="sessionFactory" ref="dataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.entity</value> </list> </property> </bean> <!-- 事务管理 --> <bean id="transactionManager" ref="sessionFactory"></property> </bean> <!-- 使用annotation自动注入bean,并启动相关处理注解的进程 --> <context:component-scan base-package="com"> <context:include-filter type="regex" expression="com/.dao.*"/> <!-- 正则表达式必须格式正确,否则无效。以下是无效的示例 <context:exclude-filter type="regex" expression="/.service/..*"/> <context:exclude-filter type="regex" expression="com/.service*"/> <context:exclude-filter type="regex" expression=".service*"/> --> <!-- 正确格式:从base-package开始 <context:exclude-filter type="regex" expression="com/.service.*"/> <context:exclude-filter type="regex" expression="com/.service/..*"/> --> </context:component-scan></beans>?
web.xml
?
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>mytest</display-name><!-- 指定spring配置文件的位置 --><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/applicationContext.xml</param-value> </context-param><!-- Struts 2 --><filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class><init-param> <param-name>actionPackages</param-name> <param-value>com.action</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 自动加载applicationContext --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>?
使用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
?
转自http://blog.csdn.net/cuihaiyang/article/details/6238257#