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

Non-Proxy Custom Scopes #6064

Closed
RyanHoldren opened this issue Aug 26, 2021 · 1 comment · Fixed by #6367
Closed

Non-Proxy Custom Scopes #6064

RyanHoldren opened this issue Aug 26, 2021 · 1 comment · Fixed by #6367
Assignees
Labels
type: bug Something isn't working
Milestone

Comments

@RyanHoldren
Copy link
Contributor

Issue description

I suspect this is not a bug, but rather that I am just misunderstanding how Micronaut works.

Consider this example:

@Documented
@Retention(RUNTIME)
@Scope
@Target({TYPE, METHOD})
public @interface SpecialBean {}

@Singleton
public class SpeciaBeanScope implements CustomScope<SpecialBean> {

	private final Map<BeanIdentifier, CreatedBean<?>> beans = new ConcurrentHashMap<>();

	@Override
	public Class<SpecialBean> annotationType() {
		return SpecialBean.class;
	}

	@Override
	public <T> T getOrCreate(BeanCreationContext<T> context) {
		final var id = context.id();
		return (T) beans.computeIfAbsent(id, key -> context.create()).bean();
	}

	// ...

}

@SpecialBean
public class SpecialService {

}

I expected that SpeciaBeanScope.getOrCreate() would be called for SpecialService but it isn't. It looks like the scope is never looked up by DefaultBeanContext.getScopedBeanForDefinition() because SpecialBean is not annotated with @ScopedProxy.

Here is the relevant code from DefaultBeanContext.getScopedBeanForDefinition():

if (registeredScope == null && !isProxy && isScopedProxyDefinition) {
	registeredScope = customScopeRegistry.findDeclaredScope(definition).orElse(null);
}

This seems very intentional, but I am not sure why. Why do custom scopes have to be proxies? In my case, I cannot simply add @ScopeProxy because some of my beans are final.

@graemerocher graemerocher added the type: bug Something isn't working label Aug 30, 2021
@graemerocher graemerocher added this to the 3.0.1 milestone Aug 30, 2021
@Paullo612
Copy link
Contributor

Looks like correct condition would be

if (registeredScope == null && ((isScopedProxyDefinition && !isProxy) || !isScopedProxyDefinition))

which can be simplified to

if (registeredScope == null && (!isScopedProxyDefinition || !isProxy)).

Also, proxy target bean definition should be searched here only if it is scoped proxy (to make AOP work correctly for non scoped proxies).

Patched locally for now, but looking forward to see this fixed upstream!

It also would be cool to have some API in BeanCreationContext, which could allow to decide whether I should throw NoSuchBeanException from CustomScope#getOrCreate, or just return null (later is relevant when bean is searched through BeanLocator#findBean).

@graemerocher graemerocher modified the milestones: 3.0.1, 3.1.1 Oct 14, 2021
Paullo612 added a commit to Paullo612/micronaut-scopes that referenced this issue Oct 16, 2021
@jameskleeh jameskleeh modified the milestones: 3.1.1, 3.1.2 Oct 18, 2021
@graemerocher graemerocher self-assigned this Oct 19, 2021
graemerocher added a commit that referenced this issue Oct 19, 2021
* Improve exception handling for AbstractConcurrentScope. Fixes #6343
* Fix custom scopes for beans not annotated with ScopedProxy. Fixes #6064
* Don't scoped proxy instances. Fixes #6342
jameskleeh pushed a commit that referenced this issue Oct 19, 2021
* Address issues with bean scopes

* Improve exception handling for AbstractConcurrentScope. Fixes #6343
* Fix custom scopes for beans not annotated with ScopedProxy. Fixes #6064
* Don't scoped proxy instances. Fixes #6342

* simplify

* fix erroneous logic

* elimnate duplicate line
GavrilovSV pushed a commit to GavrilovSV/micronaut-core that referenced this issue Nov 6, 2021
* Address issues with bean scopes

* Improve exception handling for AbstractConcurrentScope. Fixes micronaut-projects#6343
* Fix custom scopes for beans not annotated with ScopedProxy. Fixes micronaut-projects#6064
* Don't scoped proxy instances. Fixes micronaut-projects#6342

* simplify

* fix erroneous logic

* elimnate duplicate line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants