Skip to content

Support disabling @Autowired on a per-@Bean basis [SPR-8396] #13043

Closed as not planned
@spring-projects-issues

Description

@spring-projects-issues

Stepan Koltsov opened SPR-8396 and commented

public class AutowireOverrides {

    public static class TheBean {

        @Autowired
        @Value("from property")
        public void setBla(String value) {
            System.out.println("setBla: " + value);
        }
    }

    @Configuration
    public static class ContextConfiguration {

        @Bean
        public TheBean theBean() {
            TheBean r = new TheBean();
            r.setBla("from @Bean");
            return r;
        }

    }

    public static void main(String[] args) {
        new AnnotationConfigApplicationContext(ContextConfiguration.class);
    }

}

Obviously outputs

setBla: from @Bean
setBla: from property

and property specified in @Bean method has lower priority.

Requesting a @Bean(properties=...) attribute, so theBean() declaration could be written as:

@Bean(
  properties={
    @Property(name="bla", value="from @Bean")
  }
)
public TheBean theBean() {
    return new TheBean();
}

In this case Spring would call setBla only once with "from @Bean" parameter value.

Assume HttpServer library class with @Autowired threadPool property. I cannot declare two HttpServer instances that use different thread pools in the same application (without disabling autowire, switching to xmlconf etc).


Affects: 3.1 M1

Attachments:

Issue Links:

1 votes, 4 watchers

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions