스프링 주석 자동 주입 (서비스, DAO)
@ 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!");
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.