Sobes.tech
Junior — Middle

Is it necessary to use the Autowired annotation when configuring components via XML files in Spring?

sobes.tech AI

Answer from AI

No, the @Autowired annotation is not mandatory when configuring components through XML files in Spring. In XML configuration, dependencies are usually specified explicitly using <property> or <constructor-arg> tags, which allows Spring to inject dependencies without using annotations.

The @Autowired annotation is more commonly used in annotation-based configuration and Java configuration to automatically wire beans by type.

Example of XML configuration:

<bean id="myService" class="com.example.MyService">
    <property name="repository" ref="myRepository" />
</bean>

Here, the repository dependency is injected without using @Autowired.

Thus, when using XML, the @Autowired annotation is not needed.

Is it necessary to use the Autowired annotation when… - sobes.tech