1. Java 기반으로 Spring 설정 변경하기
기존 시큐리티 적용 시에 xml로 관리할 경우, 예외 페이지 등록이 매우 번거로움.
→ 시큐리티를 좀 더 쉽게 관리하기 위해 Java로 설정을 변경하려고 함
- 참고할 파일 목록
1) web.xml : 어플리케이션 설정
2) root-context.xml : 스프링 코어 설정
3) servlet-context.xml : 스프링 mvc 설정
2. pom.xml(maven-war-plugin 추가)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
3 config 패키지&클래스 생성
package works.yermi.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() { // root-context
// TODO Auto-generated method stub
return new Class[] {RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() { // servlet-context
// TODO Auto-generated method stub
return new Class[] {ServletConfig.class};
}
@Override
protected String[] getServletMappings() { // url-pattern
// TODO Auto-generated method stub
return new String[]{"/"};
}
}
* RootConfig.java와 ServletConfig.java는 클래스만 생성해 놓는다.
4. DB 연동 확인(히카리 연동)
package works.yermi.config;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
public class RootConfig {
@Bean // Hikari 빈 등록
public DataSource dataSource() {
HikariConfig config = new HikariConfig();
// 기존 property 내용들 set으로 입력
config.setDriverClassName("net.sf.log4jdbc.sql.jdbcapi.DriverSpy");
config.setJdbcUrl("jdbc:log4jdbc:oracle:thin:@서버이름:1521:xe");
config.setUsername("spring");
config.setPassword("1234");
return new HikariDataSource(config);
}
@Bean // SqlSessionFactoryBean 빈 등록
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource());
bean.setTypeAliasesPackage("works.yermi.domain");
return bean.getObject();
}
}
- 테스트 코드(ContextConfiguration 내용 변경)
package works.yermi.persistence;
import static org.junit.Assert.assertNotNull;
import java.sql.Connection;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.Setter;
import lombok.extern.log4j.Log4j;
import works.yermi.config.RootConfig;
@Log4j
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=RootConfig.class) // rootConfig로 경로 재설정
public class DataSourceTests {
@Setter @Autowired
private DataSource dataSource;
@Setter @Autowired
private SqlSessionFactory sqlSessionFactory;
@Test
public void testExist(){
assertNotNull(dataSource);
}
@Test
public void testConnection(){
try(
SqlSession sessionFactory = sqlSessionFactory.openSession();
Connection conn = sessionFactory.getConnection()
) {
log.info(sessionFactory);
log.info(conn);
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}