我一直在用 java+maven+spring+hibernate 开发一个项目,我想在调用 saveorupdate 之前自动将当前日期分配给 POJO。我不介意new Date()
为所有类的所有 date_created 创建,但它们已经足够了。我刚刚发现 spring aop 擅长这些事情。
在谷歌上几分钟后,我找到了一个很好的方法。但到目前为止,我无法看到我如何准确地将新日期分配给 POJO,它们是我的介绍接口的实现类。我真的不知道如何把它说得通。所以根据我的理解,我会这样做:
//DateSetter.java
package org.personal.myproject.model;
import java.util.Date;
//this is how i'm getting the current date
public interface DateSetter {
public Date getCurrentDate();
}
//DateCreatedDateSetterImpl.java
package org.personal.myproject.model;
import java.util.Date;
// the implementation
public class DateCreatedDateSetterImpl implements DateSetter {
public Date getCurrentDate() {
return new Date();
}
}
//DateIntroduction.java
package org.personal.myproject.model;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.DeclareParents;
@Aspect
public class DateIntroduction {
//making all the model implementors of the DateSetter interface
@DeclareParents(value="org.personal.myproject.model.*",
defaultImpl=DateCreatedDateSetterImpll.class)
public DateSetter dateSetter;
/**here is to add my "before trigger" for every calling of every method.
if you can see i have the daos in 2 packages, i don't know whether is wrong or not.i'm actually using the generic Dao so should i use that interface instead?
**/
@Before("execution * org.personal.myproject.dao.hibernate.*.(..) || * org.personal.myproject.dao.hibernate.*.*.(..)" + "&& this(dateSetter)")
public void injectLastModifiedDate(DateSetter dateSetter){
/**so here i'm expecting to inject the new date but apparently i'm missing something**/
}
}
谁能告诉我我在这里做错了什么?或者这只是他实现我想做的错误的方法。感谢阅读