Spring AOP传递参数的问题
舞黯然 2010-06-02 07:39:15 我的通知中需要用到被通知对象的参数,如何传递这个参数呢?我使用arg-names传递参数但是报错了。
代码如下
这是被通知对象
public boolean SaveNewBlog(int Id, Blog blog) {
boolean b = false;
try {
java.util.Date date = new java.util.Date();
blog.setUserId(Id);
blog.setBlogDay(new java.sql.Date(date.getTime()));
blog.setBlogTime(new java.sql.Time(date.getTime()));
template.save(blog);
b = true;
} catch (Exception e) {
e.printStackTrace();
b = false;
} finally {
return b;
}
}
这是通知
public void AfterSaveBlog(int Id) throws Throwable {
java.util.Date d = new java.util.Date();
UserAction ua=new UserAction();
ua.setUsersId(Id);
ua.setActionType("blog");
ua.setActionDay(new java.sql.Date(d.getTime()));
ua.setActionTime(new java.sql.Time(d.getTime()));
template.save(ua);
}
我的applicationContext.xml中关于切面的配置如下
<aop:config>
<aop:advisor advice-ref="tx-advice" pointcut-ref="SaveBlogPointcut" />
<aop:aspect id="SaveBlogAspect" ref="aopManager">
<aop:pointcut id="SaveBlogPointcut"
expression="execution(* BLOG.DAO.Impl..*.*(int,..)) and args(Id,..)" />
<aop:after-returning method="AfterSaveBlog" pointcut-ref="SaveBlogPointcut" arg-names="Id"/>
</aop:aspect>
</aop:config>
测试方法如下:
public void testSaveNewBlog() {
BlogDao blogdao=new BlogDaoImpl();
Blog blog = new Blog();
java.util.Date d = new java.util.Date();
blog.setUserId(1);
blog.setBlogTitle("111");
blog.setBlogText("222");
blog.setBlogDay(new java.sql.Date(d.getTime()));
blog.setBlogTime(new java.sql.Time(d.getTime()));
blogdao.SaveNewBlog(1, blog);
}