root-context.xml에서 하단 탭의 Namespaces를 클릭한다.
잠시 기다리면 file들이 뜨는데 총 네 가지 항목의 체크 박스에 체크를 해주면 된다.
- aop
- beans
- context
- jdbc
하단 탭 Source를 클릭하고 DB 경로를 작성한다.
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/본인이 사용할 DB이름"></property>
<property name="username" value="본인이 사용할 DB ID명"></property>
<property name="password" value="본인이 사용하는 DB ID의 비밀번호"></property>
</bean>
https://mybatis.org/spring/ko/factorybean.html 링크를 참고하여 sqlSessionFactory를 작성하고자 했다.
나는 value만 나에게 맞추어 바꾸어주었다.
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 만약 경로를 찾지 못하면 classpath*:mappers/**/*Mappers.xml -->
<property name="mapperLocations" value="classpath:mappers/**/*Mappers.xml" />
</bean>
마지막으로 mybatis를 추가한다.
<mybatis-spring:scan base-package="repository package 이름"/>
전체적인 root-context.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/본인이 사용할 DB이름"></property>
<property name="username" value="본인이 사용할 DB ID명"></property>
<property name="password" value="본인이 사용하는 DB ID의 비밀번호"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 만약 경로를 찾지 못하면 classpath*:mappers/**/*Mappers.xml -->
<property name="mapperLocations" value="classpath:mappers/**/*Mappers.xml" />
</bean>
<mybatis-spring:scan base-package="repository package 이름"/>
</beans>
Security-context.xml은 직접 생성해 주어야한다.
단축키 Ctrl + n을 누르고 Spring > Spring Bean Configuration File을 선택하고 Next 버튼을 클릭한다.
appServlet 하위에 security-context.xml라고 이름을 정해주고 Next 버튼을 클릭한다.
상단에서는 beans만 체크박스에 체크하고 하단에는 제일 첫 번째에 위치한 spring-beans.xsd에 체크한다.
그 후, Next 버튼을 클릭한다.
아무 것도 하지 않고 바로 Finish 버튼을 클릭한다.
비밀번호를 암호화하여 DB에 저장하는 클래스를 추가한다.
<bean id="bCryptPasswordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"></bean>
완성된 전체적인 security-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bCryptPasswordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"></bean>
</beans>
web.xml에서security-context.xml를 선언하기 위해 servlet의 param-value를 추가한다.
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/servlet-context.xml
/WEB-INF/spring/appServlet/security-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
web.xml의 전체적인 코드는 다음과 같다.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/servlet-context.xml
/WEB-INF/spring/appServlet/security-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Spring MVC(JSP) 개발환경 설정하기(5)_root-context.xml과 security-context.xml 설정 (ver.xml) 끝!
얼렁뚱땅 주니어 개발자
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!