<context:component-scan base-package="com.ssafy"/>
해당 패키지을 탐색하며 component 등을 읽어서 생성 및 DI 기능을 수행한다.
생성하고 싶은 클래스 위에 @Repository,@Service,@Controller,@Component 를 선언한다.
클래스 내부에서 다른 클래스를 불러와 사용하는 곳에는 @Autowired 를 사용하면, 생성한 객체들을 알아서 매칭해서 불러온다.
@Repository("dicumentImpl") 와 같이 소괄호를 넣어 이름을 설정해준다.
사용하는 곳에서는 @Autowired 와 @Qualifier("dicumentImpl")를 둘 다 써준다.
그러면 해당 이름으로 설정해준 것을 불러 온다.
@Scope("singleton") 방식으로 사용할 수 있다.
싱글톤이 디폴트 상태이다. prototype을 하면 계속 생성해서 사용가능하다.
(request와 session scope도 있다.)
만약 throw하지 않는 에러가 나타났을 경우에는?
ㄱ. web.xml에 디스패처에 다음 파라미터를 추가한다.
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
ㄴ. 에러 관련 클래스를 만든다.
@ControllerAdvice //클래스 위에 설정
@ExceptionHandler(NoHandlerFoundException.class) //함수 위에 설정
@ResponseStatus(value = HttpStatus.NOT_FOUND) //404에러가 나타났을 때를 의미.
상황에 따라 Autowired 대신 Resource, Inject를 사용한다.
Resource : name으로 DI
(프레임 워크에 종속적이지 않다.)
Autowired : type으로 DI
(Spring 프레임 워크를 쓰다가 다른 프레임워크를 쓰면 많은 Resource발생. name으로 연결하고 싶으면 Qualfier를 쓴다.)
Inject : type으로 DI.
Resource는 특정 멤버 변수에 연결하는 느낌
Autowired,Inject는 멤버 변수 타입에 연결하는 느낌
ControllerAdvice로 오류가 발생할 시 들리는 클래스를 선택해준다.
ExceptionHandler로 특정 오류가 발생할 때 동작할 함수를 선택해준다.
@ControllerAdvice
public class ExceptionController {
@ExceptionHandler({MemberJoinException.class,RuntimeException.class})
public String handleException(Model model, Exception e) {
if(e instanceof MemberJoinException ) {
model.addAttribute("message", "파라미터가 잘 등록되지 않았습니다." );
}
return "/error/errorpage";
}
클래스들의 연결 상태를 xml에서 설정해서 사용한다.
bean을 사용한다.
(1) name : 해당 클래스의 이름을 의미한다.
(2) class : 해당 클래스의 경로를 의미한다.
(3) scope : singleton 등의 추가 설정 의미
constructor-arg 사용하기
(1) ref : 클래스의 이름을 넣는다.
property를 사용한다.
(1) name : 어떤 setter에 넣을 지 선택한다.
(2) value : 값 자체를 넣는다. (String int 다 가능)
(3) ref : 객체를 넣는다.(객체 이름 넣기)
<예시>
<bean id="boardService" class="com.ssafy.board.model.service.BoardServiceImpl">
<property name="boardDao" ref="boardDao"></property>
</bean>
<bean id="boardDao" class="com.ssafy.board.model.dao.BoardDaoImpl">
<constructor-arg ref="ds"></constructor-arg>
<constructor-arg ref="dbUtil"></constructor-arg>
</bean>
<bean id="dbUtil" class="com.ssafy.util.DBUtil"></bean>
<bean id="ds" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/ssafyweb?serverTimezone=UTC&useUniCode=yes&characterEncoding=UTF-8"></property>
<property name="username" value="ssafy"></property>
<property name="password" value="ssafy"></property>
</bean>
(1) property 혹은 constructor-arg에 Collection 값 넣기
<예시>
<bean id="player" class="com.ssafy.di.Player">
<bean id="mapdi" class="com.ssafy.di.MapDi">
<property name="myMap">
<map>
<entry key="username" value="uiseok"/>
<entry key="py" value-ref="player"/>
</map>
(2) 필터 적용
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(3) xmlns 속성
xml namespace의 약자이다.
xmlns:p 와 같이 설정했으면, 해당 xml의 설정을 쓸 수 있다.
쓸 때는 <p:xxx /> 이런식으로 쓰면 된다.
ApplicationContext context = new ClassPathXmlApplicationContext("com/ssafy/board/config/application.xml");
BoardService boardService = context.getBean("boardService", BoardServiceImpl.class);