The difference between () and ()

Spring allows you to do two things:
  1. Autowiring of beans
  2. Autodiscovery of beans
1. Autowiring
Usually in applicationContext.xml you define beans and other beans are wired using constructor or setter methods. You can wire beans using XML or annotations. In case you use annotations, you need to activate annotations and you have to add <context:annotation-config /> inapplicationContext.xml. This will simplify the structure of the tag from applicationContext.xml, because you will not have to manually wire beans (constructor or setter). You can use @Autowire annotation and the beans will be wired by type.
A step forward for escaping the manual XML configuration is
2. Autodiscovery
Autodiscovery is simplifying the XML one step further, in the sense that you don't even need too add the <bean> tag in applicationContext.xml. You just mark the specific beans with one of the following annotation and Spring will automatically wire the marked beans and their dependencies into the Spring container. The annotations are as follow: @Controller@Service@Component@Repository. By using <context:component-scan> and pointing the base package, Spring will auto-discover and wire the components into Spring container.

As a conclusion:

  • <context:annotation-config /> is used in order to be able to use @Autowired annotation
  • <context:component-scan /> is used to determine the search of specific beans and attempt of autowiring.



<context:annotation-config /> :
Enables you to use annotations that are restricted to wiring up properties and constructors only of beans!.
<context:component-scan/>:
Enables everything that <context:annotation-config /> can do, with addition of using stereotypes  @Component@Service , @Repository. So you can wire entire beans and not just restricted to constructors or properties!.

<context:annotation-config/> <!-- activates the annotations --> 
<context:component-scan base-package="" /> <!-- activates the annotations + register the beans by looking inside the base-package -->

Commentaires

Posts les plus consultés de ce blog

-Xlint:deprecation | deprecated API | use unchecked or unsafe operations

Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: MinuteOfHour

Jasper Raport bug on mac