Skip to content

annotation dictionary

Eric F. Alsheimer edited this page Jan 16, 2018 · 14 revisions

Annotation Dictionary

@Controller

@Controller tells the DispatcherServlet that this class will respond to some requests and to register it with the DispatcherServlet.

@RequestMapping

@RequestMapping used on a method converts the method to an action. It notifies the controller that this action should be called in response to a specific URL being requested.

@ComponentScan

@ComponentScan (or the web.xml tag <context:component-scan>) will scan a package for appropriate annotations (like @Controller) and wire up the beans automatically based on the scan.

<context:annotation-config> activates the annotations of the beans, which is already registered in the application context. It doesn't mind or care how the beans are originally registered, whether by component-scan or bean definition file description. <context:component-scan> will do what <context:annotation-config> does and will register the bean with the container.

Better illustration:
1.  If you define beans A, B, and C (C requires the injection of A and B) in the XML, and wire them up in the XML, you'll have a working application with a full XML bean definition file.  
2.  If you add annotations to your file and remove the A and B injection refs in the XML (using @Autowired) and recompile, your application will not work because it will not inject A or B beans into C.  You may have annotations, but they are just metadata and mean nothing to the compiler as it stands.
3.  If you add <context:annotation-config> to the bean definition file, now it finds the beans because of their annotations and their definition in the bean definition file and wires everything up.  Your combination of bean definitions and annotations works.
4.  If you want to remove more bean definitions from the file, so you mark A, B, and C as annotated components with @Component and recompile, your application just stalls because no beans are created.
5.  If you replace <context:annotation-config> with <context:component-scan>, your @Component annotations identify beans, register them with the container, and wire them correctly.  Your application works!

@AutoWiring

Autowiring is when Spring tries to satisfy a bean's dependencies by finding other beans in the application that fit the original bean's requirements. @Autowired is declared on the dependencies.

@Controller
public class WelcomeController {
  @Autowired
  private GenericWelcomeService welcomeService;
}

Autowiring is not necessary anymore if a bean implements a single constructor. It is implied that it would use Autowiring to populate its dependencies. If there are more than one constructor, you can use @Autowire to declare your argument-accepting constructors, but they need to have it if you are planning to use annotations to populate the instance.

When you use Autowired you no longer need to declare getters and setters.

Configuration-based Annotations

Clone this wiki locally