Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail fast if a @Bean method has a void return type #31007

Closed
snicoll opened this issue Aug 7, 2023 · 5 comments
Closed

Fail fast if a @Bean method has a void return type #31007

snicoll opened this issue Aug 7, 2023 · 5 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@snicoll
Copy link
Member

snicoll commented Aug 7, 2023

It is currently possible to craft the following faulty configuration:

@Configuration
public class FaultyConfiguration {

	@Bean
	public void faultyBean() {
		System.out.println("Hello");
	}

}

As a result of processing this configuration class, "Hello" is displayed during the processing phase and a NullBean bean is contributed. We should reject this upfront.

@snicoll snicoll added the type: enhancement A general enhancement label Aug 7, 2023
@snicoll snicoll added this to the 6.1.x milestone Aug 7, 2023
@snicoll snicoll added the in: core Issues in core modules (aop, beans, core, context, expression) label Aug 7, 2023
@snicoll snicoll changed the title Fail fast if a @Bean method as a void return type Fail fast if a @Bean method has a void return type Aug 7, 2023
@jhoeller jhoeller self-assigned this Aug 7, 2023
@jhoeller jhoeller modified the milestones: 6.1.x, 6.1.0-M4 Aug 7, 2023
@jamesmoessis
Copy link

jamesmoessis commented Nov 20, 2023

Hi @snicoll - In 6.1.0 this is causing a problem for us. I was using void beans to run setup methods, pairing them with the @Conditional* annotations. For example, enabling certain reactor hooks, if reactor is on the class path:

    @Configuration
    @ConditionalOnClass(Mono.class)
    static class ReactiveConfiguration {
        @Bean
        void enableAutomaticContextPropagation() {
            Hooks.enableAutomaticContextPropagation();
        }
    }

I couldn't see any other way to do this, since @PostConstruct doesn't work with the annotations I need. Is there an alternative now that 6.1.0 blocks this behaviour?

@ttddyy
Copy link
Contributor

ttddyy commented Nov 20, 2023

@jamesmoessis fyi, see this ReactorAutoConfiguration class in Spring Boot.

@jamesmoessis
Copy link

Perfect! Thanks @ttddyy

@snicoll
Copy link
Member Author

snicoll commented Nov 20, 2023

Thanks @ttddyy. @jamesmoessis I don't see really how that change blocks anything considering it was abusing @Bean factory method. The reference guide has a number of alternatives for bean initialization. Remember that a @Configuration class is also a bean so it could have implemented InitializingBean if @PostConstruct does not work for you. However, the constructor-based approach is better as it is invoked early.

@GeneralNitin
Copy link

GeneralNitin commented May 9, 2024

Thanks @ttddyy. @jamesmoessis I don't see really how that change blocks anything considering it was abusing @Bean factory method. The reference guide has a number of alternatives for bean initialization. Remember that a @Configuration class is also a bean so it could have implemented InitializingBean if @PostConstruct does not work for you. However, the constructor-based approach is better as it is invoked early.

Can you please attach an example.
In my case, I was doing something like this, and after upgrading to Spring 3.2, my build started failing.

    @Bean
    @DependsOn(value = { "bean1" })
    public void initSystemProperty() {
        System.setProperty("someProperty", someValue);
    }

    @Bean
    @DependsOn(value = { "initSystemProperty" })
    public void performAnotherAction() {
        // due some more action
    }

Can you please suggest how I should refactor this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants