Skip to content

Commit fc699b2

Browse files
committed
@bean provides autowireCandidate flag (analogous to XML definitions)
Issue: SPR-16204
1 parent 182243d commit fc699b2

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

spring-context/src/main/java/org/springframework/context/annotation/Bean.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -251,6 +251,14 @@
251251
*/
252252
Autowire autowire() default Autowire.NO;
253253

254+
/**
255+
* Is this bean a candidate for getting autowired into some other bean?
256+
* <p>Default is {@code true}; set this to {@code false} for internal delegates
257+
* that are not meant to get in the way of beans of the same type in other places.
258+
* @since 5.1
259+
*/
260+
boolean autowireCandidate() default true;
261+
254262
/**
255263
* The optional name of a method to call on the bean instance during initialization.
256264
* Not commonly used, given that the method may be called programmatically directly

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
232232
beanDef.setAutowireMode(autowire.value());
233233
}
234234

235+
boolean autowireCandidate = bean.getBoolean("autowireCandidate");
236+
if (!autowireCandidate) {
237+
beanDef.setAutowireCandidate(false);
238+
}
239+
235240
String initMethodName = bean.getString("initMethod");
236241
if (StringUtils.hasText(initMethodName)) {
237242
beanDef.setInitMethodName(initMethodName);

spring-context/src/test/java/org/springframework/context/annotation/configuration/BeanAnnotationAttributePropagationTests.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import org.junit.Test;
2020

21+
import org.springframework.beans.factory.annotation.Autowire;
2122
import org.springframework.beans.factory.config.BeanDefinition;
2223
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2324
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -41,9 +42,30 @@
4142
* correctly into the resulting BeanDefinition
4243
*
4344
* @author Chris Beams
45+
* @author Juergen Hoeller
4446
*/
4547
public class BeanAnnotationAttributePropagationTests {
4648

49+
@Test
50+
public void autowireMetadataIsPropagated() {
51+
@Configuration class Config {
52+
@Bean(autowire=Autowire.BY_TYPE) Object foo() { return null; }
53+
}
54+
55+
assertEquals("autowire mode was not propagated",
56+
AbstractBeanDefinition.AUTOWIRE_BY_TYPE, beanDef(Config.class).getAutowireMode());
57+
}
58+
59+
@Test
60+
public void autowireCandidateMetadataIsPropagated() {
61+
@Configuration class Config {
62+
@Bean(autowireCandidate=false) Object foo() { return null; }
63+
}
64+
65+
assertFalse("autowire candidate flag was not propagated",
66+
beanDef(Config.class).isAutowireCandidate());
67+
}
68+
4769
@Test
4870
public void initMethodMetadataIsPropagated() {
4971
@Configuration class Config {
@@ -138,7 +160,7 @@ public void eagerBeanOverridesDefaultLazyConfiguration() {
138160

139161
@Test
140162
public void eagerConfigurationProducesEagerBeanDefinitions() {
141-
@Lazy(false) @Configuration class Config { // will probably never happen, doesn't make much sense
163+
@Lazy(false) @Configuration class Config { // will probably never happen, doesn't make much sense
142164
@Bean Object foo() { return null; }
143165
}
144166

0 commit comments

Comments
 (0)