spring注入问题

之前模拟了下spring的aop动态代理,出现了无法注入的问题,导致空指针异常
TransactionManager自定义事务管理器
@Component(value = “tm”)
public class TransactionManager {

//开启事物
@Autowired
private ConnnectionUntils connnectionUntils;
public void start() {
    try {
        //获取连接处已经对事务做了处理
        Connection conn = connnectionUntils.getConnecting();
        conn.setAutoCommit(false);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException();
    }
}
//提交事务
public void conmit(){
    try {
        //获取连接处已经对事务做了处理
        Connection conn = connnectionUntils.getConnecting();
        conn.commit();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException();
    }
}
//回滚事务
public void rollback(){
    try {
        //获取连接处已经对事务做了处理
        Connection conn = connnectionUntils.getConnecting();
        conn.rollback();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException();
    }
}
//释放资源
public void exit(){
    try {
        //获取连接处已经对事务做了处理
        Connection conn = connnectionUntils.getConnecting();
        conn.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException();
    }
}

}

ProxyUntis动态代理增强service层的所有方法
@Component()
public class ProxyUntis {

@Resource(name = "tm") 此处注入
private TransactionManager tm;

/**
 * @param obj 传入的接口,为这个接口的子类增强
 * @return
 */
public Object Proxytest(Object obj) {
    Object proxy = null;    //deBug 为空值
    try {
        proxy = Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object invoke=null;
                 //传入被代理对象
                try {
                    //开启事务    此处出现空指针异常
                    ****tm.start(); 此处出现空指针异常****
                    
                    invoke = method.invoke(obj, args);
                    tm.conmit();

                } catch (Exception e) {
                    e.printStackTrace();
                    tm.rollback();
                    throw new RuntimeException("方法内部的异常");
                } finally {
                    tm.exit();
                }
                return invoke;
            }
        });

    } catch (Exception e) {
        e.printStackTrace();
    }
    return proxy;
}

}

service层
public interface IAccountService {

void save(Account account);

void update(Account account);

void delete(Integer accountId);

Account findById(Integer accountId);

List<Account> findAll();

//转账
void tarnsfer(String soucename, String targetname, Float money);

}

至于dao层数据源测试都没有问题,pom等都没有问题

测试
@Test
public void Proxytest() {
ProxyUntis untis = new ProxyUntis();
//执行动态代理
IAccountService proxy = (IAccountService) untis.Proxytest(is);//此行出现异常
List all = proxy.findAll();
for (Account account : all) {
System.out.println(account);
}
}
@找不到解决的原因,希望大神指教

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值