프로젝트/스프링

스프링 부트(SpringBoot) DB연결(xml설정)

redbear0077 2025. 1. 27. 18:34
반응형

스프링 부트(SpringBoot) DB연결(xml설정)

 

환경

자바8
인텔리제이
스프링부트
마리아 디비
그레이들(gradle)
타임리프(thymeleaf)
applicationContext.xml

 

기본프로젝트 설정
보통 스프링 부트에서 데이터 소스 설정은 application.properties에서 설정하지만 이번 설정은 xml파일을 이용한 설정을 진해한다. 기존 설정과 다른부분은 application.properties를 사용하지 않고 applicationContext.xml을 사용한다. 기본 프로젝트 (설정 방법URL)

 

 

xml설명
  • applicationContext.xml역할
스프링 빈 정의 : 애플리케이션에서 사용할 객체를 스프링 컨테이너에 등록
의존성 주입 : 빈 간의 의존성을 설정
외부 이소스 설정 : 데이터베이스 연결, 메시지  솟, 프로퍼티 파일 등 외부 리소스를 연결
AOP, 트랜잭션 설정 : Aspect 설정 및 트랜잭션 관리

 

기본구성
<?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.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 데이터 소스 설정 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.mariadb.jdbc.Driver" />
        <property name="url" value="jdbc:mariadb://127.0.0.1:3306/psb?characterEncoding=utf8" />
        <property name="username" value="root" />
        <property name="password" value="1234" />
    </bean>

    <!-- MyBatis SqlSessionFactory 설정 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:mapper/**/*.xml" />
    </bean>

</beans>

 

주요 태그 설명
  • <bean> : 스프링 빈을 정의하며, 클래스와 속성을 설정한다.
속성
id : 빈의 고유 식별자
class : 빈의 클래스 경로
property : 빈의 의존성을 주입

 

예시 코드

<bean id="userService" class="com.example.service.UserService">
    <property name="userRepository" ref="userRepository" />
</bean>

<bean id="userRepository" class="com.example.repository.UserRepository" />

 

  • <context:component-scan> : 지정한 패키지에서 자동으로 빈을 스캔하고 등록한다.
속성
base-package : 스캔할 패키지 경로

 

예시 코드

<context:component-scan base-package="com.example" />
이 설정으로 @Component, @Service, @Repository, @controller가 붙은 클래스가 자동으로 빈으로 등록된다.

 

  • <Context:property-placeholder> : 외부 프로퍼티 파일을 읽어와 애플리케이션에 갓을 주입한다.
속성
location : 프로퍼티 파일의 경로

 

예시 코드

<context:property-placeholder location="classpath:application.properties" />
이 설정으로 application.properties파일의 값을 ${}를 통해 사용할 수 있도록한다.

 

전체코드

<?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.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 데이터 소스 설정 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.mariadb.jdbc.Driver" />
        <property name="url" value="jdbc:mariadb://127.0.0.1:3306/psb?characterEncoding=utf8" />
        <property name="username" value="root" />
        <property name="password" value="1234" />
    </bean>

    <!-- MyBatis SqlSessionFactory 설정 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:mapper/**/*.xml" />
    </bean>

</beans>

 

  • SpringtestApplication설정
기본 프로젝트 설정과 다른점은 applicationContext.xml(application.properties사용 안함)과 SpringtestApplication.java를 수정해야하는것이다.
@ImportResource("classpath:applicationContext.xml")를 추가해야한다.
@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
public class SpringtestApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringtestApplication.class, args);
	}

}

 

주의
applicationContext.xml(application.properties사용 안함)과 SpringtestApplication.java에
@ImportResource("classpath:applicationContext.xml")를 추가

 

반응형