spring @resource注入NullPointerException

时间:2015-12-11 07:56:26

标签: java spring inversion-of-control aop

我定义了一个Aspect类RedisAspect.java

 @Aspect
public class RedisAspect implements IAspect {

    private static String INTERFACE_COUNT = "interfaces";
    @Resource(name = "RedisDao")
    private IRedisDao redisDao;

    @Pointcut("execution(* com.xxx.web.service.xxxWebImpl.*.*(..))")
    public void servicePoint() {
    }

    /**
     * count all interfaces
     * 
     * @throws Exception
     */
    @Override
    @Before("servicePoint()")
    public void doBefor(JoinPoint point) throws Exception {
        String interfaceName = point.getSignature().getName();
        System.out.println("interfaceName : " + interfaceName);
        redisDao.addInterfaceCount(interfaceName);
    }

    @Override
    @After("servicePoint()")
    public void doAfter(JoinPoint point) {
        System.out.println("do After");
    }

    @Override
    public void doAterThrowing(Exception ex) {
        // TODO Auto-generated method stub

    }

}

以下是RedisDaoImpl.java:

@Component("RedisDao")
public class RedisDaoImpl extends AbstractBaseRedisDao<String, Integer> implements IRedisDao {

    private static String INTERFACES = "interfaces";
    @Override
    public void addInterfaceCount(final String index) throws Exception {
        redisTemplate.execute(new RedisCallback<Object>() {

            @Override
            public Object doInRedis(RedisConnection connection)
                    throws DataAccessException {
                connection.hIncrBy(INTERFACES.getBytes(), index.getBytes(), 1);
                return null;
            }
        });
    }

    public void delete(String index) {
        redisTemplate.delete(index);
    }


    @Override
    public void addFailedRequest(String index) throws Exception {
    }
}

然后是xml配置

<bean id="redisAspect" class="com.xxx.web.aspect.RedisAspect"/>
<context:component-scan base-package="com.xxx.web.*" />
<aop:aspectj-autoproxy proxy-target-class="true" />
<mvc:annotation-driven />
<mvc:default-servlet-handler />

现在在调用login方法(在com.xxx.web.service.xxxWebImpl.UserServiceImpl.java中)之前,将执行doBefor方法,但它向我显示redisDao为null。但是当我尝试直接调用时服务中的redisDao似乎没问题,我在错误的地方感到困惑,希望有人能帮助我。

0 个答案:

没有答案
相关问题