728x90
๋ฐ์ํ
๐ฏ ์์ดํ 39. ๋ช ๋ช ํจํด๋ณด๋ค ์ ๋ํ ์ด์ ์ ์ฌ์ฉํ๋ผ.
๋ช ๋ช ํจํด์ ์ด์
- ์คํ๊ฐ ๋๋ฉด ์๋๋ค.
- ์ฌ๋ฐ๋ฅธ ํ๋ก๊ทธ๋จ ์์์์๋ง ์ฌ์ฉ๋๋ฆฌ๋ผ ๋ณด์ฆ ํ ๋ฐฉ๋ฒ์ด ์๋ค.
- ํ๋ก๊ทธ๋จ ์์๋ฅผ ๋ฐฐ๊ฐ๋ณ์๋ก ์ ๋ฌํ ๋ง๋
ํ ๋ฐฉ๋ฒ์ด ์๋ค.
์ด๋ฌํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ ๋ํ
์ด์
์ ์ฌ์ฉํ์!
@Aspect
@Component
public class SessionAspect {
private static final String VO_SETTING_EXPRESSION = "execution(* com.inno.backoffice..*Mapper.insert*(..))"
+ " || execution(* com.inno.backoffice..*Mapper.update*(..))"
+ " || execution(* com.inno.backoffice..*Mapper.delete*(..))"
+ " || execution(* com.inno.common..*Mapper.insert*(..))"
+ " || execution(* com.inno.common..*Mapper.update*(..))"
+ " || execution(* com.inno.common..*Mapper.delete*(..))";
@Before(VO_SETTING_EXPRESSION)
public void setVO(JoinPoint joinPoint) {
Object[] objects = joinPoint.getArgs();
if(SecurityContextHolder.getContext().getAuthentication()!= null) {
InnoUser user = (InnoUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
AdminVO vo = user.getAdminVO();
for (Object o : objects) {
Method[] methods = o.getClass().getMethods();
Optional<Method> setRegDtime = Arrays.stream(methods).filter(e -> e.getName().equals("setRegDate")).findFirst();
Optional<Method> setModDtime = Arrays.stream(methods).filter(e -> e.getName().equals("setModDate")).findFirst();
Optional<Method> setRegSn = Arrays.stream(methods).filter(e -> e.getName().equals("setRegSn")).findFirst();
Optional<Method> setModSn = Arrays.stream(methods).filter(e -> e.getName().equals("setModSn")).findFirst();
Optional<Method> getRegSn = Arrays.stream(methods).filter(e -> e.getName().equals("getRegSn")).findFirst();
Optional<Method> getModSn = Arrays.stream(methods).filter(e -> e.getName().equals("getModSn")).findFirst();
Date now = new Date();
try {
if (joinPoint.getSignature().getName().startsWith("insert")) {
if (setRegDtime.isPresent()) {
setRegDtime.get().invoke(o, now);
}
if (setRegSn.isPresent()) {
if (CommonConstants.EMPTY.getValue().equals(StringUtil.null2void((String) getRegSn.get().invoke(o)))) {
setRegSn.get().invoke(o, vo.getAdminSn());
}
}
}
if (setModDtime.isPresent()) {
setModDtime.get().invoke(o, now);
}
if (setModSn.isPresent()) {
if (CommonConstants.EMPTY.getValue().equals(StringUtil.null2void((String) getModSn.get().invoke(o)))) {
setModSn.get().invoke(o, vo.getAdminSn());
}
}
} catch (Exception e) {
//todo
}
}
}
}
}
์ ์์ค๋ ๋ด๊ฐ AOP๋ฅผ ํ์ฉํ์ฌ ๋ฑ๋ก์, ๋ฑ๋ก๋ ์ง, ์์ ์, ์์ ๋ ์ง๋ฅผ setํด์ฃผ๋ ๊ธฐ๋ฅ์ด๋ค.
ํ์ง๋ง ์ ์์ค์ VO_SETTING_EXPRESSION
์ ํตํด ๋ช
๋ช
ํจํด์ ์ฌ์ฉํ์๊ธฐ ๋๋ฌธ์ ์์ ์ค๋ช
ํ ์ด์๋ค์ ๊ฐ๊ณ ์๋ค.
๋ฐ๋ผ์, @Before("@annotation(์ปค์คํ
์ ๋ํ
์ด์
)")
์ ์ฌ์ฉํ์ฌ ๋ช
๋ช
ํจํด์ ํํผํด์ผ ๋ ๊ฒ ๊ฐ๋ค.
์ฐธ๊ณ ์๋ฃ
Joshua Bloch, ใEffective Java 3/Eใ, ๊ฐ์๋งต์ ์ฎ๊น, ํ๋ก๊ทธ๋๋ฐ์ธ์ฌ์ดํธ(2018)
http://www.kyobobook.co.kr/product/detailViewKor.laf?ejkGb=KOR&mallGb=KOR&barcode=9788966262281&orderClick=LEa&Kc=
728x90
๋ฐ์ํ