Java

[Effective Java] ์•„์ดํ…œ 39. ๋ช…๋ช… ํŒจํ„ด๋ณด๋‹ค ์• ๋„ˆํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ผ.

quedevel 2023. 3. 21. 10:48
728x90
๋ฐ˜์‘ํ˜•

๐ŸŽฏ ์•„์ดํ…œ 39. ๋ช…๋ช… ํŒจํ„ด๋ณด๋‹ค ์• ๋„ˆํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ผ.

๋ช…๋ช… ํŒจํ„ด์˜ ์ด์Šˆ

  1. ์˜คํƒ€๊ฐ€ ๋‚˜๋ฉด ์•ˆ๋œ๋‹ค.
  2. ์˜ฌ๋ฐ”๋ฅธ ํ”„๋กœ๊ทธ๋žจ ์š”์†Œ์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋ฆฌ๋ผ ๋ณด์ฆ ํ•  ๋ฐฉ๋ฒ•์ด ์—†๋‹ค.
  3. ํ”„๋กœ๊ทธ๋žจ ์š”์†Œ๋ฅผ ๋ฐฐ๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•  ๋งˆ๋•…ํ•œ ๋ฐฉ๋ฒ•์ด ์—†๋‹ค.

์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์• ๋„ˆํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์ž!

@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
๋ฐ˜์‘ํ˜•