dependencies { // 기존 javaEE에서 Servlet으로 변경 compileOnly('javax.servlet:javax.servlet-api:4.0.1') testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") // 스프링 설정 implementation group: 'org.springframework', name:'spring-core', version: '5.3.19' implementation group: 'org.springframework', name:'spring-context', version: '5.3.19' implementation group: 'org.springframework', name:'spring-test', version: '5.3.19' }
dependencies { 스프링 라이브러리 설정... // Lombok compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.24' annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.24' // 테스트 환경에서도 사용할 수 있게 설정 추가 testCompileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.24' testAnnotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.24' }
dependencies { 스프링 라이브러리 설정 ... Lombok 설정 ... // Log4j2 implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.19.0' implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.19.0' implementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.19.0' }
<?xml version="1.0" encoding="UTF-8" ?> <configuration status="INFO"> <Appenders> <Console name="console" target="SYSTEM_OUT"> <PatternLayout charset="UTF-8" pattern="%d{hh:mm:ss} %5p [%c] %m%n" /> </Console> </Appenders> <loggers> <logger name="org.springframework" level="INFO" additivity="false"> <appender-ref ref="console"/> </logger> <logger name="org.zerock" level="INFO" additivity="false"> <appender-ref ref="console" /> </logger> <root level="INFO" additivity="false"> <AppenderRef ref="console"/> </root> </loggers> </configuration>
dependencies { 스프링 라이브러리 설정 ... Lombok 설정 ... Log4j2 설정 ... // JSTL implementation group: 'jstl', name: 'jstl', version: '1.2' }
스프링 프레임워크는 자체적으로 객체를 생성하고 관리하면서 필요한 곳으로 객체를 주입하는 역할
설정 파일이나 어노테이션을 이용해 의존성 주입
root-context.xml
... <bean class="org.zerock.springex.sample.SampleDAO"></bean> <bean class="org.zerock.springex.sample.SampleService"></bean> ...
테스트 해보기
@Log4j2 // JUnit5 버전에서 spring-test를 이용하기 위한 설정 // JUnit4 버전은 @Runwith @ExtendWith(SpringExtension.class) // 스프링 설정 정보 로딩 @ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/root-context.xml") public class SampleTests { // 의존성 주입 관련 어노테이션 @Autowired private SampleService sampleService; @Test public void testService1(){ log.info(sampleService); Assertions.assertNotNull(sampleService); } }
스프링이 시작되면 빈 객체를 관리하기 위해 ApplicationContext가 생성
root-context에 bean태그나 context:component-scan 태그를 통해 빈객체 생성 및 관리 시작
코드를 실행하다 @Autowired를 만나면 스프링이 생성된 빈객체에 존재하는지 확인 후 존재하면 해당 부분에 주입
@Autowired
@Contoller
@Service
@Repository
@Component
implementation group: 'org.springframework', name: 'spring-webmvc', version: '5.3.23'
// 서블릿 이전에 서블릿을 초기화 <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/root-context.xml</param-value> </context-param> // 웹 애플리케이션 설정 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>