Spring annotation 自动注入(Service,DAO)
@Resource 相当于<bean>↓使用到的其他对象</bean>
<property name="sessionFactory" ref="mySessionFactory" />
?
?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" /> --><!-- <bean id="testService" /> --></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;@Componentpublic class TestDAOImpl implements TestDAO {@Overridepublic 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;@Componentpublic class TestServiceImpl implements TestService { @Resource private TestDAO testDao; @Override public void testDaoInsert() { // TODO 自生成されたメソッド?スタブ testDao.insert(); System.out.println("invoke service success!"); }}?
在Action中调用一下:(至于struts2和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; }}?
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")//@Componentpublic class TestServiceImpl implements TestService { @Autowired @Qualifier("testDao")//@Resourceprivate TestDAO testDao;@Overridepublic void testDaoInsert() {// TODO 自生成されたメソッド?スタブtestDao.insert();System.out.println("invoke service success");}}?package test.dao.impl;import org.springframework.stereotype.Component;import test.dao.TestDAO;@Component("testDao")//@Componentpublic class TestDAOImpl implements TestDAO {@Overridepublic void insert() {// TODO 自生成されたメソッド?スタブSystem.out.println("invoke dao success!");}}?