Вопрос

I am new in Spring AOP, and I have create an application to learn the Spring AOP, however I found that the Aspect does not work as expected, this is the core code:

Target:

public interface UserService {
    void getUserList();
}

@Component
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void getUserList() {
        this.userDao.getUserList();
    }
}

Aspect:

@Aspect
public class LoggingAspect {

    @Pointcut("execution(* com.kk.service.impl.UserServiceImpl(..))")
    public void logTargetUserService() {
    }

    @Before("execution(* com.kk.service.impl.UserServiceImpl.getUserList(..))")
    public void logBefore(JoinPoint joinPoint) {

        System.out.println("logBefore() is running!");
        System.out.println("hijacked : " + joinPoint.getSignature().getName());
        System.out.println("******");
    }
}

spring.xml:

<context:component-scan base-package="com.kk"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>

Application:

public class Application {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});

        UserServiceImpl userService = context.getBean(UserServiceImpl.class);
        userService.getUserList();
    }
}

When I run it I only got this output:

get user list at dao

It seems that the advise does not run at all.

What is going on?

Это было полезно?

Решение

You should regist your bean as like this

<bean id="loggingAspect" class="your.package.LoggingAspect"/>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top