본문 바로가기

프로그래밍/Spring & MyBatis

[Spring Framework]Bean 등록 방법

출처 : http://www.mungchung.com/

1. xml 을 이용한 등록 방법 - setter 이용

1) applicationContext.xml

<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
    <property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
 
<bean id="loginDAO" class="com.mungchung.sample.login.LoginDAOImpl">
    <property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"/>
</bean>

 2) Bean

public class LoginDAOImpl implements LoginDAO {
    private SqlMapClientTemplate sqlMapClientTemplate;
 
    public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
        this.sqlMapClientTemplate = sqlMapClientTemplate;
    }
}

 

 

2. xml 을 이용한 등록 방법 - 생성자 이용

1) applicationContext.xml

<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
    <property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
 
<bean id="loginDAO" class="com.mungchung.sample.login.LoginDAOImpl">
    <constructor-arg name="sqlMapClientTemplate" ref="sqlMapClientTemplate"/>
</bean>

2) Bean

public class LoginDAOImpl implements LoginDAO {
    private SqlMapClientTemplate sqlMapClientTemplate;
 
    public LoginDAOImpl(SqlMapClientTemplate sqlMapClientTemplate) {
        this.sqlMapClientTemplate = sqlMapClientTemplate;
    }
}

 

 

3. 어노테이션 이용 - 직접 Bean 등록

1) servlet-context.xml

<context:annotation-config/>
<bean id="loginDAO" class="com.mungchung.sample.login.LoginDAOImpl"/>

2) Bean

public class LoginDAOImpl implements LoginDAO {
    @Autowired
    private SqlMapClientTemplate sqlMapClientTemplate;
}


 

 

4. 어노테이션 이용 - Component-Scan 이용

1) servlet-context.xml

<context:component-scan base-package="com.mungchung.sample.login"/>

2) Bean

@Repository
public class LoginDAOImpl implements LoginDAO {
    @Autowired
    private SqlMapClientTemplate sqlMapClientTemplate;
}