Spring建议不能按预期工作

时间:2014-02-10 01:51:44

标签: spring spring-aop

我是Spring AOP的新手,我已经创建了一个学习Spring AOP的应用程序,但是我发现Aspect没有按预期工作,这是核心代码:

目标:

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
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"/>

应用:

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();
    }
}

当我运行它时我只得到这个输出:

get user list at dao

似乎建议根本没有运行。

发生了什么事?

1 个答案:

答案 0 :(得分:2)

您应该像这样注册您的bean

<bean id="loggingAspect" class="your.package.LoggingAspect"/>