Skip to content

EntityScanPackages has package-private constructor #28232

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

Closed
riba2101 opened this issue Oct 7, 2021 · 7 comments
Closed

EntityScanPackages has package-private constructor #28232

riba2101 opened this issue Oct 7, 2021 · 7 comments
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@riba2101
Copy link

riba2101 commented Oct 7, 2021

Hello,

is there a reason why EntityScanPackages has a package-private constructor?
The whole thing about entity scanning is extensible, but the class is "hindering" this?

BR

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 7, 2021
@snicoll
Copy link
Member

snicoll commented Oct 7, 2021

What are you trying to achieve and why do you think you should be creating your own instance of EntityScanPackages?

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Oct 7, 2021
@riba2101
Copy link
Author

riba2101 commented Oct 7, 2021

Hi,

well, a lib that has entities and with this the consumer of a lib would not need to always use @EntityScan("{their package}", "{lib package}")

@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 Oct 7, 2021
@riba2101
Copy link
Author

riba2101 commented Oct 7, 2021

Btw after a bit of looking into it, it would be super nice to be able to merge annotations like EnableJpaRepositories?

@wilkinsona
Copy link
Member

It sounds like using @AutoConfigurationPackage may do what you want. It's not yet very well documented so please see #27549 for some more details.

@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Oct 7, 2021
@riba2101
Copy link
Author

riba2101 commented Oct 7, 2021

I'm not quite sure if @AutoConfigurationPackageis intended for this. (and the lack of the docs is just baffling)
Let's say you are writing an auditing lib.
Let's say that you want to support and use various already existing integrations from spring as a basis (spring-data-jpa as one provider, spring-data-elastic as another etc)

The lib itself knows where the configurations are so it would make sense to configure the defaults for it because those will be used in 98% of the cases.

what im proposing is to have @Repeatable or some other type of merge on all @Enable... annotations.

what is happening here is that:
there are 3 projects and all have their own custom @Enable annotations:

  • rmi lib(the name is misleading - ignore i,t it's not important)
  • storage lib
  • storage audit lib

The following is the setup(sry, code example would be too big and complex):

  • Component scan happens in rmi -> so autoconfiguration helps here, the stack starts, where to scan etc
  • Storage lib bootstraps based on selected impl (or multiple) the whole, for example, JPA stack (there are custom things like QueryDSL extensions etc). Among other things it has out of "convenience" @EnableJpaRepositories on its own @EnableJpa
  • storage audit lib also has its own @Enable... annotation and now here we have the problem

I would need another @EnableJpaRepositories for example, but this one gets swallowed.

maybe for visual reference:

JpaAuditTestConfiguration

@SpringBootApplication(scanBasePackages = "test.app.jpa")
@EntityScan(basePackages = {"test.app.jpa"})
@EnableStorageJpa(basePackages = {"test.app.jpa"})
@EnableStorageAuditJpa
public class JpaAuditTestConfiguration {
}

EnableStorageJpa

@EnableTransactionManagement
@EnableJpaRepositories
public @interface EnableCloudStorageJpa {
....
    @AliasFor(annotation = EnableJpaRepositories.class, attribute = "basePackages")
    String[] basePackages();
...
}

EnableStorageAuditJpa

@EnableJpaRepositories
@EnableAspectJAutoProxy
public @interface EnableStorageAuditJpa {
...
    @AliasFor(annotation = EnableJpaRepositories.class, attribute = "basePackages")
    String[] basePackages() default {"lib.storage.audit.provider.jpa"};
...
}

now, yes, i could slam the @EnableJpaRepositories onto a config class, hardcode it and import it...but then you couldn't configure DataSource, TX, etc...if for example, someone would like to keep them somewhere else.

In addition to this problem, there's also @EntityScan who behaves the same - that's probably why you proposed the
@AutoConfigurationPackage @wilkinsona rights?

@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 Oct 7, 2021
@philwebb
Copy link
Member

philwebb commented Oct 7, 2021

I'm not really following the above example I'm afraid, but it won't be possible to make @Enable annotations @Repeatable. I also don't think we want to make the constructor of EntityScanPackages public.

If you want to register additional packages for entity scanning you can use the EntityScanPackages.register static methods.

@philwebb philwebb closed this as completed Oct 7, 2021
@philwebb philwebb added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Oct 7, 2021
@riba2101
Copy link
Author

riba2101 commented Oct 8, 2021

This is what I'm doing, but there is no such (easy) thing for repositories. What I'm saying is that for example @EnableJpaRepositories will be scanned multiple times if present on multiple config classes, but won't if present on annotations.

This is java related but still would say a bit inconsistent in spring?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

5 participants