|
2. 工程结构
|
3. Beans.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans
<context:annotation-config /> <context:component-scan base-package="com.edu.ssh" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfi <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactory <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.edu.ssh.model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionMana <property name="sessionFactory" ref="sessionFactory" /> </bean>
<!-- 用spring AOP给service层的方法添加事务管理 --> <aop:config> <aop:pointcut id="bussinessService" expression="execution(public * com.edu.ssh.service.*.*(..))" /> <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" /> </aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="exists" read-only="true" /> <tx:method name="insert*" propagation="REQUIRED"/> </tx:attributes> </tx:advice>
</beans> |
4. UserAction.java
package com.edu.ssh.action;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationC
import com.edu.ssh.model.User; import com.edu.ssh.service.UserManager; import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L;
// 用编译器生成getter/setter方法 private int id; private String username; private String password; private String password2;
private UserManager userManager;
public UserAction() { // 直接用编码实现UserManeger的初始化 // struts2的Action暂时还没交给spring管理, //下一个工程将会实现这个功能,
ApplicationContext ctx = new
ClassPathXmlApplicationC userManager = (UserManager) ctx.getBean("userManager"); }
public UserManager getUserManager() { return userManager; }
public void setUserManager(UserManager userManager) { this.userManager = userManager; }
@Override public String execute() throws Exception { User user = new User(); user.setUsername(username); user.setPassword(password); if (userManager.exists(user)) return "fail";
userManager.insertOneUser(user); return "success"; } // 。。。。。由于代码是和之前的是一样的在这就省略}
|
5. UserActionTest.java
package com.edu.ssh.action;
import org.junit.After; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test;
public class UserActionTest {
@Test public void testExecute() { UserAction userAction = new UserAction(); userAction.setUsername("hehe"); userAction.setPassword("hehe"); try {
assertEquals("fail", userAction.execute()); } catch (Exception e) { e.printStackTrace(); } }
}
|
7. UserDAOImpl.java
package com.edu.ssh.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Component;
import com.edu.ssh.dao.UserDAO; import com.edu.ssh.model.User;
@Component public class UserDAOImpl implements UserDAO {
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; }
// 使用spring的依赖注入,默认找 名字为 hibernateTemplate 的bean @Resource public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; }
} |
8. UserManagerImpl.java
package com.edu.ssh.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.edu.ssh.dao.UserDAO; import com.edu.ssh.model.User; import com.edu.ssh.service.UserManager;
// 用Component注解,使得本类交给spring容器管理, // 并且指定bean的名字为userManager @Component("userManager") public class UserManagerImpl implements UserManager {
private UserDAO userDAO;
public UserDAO getUserDAO() { return userDAO; }
// 使用spring依赖注入 @Resource public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; }
}
|
版权声明:本文为博主原创文章,未经博主允许不得转载。