공간정보아카데미

Spring - Bean 주입 / XML vs JavaConfig

minjava 2025. 9. 6. 22:21

Spring 에서 Bean(객체)을 컨테이너에 등록하고 의존성을 주입하는 방법은 크게 두 가지가 있음. - XML 설정, JavaConfig 방식

 

 

XML 기반

- 설정이 코드와 분리되며, 운영 중 XML만 수정이 가능하다.

- 설정파일이 길어지고 관리가 어렵다.

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" id="WebApp_ID" version="5.0">

<display-name>myspringmvc</display-name>

 

<!-- Spring CharacterEncodingFilter 설정 -->

<filter>

<filter-name>encodingFilter</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>

<init-param>

<param-name>forceEncoding</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring-context.xml</param-value> <!-- /servlet-context.xml : 스프링의 설정 파일 -->

</context-param>

 

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

 

<servlet>

<servlet-name>myservlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:servlet-context.xml</param-value> <!-- /servlet-context.xml : 스프링의 설정 파일 -->

</init-param>

</servlet>

 

<!-- 서블릿이 모든 요청을 받음 -->

<servlet-mapping>

<servlet-name>myservlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

 

</web-app>

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns="http://www.springframework.org/schema/mvc"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:beans="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

 

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

 

<!-- Enables the Spring MVC @Controller programming model -->

<annotation-driven />

 

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->

<resources mapping="/resources/**" location="/resources/" />

 

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<beans:property name="prefix" value="/WEB-INF/views/" />

<beans:property name="suffix" value=".jsp" />

</beans:bean>

 

<interceptors>

<interceptor>

<mapping path="/addrbook_list.do"/>

<mapping path="/insert.do"/>

<beans:bean class="lx.edu.springmvc.interceptor.LoginInterceptor"></beans:bean>

</interceptor>

</interceptors>

 

<context:component-scan base-package="lx.edu.springmvc" />

 

</beans:beans>

<?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"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

https://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

https://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop

https://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">

<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>

<property name="url" value="jdbc:mysql://localhost:3306/newhr"></property>

<property name="username" value="root"></property>

<property name="password" value="rootroot"></property>

</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"></property>

<property name="mapperLocations" value="classpath:mapper-*.xml"></property>

<property name="configuration">

<bean class="org.apache.ibatis.session.Configuration">

<property name="mapUnderscoreToCamelCase" value="true"></property>

</bean>

</property>

</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>

</bean>

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

 

<!--

<context:component-scan base-package="lx.edu.springmvc">

</context:component-scan>

-->

</beans>

 

JavaConfig

- 타입 안전

- 코드 기반이라 가독성, 유지보수성이 높다.