๐ŸŒธ [SPRING] | <bean> , <property> ,<constructor-arg> , classpath , applicationContext.xml

0
post-thumbnail


๐Ÿ”น <bean>

bean๋“ฑ๋ก(๊ฐ์ฒด๋“ฑ๋ก) ํ•ด์ค˜์š”.

  • ํด๋ž˜์Šค๋ฅผ ๊ฐ์ฒดํ™”ํ•˜์—ฌ spring์ด ๊ทธ ์ฃผ์†Œ๊ฐ’๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ์š”์ฒญํ•  ๋•Œ ๋งˆ๋‹ค ๊ฐ€์ ธ๋‹ค ์ค˜์š”.

๐Ÿ”น <property>

  • <property name="๋ฉค๋ฒ„ํ•„๋“œ" value ๋˜๋Š” ref="๋ฉค๋ฒ„ํ•„๋“œ์— ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค">
  • setter ๋ฉ”์†Œ๋“œ๋ฅผ ์“ฐ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์•„์š”.
  • value ์†์„ฑ์€ ๊ฐ’์„, ref์†์„ฑ์€ ๊ฐ์ฒด๋ฅผ ๋ฉค๋ฒ„ํ•„๋“œ์— ๋‹ด์•„์ค˜์š”.

๐Ÿ”น <constructor-arg>

  • <constructor-arg value ๋˜๋Š” ref=""> </constructor-arg>
  • ์ƒ์„ฑ์ž์˜ ์ธ์ž๋ฅผ ๋„ฃ๋Š” ๊ฑฐ์˜ˆ์š”.

๐Ÿ”น classpath

โœ… application.properties๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ์ด์œ 

  • ์–ด๋””์„œ๋“  classpath๋กœ ์‹œ์ž‘ํ•˜๋ฉด resourcesํด๋”์—์„œ ๋ถ€ํ„ฐ ์ฐพ๊ธฐ ์‹œ์ž‘ํ•ด์š”.

<context:property-placeholder location="classpath:application.properties"/>


๐Ÿ”น applicationContext.xml

โœ… <beans></beans> ์•ˆ์—์„œ ์ž‘์„ฑ๋ผ์š”.

โœ… <context:property-placeholder>

<context:property-placeholder location="classpath:application.properties"/>

  • propertiesํŒŒ์ผ์˜ ์œ„์น˜์™€ ํŒŒ์ผ์ด๋ฆ„์„ ์ ์–ด์ค˜์š”.
  • propertiesํŒŒ์ผ์—๋Š” DB์ •๋ณด๊ฐ€ ์žˆ์–ด์š”.

โœ… DataSource ์„ค์ •

    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="driverClassName" value="${db.driver}" />
        <property name="jdbcUrl" value="${db.host}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <constructor-arg ref="hikariConfig" />
    </bean>

HikariConfig ๊ฐ์ฒด๋ฅผ HikariDataSource ๊ฐ์ฒด ์ƒ์„ฑ์ž์— ์ธ์ž๋กœ ๋„ฃ์–ด์ค˜์š”.

โœ… MyBatis ์„ค์ •

  • MyBatis ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.koreait.first" />
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>
    
    <mybatis-spring:scan base-package="com.koreait.first"/>
  • MyBatis๋Š” Mapper.interface์™€ Mapper.xml์„ ์กฐํ•ฉํ•˜์—ฌ DAOํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์š”.
  • ๋ฐ์ดํ„ฐ์†Œ์Šค์™€ mapper๋ฅผ ๋ชจ์•„์š”.
  • ๋ฉค๋ฒ„ํ•„๋“œ dataSource๋Š” HikariDataSourceํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ setํ–ˆ์–ด์š”.
  • ๋ฉค๋ฒ„ํ•„๋“œ typeAliasePackage์—๋Š” Mapper.interface๊ฐ€ ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ setํ•ด์š”
  • ๋ฉค๋ฒ„ํ•„๋“œ mapperLocations์—๋Š” Mapper.xml๊ฐ€ ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ setํ•ด์š”.

โœ… applicationContext.xml์— ๋Œ€ํ•œ ๋‚˜์˜ ๋Š๋‚Œ

๋ญ”๊ฐ€ DB์— ๋Œ€ํ•œ ์„ค์ •๋งŒ ํ•ด๋†“์€ ๊ณณ ๊ฐ™์•„์š”.

๐Ÿ”น ์ „์ฒด์ฝ”๋“œ

๐Ÿ‘† ๋งํฌ๋ฅผ ๋ˆ„๋ฅด๋ฉด ์†Œ์Šค๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์–ด์š”.

<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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd">

    <!--์–ด๋””์„œ๋“  classpath๋กœ ์‹œ์ž‘ํ•˜๋ฉด resourcesํด๋”์—์„œ ๋ถ€ํ„ฐ ์ฐพ๊ธฐ ์‹œ์ž‘ํ•œ๋‹ค.-->
    <context:property-placeholder location="classpath:application.properties"/>

    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="driverClassName" value="${db.driver}" />
        <property name="jdbcUrl" value="${db.host}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <constructor-arg ref="hikariConfig" />
    </bean>

    <!--Mybatis ์„ค์ •-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.koreait.first" />
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

    <mybatis-spring:scan base-package="com.koreait.first"/>
</beans>
profile
๋ช‡ ๋ฒˆ์„ ๋„˜์–ด์ ธ๋„ ์•ž์œผ๋กœ ๊ณ„์† ๋‚˜์•„๊ฐ€์ž

0๊ฐœ์˜ ๋Œ“๊ธ€