From 02362f43476bbe011ced774c3c862f3a3cc051e2 Mon Sep 17 00:00:00 2001 From: Michael Isvy Date: Fri, 22 Jul 2011 01:18:29 +0000 Subject: [PATCH] SPR-7858 Updating references to @Resource usage --- spring-framework-reference/src/aop.xml | 4 +- spring-framework-reference/src/dao.xml | 2 +- spring-framework-reference/src/testing.xml | 108 ++++----------------- 3 files changed, 20 insertions(+), 94 deletions(-) diff --git a/spring-framework-reference/src/aop.xml b/spring-framework-reference/src/aop.xml index d1844d4cf73..504b32a85d5 100644 --- a/spring-framework-reference/src/aop.xml +++ b/spring-framework-reference/src/aop.xml @@ -2869,8 +2869,8 @@ public class Account { autowiring by type or by name respectively. As an alternative, as of Spring 2.5 it is preferable to specify explicit, annotation-driven dependency injection for your @Configurable - beans by using @Autowired and - @Resource at the field or method level (see + beans by using @Autowired or + @Inject at the field or method level (see for further details). Finally you can enable Spring dependency checking for the object diff --git a/spring-framework-reference/src/dao.xml b/spring-framework-reference/src/dao.xml index c8eec310719..7a20be23637 100644 --- a/spring-framework-reference/src/dao.xml +++ b/spring-framework-reference/src/dao.xml @@ -87,7 +87,7 @@ public class SomeMovieFinder implements MovieFinder { DataSource; a JPA-based repository will need access to an EntityManager. The easiest way to accomplish this is to have this resource dependency injected using one of - the @Autowired, + the @Autowired,, @Inject, @Resource or @PersistenceContext annotations. Here is an example for a JPA repository: diff --git a/spring-framework-reference/src/testing.xml b/spring-framework-reference/src/testing.xml index 50093466ac3..450c1eb5ee1 100644 --- a/spring-framework-reference/src/testing.xml +++ b/spring-framework-reference/src/testing.xml @@ -1110,19 +1110,10 @@ public class ExtendedTest extends BaseTest { @ContextConfiguration. You may use setter injection, field injection, or both, depending on which annotations you choose and whether you place them on setter methods or fields. For - consistency with the annotation support introduced in Spring 2.5, you + consistency with the annotation support introduced in Spring 3.0, you can use Spring's @Autowired annotation - or the @Resource annotation from JSR - 250. As of Spring 3.0 you may alternatively use the - @Inject annotation from JSR 330. For - example, if you prefer autowiring by - type, annotate your setter methods or fields with - @Autowired or - @Inject. If you prefer to have your - dependencies injected by name, annotate your - setter methods or fields with - @Resource. + or the @Inject annotation from JSR + 300. The TestContext framework does not instrument the manner in @@ -1132,27 +1123,24 @@ public class ExtendedTest extends BaseTest { effect for test classes. - Because @Autowired performs Because @Autowired used solely performs autowiring by type, if you have multiple bean definitions of the same type, you cannot rely on this approach for those particular - beans. In that case, you can use - @Resource for injection by - name. Alternatively, if your test class has access to its - ApplicationContext, you can perform an explicit - lookup by using (for example) a call to - applicationContext.getBean("titleRepository"). A - third option is to use @Autowired in + beans. In that case, you can use @Autowired in conjunction with @Qualifier. As of Spring 3.0 you may also choose to use @Inject in conjunction with - @Named. + @Named. + Alternatively, if your test class has access to its + ApplicationContext, you can perform an explicit + lookup by using (for example) a call to + applicationContext.getBean("titleRepository"). If you do not want dependency injection applied to your test instances, simply do not annotate fields or setter methods with - @Autowired, - @Inject or - @Resource. Alternatively, you can + @Autowired or + @Inject. Alternatively, you can disable dependency injection altogether by explicitly configuring your class with @TestExecutionListeners and omitting @@ -1161,10 +1149,9 @@ public class ExtendedTest extends BaseTest { Consider the scenario of testing a HibernateTitleRepository class, as outlined in the Goals section. The next four - code listings demonstrate the use of @Autowired - and @Resource on fields and - setter methods. The application context configuration is presented + linkend="integration-testing-goals">Goals section. The next two + code listings demonstrate the use of @Autowired on fields + and setter methods. The application context configuration is presented after all sample code listings. @@ -1227,49 +1214,7 @@ public class HibernateTitleRepositoryTests { } } - The following is an example of using @Resource - for field injection. - - @RunWith(SpringJUnit4ClassRunner.class) -// specifies the Spring configuration to load for this test fixture -@ContextConfiguration("repository-config.xml") -public class HibernateTitleRepositoryTests { - - // this instance will be dependency injected by name - @Resource - private HibernateTitleRepository titleRepository; - - @Test - public void loadTitle() { - Title title = titleRepository.loadTitle(new Long(10)); - assertNotNull(title); - } -} - - Here is an example of using @Resource - for setter injection. - - @RunWith(SpringJUnit4ClassRunner.class) -// specifies the Spring configuration to load for this test fixture -@ContextConfiguration("repository-config.xml") -public class HibernateTitleRepositoryTests { - - // this instance will be dependency injected by name - private HibernateTitleRepository titleRepository; - - @Resource - public void setTitleRepository(HibernateTitleRepository titleRepository) { - this.titleRepository = titleRepository; - } - - @Test - public void loadTitle() { - Title title = titleRepository.loadTitle(new Long(10)); - assertNotNull(title); - } -} + The preceding code listings use the same XML context file referenced by the @ContextConfiguration @@ -1325,26 +1270,7 @@ public class HibernateTitleRepositoryTests { also point to a specific bean by name there (as shown above, assuming that "myDataSource" is the bean id). - Alternatively, consider using the - @Resource annotation on such - overridden setter methods. This allows you to specify the name of - the target bean explicitly, but without type matching semantics. - In contrast to the solution above that combined - @Autowired and - @Qualifier, using - @Resource results in the - selection of a bean with that specific name, regardless of - how many beans of the given type exist in the context. - - // ... - - @Resource("myDataSource") - @Override - public void setDataSource(DataSource dataSource) { - super.setDataSource(dataSource); - } - -// ... +