'Back-End'에 해당되는 글 135건

donaricano-btn

 Method Overloading

If a class have multiple methods by same name but different parameters, it is known as Method Overloading


1. Advantage of method overloading

- Method overloading increases the readability of the program


2. Different ways to overloading the method

2_1. By changing number of arguments

2_2. By changing the data type


1) By changing the number of arguments



2) By changing the data type

 


3. Questions

3_1. Why method overloading is not possible by changing the return type of method?

- there may occur ambiguity.


3_2. Can we overloading main() method?

  

- Yes

'Back-End > Java_1' 카테고리의 다른 글

[Java] Static_2  (0) 2016.07.26
[Java] Constructor_2  (0) 2016.07.26
[Java] Object and Class  (0) 2016.06.27
[Java] Java OOPs Concepts  (0) 2016.06.27
[Java] Json  (0) 2016.06.15
블로그 이미지

리딩리드

,
donaricano-btn

AOP_2


1. 애노테이션을 이용한 AOP

1_1 Advice

getProduct() 이용 할떄 마다 어드바이스 동작

- 애노테이션이 붙어있는 것을 어드바이스라한다

- execution포인트컷이며 필터링 조건이다


1_2. Bean


1_3. Annotation Advice

a. @Before, @After

1) 기본형태

@Before("primitivePointCut"), @After("primitivePointCut")

메소드명은 임의

메소드의 인수는 없는게 기본

메소드의 반환값은 void가 기본

2) 메소드 명을 출력하도록 변경

- org.aspectj.lang.JoinPoint설정


b. @AfterReturning

1) 기본형태
@AfterReturning(value="primitivePointcut", returning="반환값의 변수명")
메소드 호출이 예외를 내보내지 않고 종료했을때
메소드명은 임의
메소드인수는 AOP 적용대상이 된 메소드의 반환형과 매노테이션의 returning속성에서 지정한 변수명으로 해야함
반환값은 void가 기본

c. @Around
1) 기본형태
@Around("primitivePointcut")
메소드 호출 전후
메소드명은 임의
메소드의 파라미터에는 반드시 org.aspectj.lang.ProceedinJoinPoint를 기술
반환값은 AOP의 대상이 된 메소드의 반환값의 형과 호환성이 있어야한다. 예를 들어 반환값의 형을 object로 해두면좋다

2) proceed()
AOP 대상이 되는 메소드 호출은 인수로 기술된 org.aspectj.lang.ProceedingJoinPoin 클래스의 proceed메소드를 통해 이루어진다

3) 메소드명 출력하도록 변경

4) 다른 어드바이스 처럼 사용
- proceed() 앞에 처리를 기술하면 @before
- proceed()를 trey-catch로 묶으면 @AfterThrowing

d. @AfterThrowing

1) 기본형태
@AfterThrowing(value="primitivePointcut", throwing="예외변수명")
AOP의 대상이 되는 메소드에서 예외가 발생 했을때만 동작
메소드명은 임의
메소드의 인수에는 캐치하고 싶은 예외를 기술하고 그변수명은 애노테이션의 throwing 속성으로 지정한 "예외늬 변수명"과같다
메소드의 반환값은 기본 void

2. Bean 정의 파일을 이용한 AOP

3. 포인트컷 기술 방법

- AspectJ 라는 AOP제품 사용

- 호출하는 쪽의 메소드나 클래스, 호출되는 쪽의 메소드나 클래스를 조건으로 지정할 수 있는 Primitive Pointcut이라 불리는 포인트컷이 준비됨

- 자주사용하는 포인트컷은 execution 이다


3_1. execution기본 문구

execution(메소드 수식자^메소드의 반환값^패키기.클래스 또는 인터페이스.메소드명(인수의 형|. 인수의형...| ^throws^에외)

^:공백을 의미함 

Example

- public String getMethod() 메소드에서 에스펙트를 실행할 수 있는 포인트컷 기술방법은?

a : execution(public String com.kyle.service.ProductDaoImpl.getProduct())

b : execution(* *..*getMethod())

c : execution(* *..ProductDaoImpl.*())

d : execution(public String get*(..))

e : execution(* com.kyle.service.*.*(..))


3_2. 논리연산자 이용

- 포인트컷은 몇가지 포인트컷을 조합해서 사용가능

1) || 또는 OR

Example

execution(* *..ProductServiceImpl.findProduct()) or

execution(* *..ProductDaoImpl.getProduct())

Define

findProduct() 또는 getProduct()를 지정


2) && 또는 and

Example

execution(* * ProductServiceImpl.findProduct())&&

execution(* *..ProductDaoImpl.getProduct())

Define

findProduct() 와 getProduct()를 지정


3) !또는 not    

Example

execution(* findProduct()) and not

execution(* *..ProductDaoImpl.*())

Define

ProductDaoImpl이외의 클래스의 메소드 findProduct() 를 지정


'Back-End > SpringFrame_1' 카테고리의 다른 글

[Spring] DataAccessLayer_2  (0) 2016.07.31
[Spring] DataAccessLayer_1  (0) 2016.07.25
[Spring] DI_2  (0) 2016.07.17
[Spring] DI_1  (0) 2016.07.16
[Spring] AOP_1  (0) 2016.07.04
블로그 이미지

리딩리드

,
donaricano-btn

DI_2


1. Bean 정의 파일로 DI


1_1. BeanFactory

- DI 컨테이너의 핵심이 됨

- BeanFactory는 실행 시에 건네지는 Bean정의 파일(applicationContext.xml)을 바탕으로 인스턴스를 생성하고 인젝션한다

- 웹 어플리케이션 개발에 개발자가 직접 이용하는 일은 별로 없다

- ClassPathXmlApplicationContext는 BeanFactory의 상위에 있다

- BeanFactory의 이용


1) Bean정의 파일

- Bean의 정의 파일을 프로퍼티, xml, LDAP 등으로 다루지만 XML이 일반적이다

- Bean정의 파일 이용시 Setter 메소드가 필요하다

- applicationContext.xml

- ProductServiceImpl

2) Bean 정의 파일 분할

- 규모가 커질수록 빈 정의 파일을 분할 하는게 좋다

- applicationContext.xml

- applicationContext-bean.xml

- import.xml

3)  프로퍼티 파일 이용

a.  bean정의 파일 경우

- message.properties

- applicationContext.xml

- MessageServiceImpl

b. annotation  경우

- applicationContext.xml

- MessageServiceImpl


2. ApplicationContext

- ApplicationContext 는  BeanFactory를 확장Bean 정의파일 읽기, 메세지 소스, 이벤트 처리 등의 기능을 추가한것이다


2_1. Bean 정의 파일 읽기

- 웹 어플리케이션은  ContextLoaderListener 클래스나  ContextLoaderServlet 클래스에 의해 자동으로 ApplicationContext(XmlWebApplicationContext 클래스)가 로드됨

a. web.xml

b. 복수의  Bean 정의 파일

c. 웹 어플리케이션에선 ApplicationContext를 클래스에서 직접 이용할 일이 없지만 간단한 예제 어플리케이션을 만들때는 아래처럼 사용한다

    

d. ApplicationContext를 POJO로 만들어진 클래스에 직접 이용할떄

- @Autowired를 사용


3. Spring logging

- 스프링은 기본적으로 Commons Logging으로 로그를 출력함

- Log4j라이브러리가 있으면 Commons Logging이 Log4j를 사용한다

- WEB-INF 또는 클래스 패스 아래에 두면 사용가능하다

- log4j.xml


4. Spring unitTest

- DI 컨테이너로 관리하는 오브젝트를 유닛 테스트하기 위한 기능이 제공됨

- Test.java

- 데이터 베이스 테스트시 자바의 내장 데이터베이스를 사용한다

'Back-End > SpringFrame_1' 카테고리의 다른 글

[Spring] DataAccessLayer_1  (0) 2016.07.25
[Spring] AOP_2  (0) 2016.07.20
[Spring] DI_1  (0) 2016.07.16
[Spring] AOP_1  (0) 2016.07.04
[Spring]8_4 Skill of Spring  (0) 2016.02.28
블로그 이미지

리딩리드

,
donaricano-btn

DI_1


1. What is DI

- DI는 인터페이스를 이용해 컴포넌트화를 실현함

- 오브젝트 사이의 의존 관계를 만든다


1_1 DI 하지 않았을때

- 각자의 new한 다음에 각각의 인스턴스를 생성해서 사용


1_2 DI 사용

- new라는 키워드를 사용하지 않는다

- DI컨테이너 생성, ProductService의 인스턴스 취득은 ApplicationContext에서 함

- 작은 어플리케이션에서는 main에서 DI컨테이너 생성

- DI컨테이너가 인스턴스를 생성할 클래스, 인스턴스를 전달 받은 클래스는 모두 POJO로 작성된다

- 팩토리메소드 같은 디자인 패턴이 필요없다

- DI컨테이너는 클래스를 단 한번 인스턴스로 만들고 재사용 함으로 싱글톤을 구현한다


2. Using of DI

- 서비스와 컨트롤러, 서비스와 DAO의 의존관계를 구축하는데 어울림

- 서비스와 도메인(Product), DAO와 도메인(Product)은 어울리지 않는다


3. DI by using annotation


3_1. DI를 사용하는 방법

1) Bean정의 파일을 사용한 DI

2) Annotation을 사용한 DI


3_2. @Autowired, @Component



- DI 컨테이너는 @Autowired가 붙은 인스턴스 변수의 형에 대입할 수 있는 클래스를 @Component가 붙은 클래스 중에서 찾아내 그 인스턴스를 인젝션한다

- 인젝션이 setProductDao 같은 셋터 메소드는 필요없다


3_3. applicationContext.xml

- bean 스키마 : Bean(컴포넌트) 설정

- context스키마 : Bean(컴포넌트) 검색과 애노테이션 설정

- <context:annotation-config/>

@Autowired, @Resource를 이용할 때의 선언이다

context:component-scan혹은 mvc:annotation-driven이 있으면 생략 가능

- <context:component-scan base-package=""/>

@Component, @Service등의 컴포넌트를 이용할떄 선언

context:exclude-filter(검색하지 않는 컴포넌트 조건)

use-default-filters="false"(지정한 패키지 아래 컴포넌트를 찾지 않을떄)

context:include-filter(검색할 컴포넌트릐 조건)


3_4. @Autowired

1) @Autowired 위치

- 메소드에 선언

- 생성자 인젝션


2) @Autowired 필수 설정


3) 인젝션할 클래스 찾는 방법

(1) byType 

- @Component가 붙은 클래스가 여러개 있어도 형이 다르면 @Autowired가 붙은 인스턴스 변수에 인젝션되지 않는다

(2) byName 

- 인젝션할 클래스를 형이 아닌 이름으로 찾아주는 방법

(3) context:component-scan 이용

- context:component-scan을 어느 정도 크기의 컴포넌트마다 기술해 두고, 만약 어떤 컴포넌트를 테스트 용으로 바꾸자고 할때는 그 컴포넌트 부분의 정의만 테스트로 바꿈

-평상시

- 컴포넌트 스캔변경


3_5. @Component

- @Component는 DI 컨테이너가 관리하는, 주로 인젝션을 위한 인스턴스를 설정함

- 클래스 선언 앞에 @Component를 붙이면 스프링의 DI컨테이너가 찾아서 관리함


1) @Component의 확장

- @Controller

- @Service

- @Repository


2) @scope

- @Scope뒤에 value속성을 지정하면 인스턴스화와 삭제를 제어함

- @Scope을 생략하면 해당 클래스는 싱글톤이된다

singleton : 인스턴스를 싱글톤으로 한다

prototype : 이용할 떄마다 인스턴스화 한다

request : Servlet API의 Request 스코프인 동안만 인스턴스가 생존한다

session : Servlet API의 Session 스코프인 동안만 인스턴스가 생존

예제 소스 : https://github.com/KyleJeong/SpringFramework/tree/master/simpleProject/src/main/java/DI

'Back-End > SpringFrame_1' 카테고리의 다른 글

[Spring] AOP_2  (0) 2016.07.20
[Spring] DI_2  (0) 2016.07.17
[Spring] AOP_1  (0) 2016.07.04
[Spring]8_4 Skill of Spring  (0) 2016.02.28
[Spring]8_2.Purpose of Spring - 스프링의 목적  (0) 2016.02.28
블로그 이미지

리딩리드

,