기본 콘텐츠로 건너뛰기

[JAVA][Interface 2일차] #1

Spring frame

 클래스가 많아지면 복잡해진다. 객체지향 프로그래밍의 근본적인 한계. 극복하기 위해 나온 것이 Spring

POJO

---

Spring Container

---

JVM


IoC

IoC를 이용해서 Tight coupling이 아닌 Loose coupling으로 연결.

A 또는 B를 생성하는 것이 아니라 만든 것을 주입한다.


DI 방법 

 - 컨스트럭터

 - p방법

 - Setter 방법


리뷰

 - 어노테이션 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.component;
 
import java.util.ArrayList;
 
import javax.annotation.Resource;
 
import com.frame.Dao;
import com.frame.Search;
import com.frame.Service;
import com.vo.Item;
@org.springframework.stereotype.Service("itemservice") // Component라고 써되 되지만, Service라고 따로 명시
public class ItemService implements Service<String, Item>,
Search<Integer, Integer, Item>{
    @Resource(name="itemdao")
    Dao dao;
    
    @Resource(name="itemdao")
    Search search;
    
    public void setDao(Dao dao) {
        this.dao = dao;
    }
    public ItemService() {        
    }
    
    public ItemService(Dao dao) {
        this.dao = dao;
    }
 
    @Override
    public void register(Item v) {
        dao.update(v);
        
    }
 
    @Override
    public void remove(String k) {
        dao.delete(k);
        
    }
 
    @Override
    public void modify(Item v) {
        dao.update(v);
        
    }
 
    @Override
    public Item get(String k) {
        
        return (Item) dao.select(k);
    }
 
    @Override
    public ArrayList<Item> get() {
 
        return dao.select();
    }
    @Override
    public ArrayList<Item> search(Integer k) {
        return search.search(k);
    }
    @Override
    public ArrayList<Item> search(Integer k1, Integer k2) {        
        return search.search(k1,k2);
    }
 
    
 
}
 
cs

프로젝트시

 - 어노테이션이면 어노테이션, XML이면 XML으로 일관성 있게 할 것.

인터페이스 부분

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.frame;
 
import java.util.ArrayList;
 
public interface Dao<K, V> {
    public abstract void insert(V v);
    public abstract void update(V v);
    public abstract void delete(K k);
    public abstract V select(K k);
    public abstract ArrayList<V> select();
 
}
 
cs

인터페이스 구현 방법
1. 두개의 객체가 있다고 할때, 하나의 객체에만 사용되는 기능도 우선 인터페이스에 넣는다. 이렇게 하면 한쪽에선 나머지 기능들이 필요없을 텐데, 그냥 빈칸으로 남긴다.
2. 공통되는 부분만 하나의 인터페이스에서 만든다. 이후 각 객체에서 필요로 하는 기능은 인터페이스로 따로 정의한 후 붙인다.

Service 부분이 처리할 것
 - Transaction
 - 데이터 정합성 확인
 - Security

ex1) 은행창구
 - 은행원 A는 고객 B의 계좌를 개설했다는 로그를 남김.
 -> Service의 Register 부분에서 처리 할 수 있다.
1
2
3
4
5
    public void register(User v) {
        System.out.println("Log Start ..." + v.getId());
        dao.insert(v);
        System.out.println("Log End ..." + v.getId());
    }
cs
현업에선 println하지 말 것 -> 리소스
확인용으로는 가능하지만.


ex2) 쿠팡
 - 주문이 일어나는 시간. -> 평소 시간을 저장한 후, 나중에 긴 시간이 걸리면 문제가 생기는 것.

DAO
 - DB에 넣기만 함.

Service + DAO = Business

Q. 모든 CRUD에 로그를 찍어라.
A. 모두 복사를 해서 넣는다? -> 로그 포맷을 바꿔라 -> 모두다 다시 수정(비효율)

AOP(Aspect Oriented Programming)

응집도와 관련.

핵심 비즈니스 로직은 몇개 안됨. 로깅, 예외처리, 트랜잭션 처리같은 부가적인 코드가 많음 -> method의 복잡도는 커짐

횡단 관심
XML에 어떤 함수가 실행되기 전에 공통기능을 사용하도록 한다. 
 
1
2
3
4
5
    public void register(User v) {
        System.out.println("Log Start ..." + v.getId()); // 횡단 관심
        dao.insert(v); // 핵심 비즈니스 로직
        System.out.println("Log End ..." + v.getId()); // 횡단 관심
    }
cs

 

방법 

 - XML(까다롭고 복잡)

 - Annotation (Simple 좋음)


 실습

1
2
3
4
5
6
7
8
package com.log;
 
public class LogAdvice {
    public void printLog() {
        System.out.println("[LOG]: Business Logic ...");
    }
}
 
cs

횡단관심 세팅

라이브러리에 필요한 것들 추가되어야함


XML 세팅 부분

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" <- AOP 추가
    xmlns:context=        "http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">    <- AOP 추가
    
 
    <bean id="log" class="com.log.LogAdvice"/> <- 사용할 횡단 관심
        
    <bean id="userdao" class="com.component.UserDao"/>
    <bean id="itemdao" class="com.component.ItemDao"/>
    <bean id="userservice" class="com.component.UserService">
        <property name="dao" ref="userdao"></property>
    </bean>
    <bean id="itemservice" class="com.component.ItemService">
        <property name="dao" ref="itemdao"></property>
    </bean>
    
    <aop:config>
        <aop:pointcut id="mypoint" 
        expression="execution(* com.component.*Service.*(..))" /> <- 들어갈 위치
        <aop:aspect ref="log">
            <aop:before pointcut-ref="mypoint" method="printLog"/> <- 위치에 친구를 쓴다.
        </aop:aspect>
    </aop:config>
    
 
</beans>
 
 
 
 
cs

실습 2


새로운 횡단관심

1
2
3
4
5
6
7
8
package com.log;
 
public class TxAdvice {
    public void startTx() {
        System.out.println("Start Transaction.....");
    }
}
 
cs

XML에 위치 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context=        "http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">    
    
 
    <bean id="log" class="com.log.LogAdvice"/>
    <bean id="tx" class="com.log.TxAdvice"/>
        
    <bean id="userdao" class="com.component.UserDao"/>
    <bean id="itemdao" class="com.component.ItemDao"/>
    <bean id="userservice" class="com.component.UserService">
        <property name="dao" ref="userdao"></property>
    </bean>
    <bean id="itemservice" class="com.component.ItemService">
        <property name="dao" ref="itemdao"></property>
    </bean>
    
    <aop:config>
        <aop:pointcut id="mypoint2" 
        expression="execution(* com.component.*Dao.*(..))" />
        <aop:pointcut id="mypoint" 
        expression="execution(* com.component.*Service.*(..))" />
        <aop:aspect ref="log">
            <aop:before pointcut-ref="mypoint" method="printLog"/>
            <aop:after pointcut-ref="mypoint2" method="printLog2"/>        
        </aop:aspect>
        <aop:aspect ref="tx">
            <aop:before pointcut-ref="mypoint" method="startTx"/>                    
        </aop:aspect>
        
    </aop:config>
    
 
</beans>
 
 
 
 
cs


Expression 부분의 위치는 정확히 표현. p170 확인

<aop:after> 오류 발생과 상관없이 그냥 실행.

Q. Exception이 발생할 떄 실행하도록.


빅데이터 분석

성향분석 -> 로그를 남김 -> 사이트 배치를 바꿈.

after - 아무때나

after-throwing - 오류가 날때만

after-returning 정상일때만


1
2
3
<aop:after pointcut-ref="mypoint2" method="printLog2"/> // 아무때나
<aop:after-throwing pointcut-ref="mypoint2" method="printLog2"/> //오류가 날때
<aop:after-returning pointcut-ref="mypoint2" method="printLog2"/> //정상일때
cs


Around

 실행되기 전 후에 무언가 하도록 한다.

실습

XML 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context=        "http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">    
    
 
    <bean id="log" class="com.log.LogAdvice"/>
    <bean id="tx" class="com.log.TxAdvice"/>
        
    <bean id="userdao" class="com.component.UserDao"/>
    <bean id="itemdao" class="com.component.ItemDao"/>
    <bean id="userservice" class="com.component.UserService">
        <property name="dao" ref="userdao"></property>
    </bean>
    <bean id="itemservice" class="com.component.ItemService">
        <property name="dao" ref="itemdao"></property>
    </bean>
    
    <aop:config>
        <aop:pointcut id="mypoint2" 
        expression="execution(* com.component.*Dao.*(..))" />
        <aop:pointcut id="mypoint" 
        expression="execution(* com.component.*Service.*(..))" />
        <aop:aspect ref="log">
            <aop:before pointcut-ref="mypoint" method="printLog"/>
            <aop:after-returning pointcut-ref="mypoint2" method="printLog2"/>        
        </aop:aspect>
        <aop:aspect ref="tx">
            <aop:around pointcut-ref="mypoint" method="aroundTx"/> <-수정된 부분                   
        </aop:aspect>
        
    </aop:config>
    
 
</beans>
 
 
 
 
cs


around내용 정의

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.log;
 
import org.aspectj.lang.ProceedingJoinPoint;
 
public class TxAdvice {
    public void startTx() {
        System.out.println("Start Transaction.....");
    }
    public Object aroundTx(ProceedingJoinPoint pjp) throws Throwable {
        Object result = null;
        System.out.println("aroundTx start....."); // 전에 할 일
        result = pjp.proceed(); // 메인 비즈니스 로직
        System.out.println("aroundTx end....."); // 후에 할 일
        return result;
    }
}
 
cs


Q. 어떤 함숙 ㅏ들어왔는지 확인하기 위해서 -> JoinPoint 사용

1
2
3
4
5
    public void printLog(JoinPoint jp) {
        String method = jp.getSignature().getName();
        Object [] args = jp.getArgs();
        System.out.println("[LOG]:"+method+" "+args[0].toString());
    }
cs


Q. 어떤 함수가 들왔는지 + 어떤 Return 하는지


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.log;
 
import org.aspectj.lang.JoinPoint;
 
public class LogAdvice {
    public void printLog(JoinPoint jp) {
        String method = jp.getSignature().getName();
        Object [] args = jp.getArgs();
        System.out.println("[LOG]:"+method+" "+args[0].toString());
    }
    
    public void printLog2(JoinPoint jp, Object returnObj) {
        String method = jp.getSignature().getName();
        System.out.println("[After-Returning LOG]: "+method+" "+returnObj);
    }
}
 
cs

Xml 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context=        "http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">    
    
 
    <bean id="log" class="com.log.LogAdvice"/>
    <bean id="tx" class="com.log.TxAdvice"/>
        
    <bean id="userdao" class="com.component.UserDao"/>
    <bean id="itemdao" class="com.component.ItemDao"/>
    <bean id="userservice" class="com.component.UserService">
        <property name="dao" ref="userdao"></property>
    </bean>
    <bean id="itemservice" class="com.component.ItemService">
        <property name="dao" ref="itemdao"></property>
    </bean>
    
    <aop:config>
        <aop:pointcut id="mypoint2" 
        expression="execution(* com.component.*Dao.*(..))" />
        <aop:pointcut id="mypoint" 
        expression="execution(* com.component.*Service.*(..))" />
        <aop:aspect ref="log">
            <aop:before pointcut-ref="mypoint" method="printLog"/>
            <aop:after-returning pointcut-ref="mypoint2" method="printLog2" returning="returnObj"/>        
        </aop:aspect>                
    </aop:config>
    
 
</beans>
 
 
 
 
cs





댓글

이 블로그의 인기 게시물

[건축][소프트웨어] 프로그램 비교

 실무를 하면서 라이노와 그래스호퍼를 많이 쓰게된다. 그러다보니 관련 지식도 쌓이고, 나 나름대로 정리도 하며 공유를 위해 글을 쓰기 시작했다. 앞으로 계획은 라이노와 라이노 안의 파이썬 스크립트 또는 그래스호퍼 연동으로 자동화 관련해서 다루려고 한다.  그 전에 왜 라이노, 그래스호퍼 그리고 파이썬을 쓰게 되게 됬는지 프로그램들 비교 후에얘기하고자 한다. 비교하려는 프로그램들은 다음과 같다. Rhino(RobertMcNell & Associates) 3d experience (Dassault Systemes) Revit (Autodesk)  세 프로그램 모두 써볼 기회가 있었고, 사용 빈도는 다음과 같다 Rhino >= 3d experience(CATIA) >>>>Revit HTML 소프트웨어 비교 Rhino 3dExpereince Revit 가격 995$ (영구 라이센스) 9,000~65,000$(유지비 6,246$ / 년) 2,642,640원 / 년 첫 프로젝트 1994년 3월/ 25m 크기 보트 1977년 / 미라주 전투기 정확하지 않음 파라메트릭 모델링 x(그래스호퍼로 가능) O(자체 모델링 + EKL(내부언어) 등) O(자체 모델링 + Dynamo) Rhino : 최고의 가성비 프로그램 장점 : NURBs 모델링 가능, 가장 저렴한 가격, API/SDK를 통한 개발환경 제공 단

일본 / 신오쿠보 / 도장집

신오쿠보에서 도장 만들기 1. 장소이전 이미 몇몇 블로그에서 정보를 찾아서 갔다. 하지만 문제는 장소를 이전했다는 사실이다. 또한 가격도 바꼈다. 다른 블로그에서 봤을 그 3층에 있고, 돈키호테 맞은편에 있는 도장집 겸 부동산집 2. 찾아가기 위치 〒160-0021 Tōkyō-to, Shinjuku-ku, Kabukichō, 2 Chome−19−11 新宿シャロームビル9F 다음과 같다. 히가시 신주쿠 역에서 서쪽(신주쿠방향)으로 걸어오는게 제일 빠르다고 나온다. 1층에 홍콩반점이 보이면 제대로 온 것. 3. 가격 변경 가격은 다음과 같다. 다른 블로그에서 엄청 쌋던거 같은데 조금 올랐다. 나 같은경우 한자로된 3글자 도장을 만들었고, 가격은 1000엔 들었다. 가격은 다음과 같은데 잘 나온느지 모르겠다. 여튼 이렇게 도장을 해결했다.

[건축]파사드 컨설팅이야기 - 1편: 파사드 컨설팅은 왜 필요한가?

 나의 약 1년 9개월간의 파사드 컨설팅을 중심으로 파사드 컨설팅 이라는 분야를 이야기 해보겠다.  파사드 컨설팅이란?  파사드 컨설팅은 우리나라에 다소 생소한 개념이다. 우선 Facade가 무엇인지 간략히 짚고 넘어갈 필요가 있다.  구글 검색에 facade를 치면 다음과 같은 정의가 나온다.  건물의 얼굴, 열린 장소 또는 거리에서 보이는 주 입면.  아주 간단하게 건물의 입면이라고 생각하면 된다. 그럼 왜 건물 입면에 컨설팅이 필요할까?  내가 참여하게 되었던 프로젝트를 생각해보며 적어보겠다. 파사드 컨설팅이 필요한 이유 1. 복잡해지는 형태  파사드는 건물의 외관이다.   즉, 누군가 건물을 봤을때 첫인상을 좌우하게 된다. 그래서 랜드마크나 기업 사옥을 건설할때 자주 건드리는 수단이 된다. 그리고 그 방향은 주로 DDP와 같이 유기적이거나 독특한 방향으로 가는 경우가 꽤나 있다. 박스에서 형태가 조금만 벗어나도 그것을 실체화 시키는데 큰 어려움이 따르게 된다. 특히 모든 프로젝트는 정해진 예산과 시간이 있기 때문에 이 제약조건 안에서 문제를 해결하려면 기존의 방법으론 불가능한 경우가 많다. 동대문 디자인 플라자. BIM과 비정형 사례로 항상 빠지지 않는다.  이 문제를 가장 깊이 고민하게 된 프로젝트는 <동대문 디자인 플라자>가 아닐까 생각한다. 업계가 좁은지라 이 프로젝트에 연관된 몇몇 분들을 만날 기회가 있었고, 그 중 외관(Facade)을 시공한 '스틸라이프' 대표님과는 영종도 프로젝트를 한적도 있다.  좌우지간, DDP는 삼성이 시공한 것으로 알려져 있지만 그 뒤엔 비정형 패널을 제작 설치하는데 특화된 '스틸라이프' 라는 업체와 파사드 컨설팅을 한 GT(Gehry Technologies)가 있었다.  프랭크 게리와 게리 테크놀로지스   수업시간에 들어봤을지 모르겠지만 게리는 Frank Gehry의 그 게리다.    어느 공식석상에서 법규를 날린것으로 유명한 게리 아저씨는 요상한 형태를 디자인 할 뿐만 아니