我定义了一个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
似乎没问题,我在错误的地方感到困惑,希望有人能帮助我。