spring-framework: Lazy dependency proxy does not populate bean dependencies

Start off with the latest spring-petclinic project based on Boot 2.3.x Launch the app and see the JSON response for /actuator/beans Search for OwnerController bean. This bean should depend on OwnerRepository and PetRepository. The JSON for the bean however is this:

        "ownerController": {
          "aliases": [],
          "scope": "singleton",
          "type": "org.springframework.samples.petclinic.owner.OwnerController",
          "resource": "file [/Users/aboyko/Documents/runtime-sts4/spring-petclinic/target/classes/org/springframework/samples/petclinic/owner/OwnerController.class]",
          "dependencies": []
        },

If spring-petclinic is switched to Boot 2.2.x then the dependencies are detected correctly:

        "ownerController": {
          "aliases": [],
          "scope": "singleton",
          "type": "org.springframework.samples.petclinic.owner.OwnerController",
          "resource": "file [/Users/aboyko/Documents/runtime-sts4/spring-petclinic/target/classes/org/springframework/samples/petclinic/owner/OwnerController.class]",
          "dependencies": [
            "ownerRepository",
            "visitRepository"
          ]
        },

Something is off in the framework as actuator simply calls org.springframework.beans.factory.config.ConfigurableBeanFactory.getDependenciesForBean(String)

Try this in the org.springframework.samples.petclinic.PetClinicApplication for the main method:

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(PetClinicApplication.class, args);

		final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

		final Runnable printDependnecies = new Runnable() {
			public void run() {
				String[] dependenciesForBean = context.getBeanFactory().getDependenciesForBean("ownerController");
				
				if (dependenciesForBean.length == 0) {
					System.out.println("no dependencies!!!");
				} else {
					System.out.println("Dependencies:");
					for (String dep : dependenciesForBean) {
						System.out.println(dep);
					}
					System.out.println();
				}
			}
		};

		scheduler.scheduleAtFixedRate(printDependnecies, 10, 10, TimeUnit.SECONDS);

	}

You’d see that there are no dependencies returned in the Boot 2.3.x case.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Please note that dependencies behind a lazy proxy are only going to be registered once the proxy is actually being initialized, that is, once a method invocation on the proxy led to the resolution of the target instance. That’s the part that the fix addressed here.

Very nice detective work @wilkinsona!

@BoykoAlex you can workaround the problem in the meantime adding the following to application.properties:

spring.data.jpa.repositories.bootstrap-mode=default

Thank you. I’ve reproduced the problem and it does not occur with Spring Boot 2.2.9.RELEASE indeed.