Skip to content

@Primary with Cyclic DI #27668

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
daodefengshang opened this issue Nov 10, 2021 · 5 comments
Closed

@Primary with Cyclic DI #27668

daodefengshang opened this issue Nov 10, 2021 · 5 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@daodefengshang
Copy link

Version: spring-boot-2.5.6

Code:

public interface LoopService {

	void testLoop();
	
	void testInnerLoop();
}
@Service
public class DefaultLoopService implements LoopService {

	private static final Logger log = LoggerFactory.getLogger(DefaultLoopService.class);
	
	@Autowired
	private LoopService loopService;

	@Override
	public void testLoop() {
		log.info("DefaultLoopService");
	}

	@Override
	public void testInnerLoop() {
		log.info("DefaultLoopService--testInnerLoop");
	}

}
@Service
@Primary
public class UsingLoopService implements LoopService {

	private static final Logger log = LoggerFactory.getLogger(UsingLoopService.class);
	
	@Autowired
	private LoopService loopService;
	
	@Override
	public void testLoop() {
		log.info("UsingLoopService");
		loopService.testInnerLoop();
	}

	@Override
	public void testInnerLoop() {
		log.info("UsingLoopService--testInnerLoop");
	}
}
@RestController
public class LoopController {
	
	@Autowired
	private LoopService loopService;

	@GetMapping("/testLoop")
	public void testLoop() {
		loopService.testLoop();
	}
}

Expect Print:

service.impl.UsingLoopService : UsingLoopService
service.impl.UsingLoopService : UsingLoopService--testInnerLoop

But Actual Print:

service.impl.UsingLoopService : UsingLoopService
service.impl.DefaultLoopService : DefaultLoopService--testInnerLoop
@philwebb
Copy link
Member

I think that this is working as expected. You can't inject a bean into itself so UsingLoopService is injected with a DefaultLoopService

@mdeinum
Copy link
Contributor

mdeinum commented Nov 10, 2021

You can't inject a bean into itself

Well actually you can (as of Spring 4.3) so you can call a method on yourself so AOP is applied (instead of using this). See #13096.

@philwebb
Copy link
Member

Thanks @mdeinum, I wasn't aware of that. It looks like that will only work if there's not another candidate to inject. I'll re-open this issue and transfer it to the Framework team to check if the behavior is expected.

@philwebb philwebb reopened this Nov 10, 2021
@philwebb philwebb transferred this issue from spring-projects/spring-boot Nov 10, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 10, 2021
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 11, 2021
@JintaoXIAO
Copy link

the logic locates in org.springframework.beans.factory.support.DefaultListableBeanFactory@L1533
@daodefengshang

@snicoll
Copy link
Member

snicoll commented Nov 22, 2023

I don't think you should be doing that at all for a start. Cycles are highly discouraged and the self-injection for AOP purpose is actually a design smell. We'll share more details in #28299

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Nov 22, 2023
@snicoll snicoll 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 or decided on labels Nov 22, 2023
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) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

7 participants