Defining a Java class as <bean> with annotation

The solution to this <indexentry content="annotation-based DI:Java class, defining as with annotation">problem is to define <context:component-scan> in an application context (XML) file. When Spring reads this element, it will start scanning the beans from the Java package defined by its attribute, base-package. You can instruct Spring to treat the Java class as <bean> by declaring class-level annotation @Component. In short, defining annotation-based configuration is a two-step process, as follows:

  1. Scanning the package: This can be done by reading the base-package attribute of <context:component-scan>. Spring will start scanning for the classes in that Java package.
  2. Defining beans: Out of ...

Get Java 9 Dependency Injection now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.