본문 바로가기

프레임워크/Spring

[프레임워크] <Spring>〈Maven〉pom.xml 설정 개선 feat. 어노테이션

설정들이 많아진다면?

◎ 개발자에게 부담이된다.

◎ 작은 에러라도 발생하면 프로그램이 실행되지 않는다.


개선

◎ 과도한 설정을 줄이기 위해 @어노테이션 기능을 개발했다.

◎ 일반 스프링 환경에서는 .xml + @이지만 부트에서는 @ 위주로 실행한다.


부트를 활용하기 위한 추가 스키마

◎ xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.2.xsd"

<?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:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation= "http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.2.xsd">


</beans>

@어노테이션

◎ 개발자의 가독성을 증가시켜 준다.

◎ xml로의 접근을 줄여준다.

   ⇨ .java에 작성한다.

   자바 코드와 설정을 한번에 확인 가능하다.

   자바코드에만 접근하면 되기 때문에 .xml을 덜 열어보게된다.

 

◎ 컨테이너가 같은 어노테이션끼리는 모아서 관리한다.

◎ 어노테이션을 위주로 사용하는 부트를 사용하기 위해서는 추가 스키마가 필요하다.

<?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:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation= "http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.2.xsd">

</beans>

@어노테이션 종류

◎ Component 

   스프링이 클래스 경로를 스캔하고, 특정 어노테이션이 붙은 클래스들을 자동으로 빈으로 등록하는 기능이다.

   XML 파일에 빈을 명시적으로 등록하지 않고도 빈을 등록할 수 있습니다.

   ⇨ 스캔을 위해 xml 내용을 추가할 필요가 있다.

@Component("galaxy")
public class GalaxyPhone implements Phone {}
<context:component-scan base-package="test" />

 

 

◎ Autowired : 의존주입

   메모리의 자료형을 인지하고 연결해주는 역할을 수행한다.

@Component("galaxy")
public class GalaxyPhone implements Phone {
	@Autowired
	private Watch watch;   
}

 

 

Qualifier

   여러 개의 동일한 타입의 빈이 존재할 때, 특정한 빈을 주입할 때 사용된다.

@Component("galaxy")
public class GalaxyPhone implements Phone {
	@Autowired
	@Qualifier("galaxyWatch")
	private Watch watch;
}

 

 

◎ Service

   ⇨ Component를 상속받는 어노테이션 중 하나이다.

   개발자가 이 객체가 Service 레이어의 객체임을 바르게 파악 가능하다.

@Service("memberService")
public class MemberServiceImpl implements MemberService {}

 

 

◎ Repository

   ⇨ Component를 상속받는 어노테이션 중 하나이다.

   ⇨  DAO를 new하는 어노테이션이다.

@Repository("memberDAO")
public class MemberDAO {}