编程那点事编程那点事

专注编程入门及提高
探究程序员职业规划之道!

spring快速入门例子教程:04业务层

在本spring快速入门例子中,业务层仅有一个业务类,即UserService。UserService负责将持久层的UserDao和LoginLogDao组织起来完成用户、密码认证以及登录日志记录等操作。

UserService

UserService业务接口有3个业务方法:

hasMatchUser():用于检查用户名、密码的正确性

findUserByUserName():以用户名为条件加载User对象

loginSuccess():用户登录成功后调用,更新用户最后登录时间和IP信息记录,并且新增5个积分。

以下是UserService详细代码

@Service
public class UserService {
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private LoginLogDao loginLogDao;
	
	public boolean hasMatchUser(String userName, String password) {
		int matchCount =userDao.getMatchCount(userName, password,2);
		return matchCount > 0;
	}
	
	public User findUserByUserName(String userName) {
		return userDao.findUserByUserName(userName,2);
	}
	
	public void loginSuccess(User user) {
		user.setCredits( 5 + user.getCredits(),2);
		LoginLog loginLog = new LoginLog(,2);
		loginLog.setUserId(user.getUserId(),2);
		loginLog.setIp(user.getLastIp(),2);
		loginLog.setLoginDate(user.getLastVisit(),2);
        userDao.updateLoginInfo(user,2);
        loginLogDao.insertLoginLog(loginLog,2);
	}
}

在loginSuccess方法中,我们将2个DAO组织起来共同完成一个事务性的数据操作:更新t_user表记录并添加t_login_log表记录。但从UserService中却看不到任何事务操作的影子,这正是通过Spring声明式事务配置带来的好处,以后我们会详细的讲解如何赋予业务类事务管理的能力。

在Spring中装配Service

事务管理的代码虽然不用出现在程序代码中,但我们必须以某种方式告诉Spring哪些业务类需要工作与事务环境下以及事务的规则等内容,以便Spring根据这些信息自动为目标业务类添加事务管理的功能。applicationContent.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:p="http://www.springframework.org/schema/p"

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 

       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

       http://www.springframework.org/schema/context 

       http://www.springframework.org/schema/context/spring-context-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/aop

       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    

    <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->

    <context:component-scan base-package="com.bbs.dao"/>

    <context:component-scan base-package="com.bbs.service"/>

    

    <!-- 配置数据源 -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close" 

p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"

p:url="jdbc:sqlserver://localhost:1433;DatabaseName=TerrenceCRM;SelectMethod=Cursor" 

p:username="sa"

p:password="123" />


<!-- 配置Jdbc模板  -->

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"

p:dataSource-ref="dataSource" />

<!-- 配置事务管理器 -->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dataSource" />

<!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->

<aop:config proxy-target-class="true">

<aop:pointcut id="serviceMethod"

expression=" execution(* com.bbs.service..*(..))" />

<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />

</aop:config>

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="*" />

</tx:attributes>

</tx:advice>

</beans>

需要注意的是需要引入aop及tx命名空间所对应的Schema文件

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

http://www.springframework.org/schema/tx 

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

这样就完成了业务层的程序开发和配置工作,接下来,需要对该业务类进行简单的单元测试以便检验业务方法的正确性

未经允许不得转载: 技术文章 » Java编程 » spring快速入门例子教程:04业务层