Java IOC和AOP解析

1、IOC控制反转:控制权由对象转向容器,由容器对bean对象进行控制。

2、AOP面向切面编程:把具体的类创建对应的代理类,通过代理类对具体类进行操作

AOP面向切面编程,即在不修改源代码的情况下,对原有功能进行扩展,通过代理类来对具体类进行操作。

       (Spring是通过AOP的手段达到事务控制的,具体实现是靠spring-asm.jar和cglib.jar,因为这两个jar都提供了与动态代理有关的功能,实现运行时植入新特性的功能。类似的功能也可以通过我们手工实现,如果对字节码等概念掌握比较好,可以充分利用apache bcel库进行更为细致的功能控制。)

3、Spring 是一个容器,通过spring这个容器来对对象进行管理,根据配置文件来实现spring对对象的管理。

    (spring有两种事务配置方式,一种是配置的事务管理器,另一种的是代码控制的事务
配置的事务管理器的方式是我们经常用到的经常会用到在配置文件中。代码控制的事务分为jdbc模板的和事务管理器的,jdbc默认自动提交,事务管理器的和咱们通常的一样会有commit  rollback等操作


<!--将日志类注入到bean中。-->
  <!--业务层自动提交事物配置-->
<aop:config>
   <aop:pointcut id="crudMethods" expression="execution(* com.dp.*.*.*.*(..))"/>
   <aop:pointcut id="insertMethodsSql" expression="execution(* com.dp.esdocked.impl.ESDockedService.*(..))"/>
   <aop:advisor advice-ref="txAdvice" pointcut-ref="crudMethods"/>
   <aop:advisor advice-ref="txAdviceSql" pointcut-ref="insertMethodsSql"/>
   <aop:aspect id="logAspect" ref="myLog">
      <aop:pointcut id="log" expression="execution(* com.dp.*.impl.*Service.*(..))"/><!--配置在log包下所有的类在调用之前都会被拦截-->
      <aop:after method="after" pointcut-ref="log"></aop:after>
   </aop:aspect>
   <aop:aspect id="registedAspect" ref="registedSchoolInfo">
      <aop:pointcut id="regAspect" expression="execution(* com.dp.student.impl.StudentEnrollDAO.*(..))"/>
      <aop:after method="after" pointcut-ref="regAspect"></aop:after>
   </aop:aspect>
   <aop:aspect ref="esDataHandleAspect" id="es">
      <aop:pointcut id="esDataHandlePointcut" expression="execution(* com.dp.*.impl.StudentService.*(..))"></aop:pointcut>
      <aop:after method="after" pointcut-ref="esDataHandlePointcut"></aop:after>
   </aop:aspect>
</aop:config>
 
AOP面向切面对常用的:用户日志,在原有的项目基础上就行扩展
public class UserLogAspect {
    public void setiUserLogService(IUserLogService iUserLogService) {
        this.iUserLogService = iUserLogService;
    }

    private IUserLogService iUserLogService;
   //在类里面写方法,方法名诗可以任意的。此处我用标准的before和after来表示
    //此处的JoinPoint类可以获取,action所有的相关配置信息和request等内置对象。
    public void before(JoinPoint joinpoint){
        joinpoint.getArgs();//此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
        System.out.println("被拦截方法调用之前调用此方法,输出此语句");
    }
    public void after(JoinPoint joinpoint){
        Signature signature = joinpoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        String methodStr=method.getName();
        Object[] args=joinpoint.getArgs();
        if("auditChannel".equals(methodStr)){
            String auditedStatus=args[0].toString();
            Integer channelId=Integer.valueOf(args[1].toString());
            Object content=null;
            if(args.length==3){
                content=args[2];
            }
            UserLog userLog=new UserLog();
            userLog.setResType(0);//标识操作资源为资质
            if("2".equals(auditedStatus)){
//                userLog.setContent("审核通过");
                userLog.setOperation("审核通过");
            }else if("3".equals(auditedStatus)){
                userLog.setOperation("审核不通过");
                if(content!=null){
                    userLog.setContent("意见:"+content.toString());
                }

            }

            userLog.setResId(channelId);
            Subject subject = SecurityUtils.getSubject();
            User user=(User) subject.getSession().getAttribute("user");
            userLog.setUser(user);
            userLog.setCreateTime(new Timestamp(System.currentTimeMillis()));
            iUserLogService.addUserLog(userLog);
        } else if("editChannel".equals(methodStr)){
            Channel channel=(Channel) args[0];
            Integer channelId=channel.getChannelId();
            UserLog userLog=new UserLog();
            userLog.setResType(0);//标识操作资源为资质
            String auditedStatus = channel.getAuditedStatus();
            if ("1".equals(auditedStatus)) {
                userLog.setOperation("提交审核");
            } else {
                userLog.setOperation("编辑");
            }
            userLog.setResId(channelId);
            Subject subject = SecurityUtils.getSubject();
            User user=(User) subject.getSession().getAttribute("user");
            userLog.setUser(user);
            userLog.setCreateTime(new Timestamp(System.currentTimeMillis()));
            iUserLogService.addUserLog(userLog);

        }
}
 
未完待续....... 


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值