ITPub博客

首页 > Linux操作系统 > Linux操作系统 > spring2.5事务&aop (2)

spring2.5事务&aop (2)

原创 Linux操作系统 作者:seablue_xj 时间:2008-05-16 23:42:30 0 删除 编辑

开发dao层:

接口:

public interface FooDAO {

         abstract public void insert(Customer customer);

}

 

实现类:

public class FooDAOImp implements FooDAO {

 

         DataSource dataSource;

 

         public DataSource getDataSource() {

                   return dataSource;

         }

 

         public void setDataSource(DataSource dataSource) {

                   this.dataSource = dataSource;

         }

 

         @Override

         public void insert(Customer customer) {

                   // TODO Auto-generated method stub

                   String sql="insert into customer (username,password ) values(:username,:password)";

                  

                  

                   NamedParameterJdbcTemplate template=new NamedParameterJdbcTemplate(this.getDataSource());

                   Map namedParameters =new HashMap();

                   namedParameters.put("username", customer.getUsername());

                   namedParameters.put("password", customer.getPasswrod());

                  

                   template.update(sql, namedParameters);

        

         }

 

}

 

配置该实现类:

<bean id="fooDAO" class="springTransaction.dao.FooDAOImp"

       parent="daoTemplate">

bean>

此类没有使用JdbcTemplate,因此需要将dataSource注入,实现dataSourcesetter..(..) getter..()方法。

 

 

开发业务层:

业务层接口:

public interface FooService {

        

         public abstract void saveCustomer(Customer customer);

         public abstract void deleteCustomer(Customer customer);

         public List getALl();

         public Customer getCustomerById(Integer id);

        

}

 

业务实现:

public class FooServiceImp implements FooService {

 

         FooDAO fooDAO;

 

         public FooDAO getFooDAO() {

                   return fooDAO;

         }

 

         public void setFooDAO(FooDAO fooDAO) {

                   this.fooDAO = fooDAO;

         }

 

         @Override

         public void deleteCustomer(Customer customer) {

                   // TODO Auto-generated method stub

 

         }

 

         @Override

         public List getALl() {

                   // TODO Auto-generated method stub

                   return null;

         }

 

         @Override

         public Customer getCustomerById(Integer id) {

                   // TODO Auto-generated method stub

                   return null;

         }

 

         @Override

         public void saveCustomer(Customer customer) {

                   // TODO Auto-generated method stub

                   this.getFooDAO().insert(customer);

         }

}

 

业务层里没有什么特殊的方法,因为需要开启事务,方便事务正常的回滚,以及对异常的捕获,因此,不进行异常的处理。

 

FooServiceImp的配置:

<bean id="fooService"

       class="springTransaction.service.FooServiceImp">

       <property name="fooDAO">

           <ref local="fooDAO" />

       property>

bean>

 

因为需要使用到切面,所以创建切面文件,在切面文件中,分别配置环绕拦截AroundProceeding和异常体拦截afterThrowing

public class Aspect {

        

         private Logger logger=new Logger();

        

         public Object AroundProceeding(ProceedingJoinPoint point) throws Throwable

         {

                   Object returnValue ;

                   StopWatch clock =new StopWatch(this.getClass().getName());

                   try{

                            logger.entry_info(point.getArgs()[0] + " run Starting "

                                               + point.getSignature().getName() + " method");

                            clock.start(point.toShortString());

                            returnValue=point.proceed();

                   }finally{

                            clock.stop();

                            logger.entry_info(point.getArgs()[0] + " run "

                                               + point.getSignature().getName() + " method end");

                            logger.entry_info("run " + point.getSignature().getName()

                                               + " and run method in " + clock.prettyPrint());

                   }

                   return returnValue;

         }

        

        

         public void afterThrowing(JoinPoint point,Throwable ex)

         {

                   String aa = point.getSignature().getName();

                   logger.entry_error(point.getArgs()[0] + " run " + aa + " Throw...." + ex);

         }

 

}

Aspect.javaspring声明

 

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7528999/viewspace-274388/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-12-21

  • 博文量
    10
  • 访问量
    9926