简体   繁体   中英

Why is the Spring Framework Not Logging Autowired Setter Injection?

I'm new to Spring and I'm currently learning dependency injection. I'm using Spring Boot 2.2.2

If I used Constructor based instruction, I see in the logs that it is being autowired.

Here is my code:

// Sorting an array
@Autowired
private SortAlgrithm sortAlgorithm;
//^Tell Spring this is a dependency

public BinarySearchImpl(SortAlgrithm sortAlgorithm) {
    super();
    this.sortAlgorithm = sortAlgorithm;
}

And here are my logs where it reflects the Autowiring process handled by Spring:

2020-01-04 20:34:30.435 DEBUG 10396 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'springIn10StepsApplication'
2020-01-04 20:34:30.441 DEBUG 10396 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'binarySearchImpl'
2020-01-04 20:34:30.446 DEBUG 10396 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'bubbleSortAlgorithm'
2020-01-04 20:34:30.447 DEBUG 10396 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Autowiring by type from bean name 'binarySearchImpl' via constructor to bean named 'bubbleSortAlgorithm'

Now, if I change the code slightly by using setter injection:

    private SortAlgorithm sortAlgorithm;

    // Setter injection
    @Autowired
    public void setSortAlgorithm(SortAlgorithm sortAlgorithm) {
        System.out.println("setter called");
        this.sortAlgorithm = sortAlgorithm;
    }

And providing the logs:

2020-01-04 20:37:23.853 DEBUG 7600 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'org.springframework.boot.context.internalConfigurationPropertiesBinderFactory'
2020-01-04 20:37:23.858 DEBUG 7600 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'springIn10StepsApplication'
2020-01-04 20:37:23.863 DEBUG 7600 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'binarySearchImpl'
2020-01-04 20:37:23.870 DEBUG 7600 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'bubbleSortAlgorithm'
setter called

I included the 'setter called' to be printed out to let me know that the setter injection was being invoked because it wasn't being included in the logs. I was wondering if this is a fluke by Spring and if others are experiencing the same thing. I understand that my beans are being managed at the applciation context, and, at runtime, Spring is looking for the dependencies. @Autowire tells Spring what my dependencies are and injects appropriately. The first log represents that behavior. But, the fact that 'setter called' is printed out, I was wondering if it implies that Spring is performing Autowiring by invoking the setter method even if it is not reflected in the logs.

In your first case the setter injection is not even fired, its constructor injection thats happening there and hence no logs.

From the docs

Autowired Constructors :

.....

If a class only declares a single constructor to begin with, it will always be used, even if not annotated. An annotated constructor does not have to be public.

For instance if you have something like this:

@Component
public class TestAbd {

    @Autowired
    private OtpService otpService;

    public TestAbd(OtpService otpService) {
        System.out.println("Setting OTP service thorugh constructor injection.");
        this.otpService = otpService;
    }

    public void setOtpService(OtpService otpService) {
        System.out.println("Setting OTP service thorugh setter injection.");
        this.otpService = otpService;
    }

}

You will have the following logs

...

Setting OTP service thorugh constructor injection.
2020-01-05 10:44:50:743 [main] 

...

So in your first case, since you have declared a single constructor it will always be used for setting your dependencies and hence the whole setter injection thingy is skipped.

Try logging.level.org.springframework=TRACE .

The debug output depends on the springframework version.

给定日志的示例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM