스프링 주석 자동 주입 (서비스, DAO)

기본적으로 스프링 으로 bean 을 생 성 하 는 인 스 턴 스 를 @ Component 로 알려 주 고, 이 bean 에 사용 되 는 다른 bean 을 @ Resource 로 주입 (발사) 합 니 다.좋 은 점 은 스프링 프로필 을 쓰 지 않 아 도 된다 는 것 이다. set 방법 을 쓰 지 않 아 도 된다 는 것 이다.
@ Component 는

@ Resource 는 < bean > ↓ 에 사 용 된 다른 대상 < / bean > 에 해당 합 니 다.

 
 1. @Component + @Resource :
 
applicationContext.xml
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   ">
	<!-- <context:annotation-config />   component-scan,       -->
	<context:component-scan base-package="test.service.impl" />
	<context:component-scan base-package="test.dao.impl" />

       
        <!--      @Component,          -->
        <!-- <bean id="testDao" class="test.dao.impl.TestDAOImpl" /> -->
	<!-- <bean id="testService" class="test.service.impl.TestServiceImpl" /> -->
</beans>

 
 
TestDAO.java
package test.dao;

public interface TestDAO {

	public void insert();
}

 
TestDAOImpl.java
package test.dao.impl;

import org.springframework.stereotype.Component;
import test.dao.TestDAO;

@Component
public class TestDAOImpl implements TestDAO {

	@Override
	public void insert() {
		// TODO     されたメソッド・スタブ
		System.out.println("invoke dao success!");
	}

}

 
TestService.java
package test.service;

public interface TestService {

	public void testDaoInsert();
}

 
 TestServiceImpl.java
package test.service.impl;

import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import test.dao.TestDAO;
import test.service.TestService;

@Component
public class TestServiceImpl implements TestService {

    @Resource
    private TestDAO testDao;

    @Override
    public void testDaoInsert() {
        // TODO     されたメソッド・スタブ
        testDao.insert();
        System.out.println("invoke service success!");
    }
}

 
Action 에서 호출: (struts 2 와 spring 의 관련 설정 과 해당 하 는 jsp 는 생략 합 니 다...)
package tutorial.hello;

import javax.annotation.Resource;
import test.service.TestService;
import com.opensymphony.xwork2.ActionSupport;

public class HelloAction {

    @Resource
    private TestService testService;

    public String doHello() {
    	testService.testDaoInsert();
        return ActionSupport.SUCCESS;
    }
}

 
실행 결과
invoke dao success!
invoke service success!
 
2. @Component + @Autowired :
1 의 리 소스 를 Autowired 로 바 꾸 면 돼 요.
 
3. @Component
("") + @Autowired + @Qualifier("") :
(") spring 에서 생 성 된 bean 의 id 를 지정 합 니 다. @ Qualifier (") 를 사용 하면 반드시 component (") 의 bean id 를 지정 해 야 합 니 다.
TestServiceImpl.java
package test.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import test.dao.TestDAO;
import test.service.TestService;

@Component("testService")
//@Component
public class TestServiceImpl implements TestService {

    @Autowired
    @Qualifier("testDao")
//	@Resource
	private TestDAO testDao;

	@Override
	public void testDaoInsert() {
		// TODO     されたメソッド・スタブ
		testDao.insert();
		System.out.println("invoke service success");
	}

}
 
TestDAOImpl.java
package test.dao.impl;

import org.springframework.stereotype.Component;
import test.dao.TestDAO;

@Component("testDao")
//@Component
public class TestDAOImpl implements TestDAO {
	@Override
	public void insert() {
		// TODO     されたメソッド・スタブ
		System.out.println("invoke dao success!");
	}
}

좋은 웹페이지 즐겨찾기