Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

java.lang.IllegalStateException: Multiple privileged packages #1414

Closed
sergey-morenets opened this issue Dec 31, 2021 · 4 comments
Closed

java.lang.IllegalStateException: Multiple privileged packages #1414

sergey-morenets opened this issue Dec 31, 2021 · 4 comments
Assignees
Labels
type: documentation A documentation task
Milestone

Comments

@sergey-morenets
Copy link

Hi

We tried to use spring-native capabilities for our project but we failed to build it using Gradle: gradle clean assemble:

Caused by: java.lang.IllegalStateException: Multiple privileged packages: [demo.job.base, demo.job]
at org.springframework.aot.context.bootstrap.generator.infrastructure.ProtectedAccessAnalysis.getPrivilegedPackageName(ProtectedAccessAnalysis.java:57)
at org.springframework.aot.context.bootstrap.generator.bean.DefaultBeanRegistrationWriter.writeBeanRegistration(DefaultBeanRegistrationWriter.java:104)
at org.springframework.aot.context.bootstrap.generator.ApplicationContextAotProcessor.writeBeanDefinitions(ApplicationContextAotProcessor.java:128)
... 15 more

The error message doesn't actually explain what the error is and how to resolve it. Our investigation showed that this error occurs because of protected field being injected in Spring bean:

public abstract class BaseJob {
	private final static Logger LOGGER = LoggerFactory.getLogger(BaseJob.class);

	@Autowired
	protected Service service;

If we change field access to public then our project is built successfully:

public abstract class BaseJob {
	private final static Logger LOGGER = LoggerFactory.getLogger(BaseJob.class);

	@Autowired
	public Service service;

So what is the error reason? Does spring-native not support field injection if field has private/protected access? Unfortunately I couldn't find any explanation in the documentation: https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/

But if it's the case I would update the error message and make it more informative.

@sergey-morenets
Copy link
Author

What is interesting is Spring AOT build works fine if Spring bean has private field being injected:

public class DemoJob extends BaseJob {
	private final static Logger LOGGER = LoggerFactory.getLogger(DemoJob.class);

	@Autowired(required = false)
	private List<Handler> handler;

But it fails to build for field injection in the base class.

@snicoll
Copy link
Contributor

snicoll commented Dec 31, 2021

@sergey-morenets the fact that it works with private injection is just a corner case, really. The problem is injecting privileged components in a parent class that sits in another package. Spring AOT is turning your arrangement into a single programmatic equivalent that shows that your bean cannot be created "manually" (in a unit test, for instance).

Spring Native requires such a bean to be created manually so reviewing this arrangement is all that I can recommend at this time.

We're going to improve the error message in #1005. See also #1402.

@snicoll snicoll closed this as completed Dec 31, 2021
@snicoll snicoll added the status: duplicate A duplicate of another issue label Dec 31, 2021
@sergey-morenets
Copy link
Author

@snicoll

Thank you for the explanation. Can you please add it to the documentation?

@snicoll snicoll reopened this Jan 1, 2022
@snicoll snicoll added type: documentation A documentation task and removed status: duplicate A duplicate of another issue labels Jan 1, 2022
@snicoll snicoll added this to the 0.11.2 milestone Jan 1, 2022
@snicoll
Copy link
Contributor

snicoll commented Jan 1, 2022

Good point, let's do that.

@snicoll snicoll self-assigned this Jan 5, 2022
@snicoll snicoll closed this as completed in 62b0f9e Jan 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: documentation A documentation task
Development

No branches or pull requests

2 participants