Spring 애플리케이션의 주요 설정 파일 중 하나. 이 파일은 Spring 애플리케이션 컨텍스트를 구성하고 빈(bean)을 정의하는 데 사용.
<context:component-scan>
: 지정된 패키지와 하위 패키지에서 Spring 컴포넌트를 검색하고 등록. @Component
, @Controller
, @Service
, @Repository
등의 어노테이션이 있는 클래스를 찾는다.<bean>
요소: Spring Bean을 정의하는 데 사용. 각 Bean은 id 속성을 사용하여 고유하게 식별되며, class 속성을 사용하여 해당 Bean의 클래스를 지정. 또한 다양한 프로퍼티(property)를 설정하여 의존성 주입 및 기타 설정을 지정할 수 있다.<property>
요소: Bean의 프로퍼티 값을 설정하는 데 사용. 각 프로퍼티는 name 속성을 사용하여 해당 Bean 클래스의 속성을 지정하고, value 속성을 사용하여 해당 속성의 값을 지정.<context:property-placeholder>
: 외부 속성 파일의 값을 읽어와서 Bean의 프로퍼티로 설정하는 데 사용. 주로 데이터베이스 연결 정보나 다른 환경 설정과 관련된 값들을 외부 파일에 저장하고 이를 읽어와서 설정.applicationContext.xml
파일은 Spring 애플리케이션의 주요 구성 파일 중 하나로, 애플리케이션의 구조와 동작을 결정하는 데 중요한 역할을 한다.<?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">
<!-- 환경변수 설정 -->
<context:property-placeholder location="classpath:database.properties"/>
<!-- DataSource 설정 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Hibernate Session Factory 설정 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="org.example.entity"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<!-- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>-->
</props>
</property>
</bean>
<!-- Hibernate Transaction Manager 설정 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Exception translation bean -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
</beans>
<context:property-placeholder>
: 외부 속성 파일(database.properties
)에서 값들을 읽어와서 Bean의 프로퍼티로 설정. 이를 통해 데이터베이스 연결 정보와 같은 중요한 설정 값을 외부 파일에 저장하고 유연하게 변경할 수 있다.<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
: 데이터베이스 연결을 위한 DataSource를 설정. JDBC를 사용하여 데이터베이스와의 연결을 담당. driverClassName
, url
, username
, password
와 같은 속성을 통해 데이터베이스 연결 정보를 설정.<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
: JPA를 사용하는 Hibernate 세션 팩토리를 설정. DataSource를 설정하고 Entity 클래스들이 위치한 패키지를 지정. 또한 HibernateJpaVendorAdapter를 통해 Hibernate의 설정을 지정하고, hibernate의 다양한 속성들을 설정.<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
: JPA를 사용하는 Hibernate 트랜잭션 매니저를 설정. entityManagerFactory를 주입하여 사용. 이를 통해 Spring의 트랜잭션 기능을 활성화하고, 데이터베이스 트랜잭션을 관리.<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
: 예외를 처리하는 데 사용되는 빈을 설정. PersistenceExceptionTranslationPostProcessor는 JPA 예외를 Spring의 DataAccessException으로 변환하여 처리.정리
Spring 기반의 애플리케이션에서 데이터베이스와의 연동을 구성하고,
JPA를 사용하여 데이터베이스 작업을 수행할 수 있도록 설정.