Is context: annotation-config an alternative to @AutoWired?

Is it right that I can put context:annotation-config in my XML configuration and it will automatically introduce the bean class without any annotations?

Therefore, instead of using these types of annotations:

 public class Mailman { private String name; @Autowired private Parcel Parcel; public Mailman(String name) { this.name = name; } @Autowired public void setParcel(Parcel Parcel) { this.Parcel = Parcel; } @Autowired public void directionsToParcel(Parcel Parcel) { this.Parcel = Parcel; } } 

I just need to write this:

 <beans ... > <bean id="mailMan" class="MailMan"> <constructor-arg value="John Doe"/> </bean> <bean id="parcel" class="Parcel" /> <context:annotation-config /> </beans> 

and then my MailMan class would look much simpler without the need for annotations:

 public class Mailman { private String name; private Parcel Parcel; public Mailman(String name) { this.name = name; } } 
+6
java spring annotations autowired
source share
3 answers

By default, the Spring context does not pay attention to @Autowired annotations. To process them, the context must have an AutowiredAnnotationBeanPostProcessor bean registered in the context.

<context:annotation-config/> registers one of them for you (along with several others), so you need it (unless you register AutowiredAnnotationBeanPostProcessor yourself, which is absolutely true).

If you don't like having @Autowired in your code, you can explicitly enter properties in XML using <property> , which simply moves the mess from one place to another.

If your context is extremely simple, you can use implicit auto-messaging, as described here . Essentially, this tells Spring to automatically auto-delete by property name or type. This required a very small configuration, but got out of hand very quickly - the automatic nature means that it is difficult to control and gives you very little flexibility.

@Autowired really the best option overall.

+6
source share

<context:annotation-config /> just automatically registers all the standard post- AutowiredAnnotationBeanPostProcessor bean processors provided by Spring as PersistenceAnnotationBeanPostProcessor , AutowiredAnnotationBeanPostProcessor , CommonAnnotationBeanPostProcessor and RequiredAnnotationBeanPostProcessor , so you don't need to register them separately in your file. You still need to provide @Autowired attribute / installer annotations as before.

+1
source share

Not.

@Autowired Annotations are required for the class along with <context:annotation-config/>

The xml configuration part is not needed when using annotations.

Also from the document Please note that <context:annotation-config/> only searches for annotations on beans in the same application context in which it is defined. This means that if you enter the WebApplicationContext for the DispatcherServlet, it will only check for @Autowired beans in your controllers, and not in your services.

If you do not need annotations, you need to specify the xml configuration.

0
source share

All Articles