문제

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