SpringAOP的MethodBeforeAdvice没有起作用,帮忙看一下。
本帖最后由 qingchen191 于 2013-09-07 18:14:17 编辑 刚开始学习Spring,按照教程,做了一个简单的SpringAOP前置通知注入,但是前置通知没有显示,不知道哪里出了问题,现在把主要的代码贴出来,大家帮忙看一下哪里出了问题。非常感谢。
前置通知类:LogAdvice
public class LogAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
// TODO Auto-generated method stub
String targetClassName = target.getClass().getName();
String targetMethodName = method.getName();
String logInfoText="前置通知:" + targetClassName + "类的"+targetMethodName+"方法执行";
System.out.println(logInfoText);
}
}
public interface UserDAO {
public boolean login(String username, String password);
public void addUser(String username, String password);
public void delUser(int id);
}
public class UserDAOImpl implements UserDAO {
@Override
public boolean login(String username, String password) {
// TODO Auto-generated method stub
if (username.equals("admin") && password.equals("123")) {
return true;
}
return false;
}
@Override
public void addUser(String username, String password) {
// TODO Auto-generated method stub
System.out.println(username + "用户添加成功!");
}
@Override
public void delUser(int id) {
// TODO Auto-generated method stub
System.out.println("编号为" + id + "的用户被删除!");
}
}
public interface UserBiz {
public boolean login(String username, String password);
public void addUser(String username, String password);
public void delUser(int id);
}
public class UserBizImpl implements UserBiz {
UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public boolean login(String username, String password) {
// TODO Auto-generated method stub
return userDAO.login(username, password);
}
@Override
public void addUser(String username, String password) {
// TODO Auto-generated method stub
userDAO.addUser(username, password);
}
@Override
public void delUser(int id) {
// TODO Auto-generated method stub
userDAO.delUser(id);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="userDAO" class="com.springtest1.dao.UserDAOImpl"></bean>
<bean id="userBiz" class="com.springtest1.biz.UserBizImpl">
<property name="userDAO" ref="userDAO" />
</bean>
<bean id="logAdvice" class="com.springtest1.aop.LogAdvice"></bean>
<bean id="ub" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.springtest1.biz.UserBiz">
</property>
<property name="target" ref="userBiz"></property>
<property name="interceptorNames">
<list>
<value>logAdvice</value>
</list>
</property>
</bean>
</beans>
public class TestAdvice {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
UserBiz userBiz = (UserBiz) context.getBean("userBiz");
userBiz.addUser("zhangsan", "123");
userBiz.delUser(2);
}
}
[解决办法]
context.getBean("userBiz"); 改成context.getBean("ub");试试