本文介紹了用于數(shù)據(jù)庫操作的Spring AOP的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我在一個Spring中工作,Hibernate項目和數(shù)據(jù)庫是Oracle。我有用于持久化相關(guān)操作的DAO層。
在我的所有表中,我有create_date和update_date列分別表示在表中插入和更新行時的時間戳。
有一個要求,每當(dāng)發(fā)生任何插入/更新操作時,我都必須更新該特定表的上述兩個時間戳列。例如,如果我的DAO層有兩個方法,假設(shè)m1和m2分別負(fù)責(zé)影響T1和T2表。現(xiàn)在,如果調(diào)用M1方法,則T1表的時間戳列將被更新。對于插入,create_date列將被更新,而對于任何更新,update_date列將被更新。
我對Spring AOP有想法,所以我想用AOP來實(shí)現(xiàn)上面的需求,但我不太確定是否可以用AOP來實(shí)現(xiàn)。
如果我可以使用AOP來滿足這個要求,請告訴我。如果可能,請向我提供如何實(shí)施它的輸入。
推薦答案
我已經(jīng)使用Spring AOP為我的應(yīng)用程序中的一個模塊實(shí)現(xiàn)了更新日期功能。
供您參考的PFB代碼
希望這將有所幫助。
我想知道是否可以對變量也有切入點(diǎn)。我知道這在Spring的方面j實(shí)現(xiàn)中可能是不可能的。但是任何解決辦法:p
**
* @author Vikas.Chowdhury
* @version $Revision$ Last changed by $Author$ on $Date$ as $Revision$
*/
@Aspect
@Component
public class UpdateDateAspect
{
@Autowired
private ISurveyService surveyService;
Integer surveyId = null;
Logger gtLogger = Logger.getLogger(this.getClass().getName());
@Pointcut("execution(* com.xyz.service.impl.*.saveSurvey*(..)))")
public void updateDate()
{
}
@Around("updateDate()")
public Object myAspect(final ProceedingJoinPoint pjp)
{
// retrieve the runtime method arguments (dynamic)
Object returnVal = null;
for (final Object argument : pjp.getArgs())
{
if (argument instanceof SurveyHelper)
{
SurveyHelper surveyHelper = (SurveyHelper) argument;
surveyId = surveyHelper.getSurveyId();
}
}
try
{
returnVal = pjp.proceed();
}
catch (Throwable e)
{
gtLogger.debug("Unable to use JointPoint :(");
}
return returnVal;
}
@After("updateDate()")
public void updateSurveyDateBySurveyId() throws Exception
{
if (surveyId != null)
{
surveyService.updateSurveyDateBySurveyId(surveyId);
}
}
}
這篇關(guān)于用于數(shù)據(jù)庫操作的Spring AOP的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,






