spring @Autowire property vs setter

前端 未结 7 1812
抹茶落季
抹茶落季 2021-01-30 16:40

What is the difference between anotate @Autowired to a property or do it in the setter?

As far as I know they both have the same result, but is there any reason to use o

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 17:17

    Sometimes you need an instance of class A, but you do not store A in the fields of the class.
    You just need A instance to perform a one-shot operation. Or, you use A instance to obtain an instance of B, and you are storing B in the field.

    In those cases, a setter (or constructor) autowire will suit you better.
    You will not have unused class-level fields.

    Concrete example:
    You need to construct RabbitTemplate (an object that sends messages to RabbitMQ) To construct it, you need ConnectionFactory
    http://docs.spring.io/spring-amqp/docs/latest_ga/api/org/springframework/amqp/rabbit/core/RabbitTemplate.html#RabbitTemplate-org.springframework.amqp.rabbit.connection.ConnectionFactory-

    You do not need to store that ConnectionFactory. In that case, code that looks like this:

    Class MyClass {
    private RabbitTemplate template;
    
    @Autowired 
    void setConnectionFactory(ConnectionFactory c) {
        template=new RabbitTemplate(c);
    }
    }
    

    ...will serve you better than directly autowiring the ConnectionFactory field.

    In this example, autowiring at the constructor level would be even better, because your object will always be completely constructed. It will be clear that ConnectionFactory is a mandatory dependency, not an optional one.

提交回复
热议问题