Spring annotation 自动流入(Service,DAO)
Spring annotation 自动注入(Service,DAO)
基本上就是用@Component提示Spring生成bean的实例,用@Resource把该bean中用到的其他bean注入(射)进去。。。 好处就是你不用再写spring配置文件了,不用再写set方法了。。。
@Component 相当于
<bean id="xxxx" class="XXXXX.XXXXX" />
@Resource 相当于<bean>↓使用到的其他对象</bean>
<property name="sessionFactory" ref="mySessionFactory" />
<bean id="xxxx" class="XXXXX.XXXXX" />
@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" 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中调用一下:(至于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; } }
运行结果
invoke dao success!
invoke service success!
invoke service success!
2. @Component + @Autowired :
就把1中的Resource换成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!"); } }