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

Allow @ConditionalOn to work on @Components #17655

Closed
xenoterracide opened this issue Jul 26, 2019 · 5 comments
Closed

Allow @ConditionalOn to work on @Components #17655

xenoterracide opened this issue Jul 26, 2019 · 5 comments

Comments

@xenoterracide
Copy link
Contributor

xenoterracide commented Jul 26, 2019

@Service
@ConditionalOnMissingBean(PatientService::class)
class DetachedPatientService(private val emrRepo: EmrRepository) : PatientService {

I get this result

Parameter 2 of constructor in com.potreromed.gateway.usecase.InitialFoleySetupApplicationService required a bean of type 'com.potreromed.gateway.model.PatientService' that could not be found.
    Action:
    Consider defining a bean of type 'com.potreromed.gateway.model.PatientService' in your configuration.

this does work... but it's so much more verbose

@Configuration
open class DetachedConfig {

    @Bean
    @ConditionalOnMissingBean
    open fun detachedPatientService(repo: EmrRepository): PatientService {
        return DetachedPatientService(repo)
    }
}

it would be nice if we could use @ConditionalOnMissingBean and other @Conditional's on @Component's.

P.S. I can create a full demo project if necessary.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 26, 2019
@philwebb
Copy link
Member

@xenoterracide How are your service beans being imported? I believe that Spring Framwork's @Condition support should work on components, however, Spring Boot conditions (and @ConditionalOn...Bean especially) are very sensitive to when they run. You can really only use them on auto-configuration classes. There's a small warning about this in the javadoc.

@philwebb philwebb added the status: waiting-for-feedback We need additional information before we can continue label Jul 26, 2019
@xenoterracide
Copy link
Contributor Author

imported? I presume you're asking how I'm injecting them... (otherwise just the normal import) I'm using constructor injection into other services.

@Service
open class InitialFoleySetupApplicationService(
    private val foleyRepo: FoleyRepository,
    private val emrRepo: EmrRepository,
    private val patientSvc: PatientService,
    private val mfcRepo: MonitorFoleyConnectionRepository,
    private val monitorRepo: MonitorRepository,
    private val site: SiteConfig
) {

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jul 26, 2019
@snicoll
Copy link
Member

snicoll commented Jul 27, 2019

@xenoterracide using conditions on components identified by classpath scanning will not work. You should really use those on auto-configurations (see the note in the javadoc that Phil already linked).

Ignoring the fact using such conditions that way is not supported, I don't know why your project fails the way you've described but it looks like a different problem to me. If you share a small sample we can have a look.

@snicoll snicoll added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Jul 27, 2019
@spring-projects-issues
Copy link
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Aug 3, 2019
@spring-projects-issues
Copy link
Collaborator

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged labels Aug 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants