简化spring--减少不必要的基于接口编程

 

简化spring--减少不必要的基于接口编程

 

众所周知,Spring是提倡基于接口编程的。
   
但也有些Service类,比如SaleOrderManager SaleOrderManagerImpl,只有5%的机会再有另一个Impl实现。95%时间里这两兄弟站一起,就像C++里的.h.cpp,徒增维护的繁琐,所以刚开始用Spring的时候就想去掉它,今天偶然在群里聊起动态代理,算是搞明白了其中的道理,谢谢Sparkle :)  
    IOC
是不会强制基于接口的,是这些Service类一般要使用Spring的声明式事务机制,而声明式的事务机制是使用Spring AOP来实现的。
   
再看Spring AOP,混合使用了Java的动态代理和CGLib2,因为:
    Java
的动态代理是AOP的理想方式,但只能用于接口。如果基于Class的时候,Spring AOP就会用CGLib生成目标类的子类来实现AOP,相当现实。
   
所以,我们只要指定使用声明式事务bean使用CGLib来实现AOP,就可以不基于接口编程了。
   
指定的方式是设置proxyTargetClasstrueeg.

1.

     <bean id="baseService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
        abstract="true">
        <property name="transactionManager">
            <ref bean="transactionManager"/>
        </property>
        <property name="proxyTargetClass">
                    <value>true</value>
        </property>
      ....
      </bean>

     又讨论了一下这样的效率问题, 因为这些Service Bean都是单例,效率应该不受影响。

 

2.

package com.rx.spring.cglib;
public class UserManagerImpl { // implements UserManager {
  public void addUser(String username, String password) {
    System.out.println("-------UserManagerImpl.addUser()----------");
  }
  public void deleteUser(int id) {
    System.out.println("-------UserManagerImpl.deleteUser()----------");
  }
}
package com.rx.spring.cglib;
import org.aspectj.lang.JoinPoint;
public class SecurityHandler {
  private void checkSecurity(JoinPoint joinPoint) {
    Object[] args = joinPoint.getArgs();
    for (int i=0; i<args.length; i++) {
      System.out.println(args[i]);
    }
    System.out.println(joinPoint.getSignature().getName());
    System.out.println("----------checkSecurity()---------------");
  }
}
package com.rx.spring.cglib;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
  public static void main(String[] args) {
    BeanFactory factory = new ClassPathXmlApplicationContext("applicationContextCglib.xml");
    UserManagerImpl userManager = (UserManagerImpl)factory.getBean("userManager");
    userManager.addUser("abc", "123");
    System.out.println("testing");
    userManager.deleteUser(1);
  }
}
<?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: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-2.0.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  <!-- 
  <aop:aspectj-autoproxy proxy-target-class="true"/>
   -->
  <bean id="securityHandler" class="com.rx.spring.cglib.SecurityHandler"/>      
  <bean id="userManager" class="com.rx.spring.cglib.UserManagerImpl"/>
  <aop:config>
    <aop:aspect id="security" ref="securityHandler">
      <aop:pointcut id="allAddMethod" expression="execution(* com.rx.spring.cglib.UserManagerImpl.add*(..))"/>
      <aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
    </aop:aspect>
  </aop:config>  
</beans>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值