How it works...

WebApplicationContext (ch02-servlet.xml) and ApplicationContext (beans-context.xml) have the same XML definition components. They exhibit the same principle of bean injection and also implement the same bean initialization and loading. The only difference is that the root context is needed by DispatcherServlet during its creation while the child contexts are optionally created once additional application layers are added to the MVC application.

Loaded beans in ApplicationContext containers are fetched by MVC components such as controllers and services using the @Autowired, @Inject, or @Resource annotations. Among the three, @Autowired is the only Spring-based annotation used to fetch and inject the bean objects of our container to any Spring component. The injected variable type and name must be coherent with the loaded bean id and class type. If we declare the bean as <bean id="empRec1" class="org.packt.starter.ioc.model.Employee" />, we inject this bean to a controller or service as:

@Autowired 
private Employee empRec1; 

And if there is a need to change the variable name against the bean id, we use the @Qualifier annotation to fix the ambiguities:

@Autowired 
@Qualifier(value="empRec1") 
private Employee employee; 

The @Qualifer(value="") is a Spring-proprietary annotation used to resolve conflicts and ambiguities on variable and bean naming syntax. What is unique about @Autowired is its non-strictness side when it comes to injection. Once @Autowired(required=false) is set, there will be no org.springframework.beans.factory.NoSuchBeanDefinitionException when ambiguities happen. Rather, it will just map the injected variable to the null value.

On the other hand, the non-Spring annotations @Inject and @Resource can also be used to resolve dependency injection. If the developer wants a strict approach in injecting beans to a component, @Inject is the best choice since it will throw an org.springframework.beans.factory.NoSuchBeanDefinitionException when the beans do not exist in the container. To resolve the variable name and bean id ambiguities, it is paired with another JSR-330 annotation, @Named(value=""), which works similar to @Qualifier.

On the other hand, @Resource is the oldest annotation among the three and is part of JSR-250, which is part of the standard annotations for Java and JEE. It has its own way of searching and fetching the object from the container. Using @Resource, the search starts by checking the injected variable name against all bean ids, followed by their types and their @Qualifier if and only if the search by name failed. Both @Autowired and @Inject search all beans by first checking the injected variable type against the bean types, @Qualifier against the bean id, and finally, by its name with the bean id.

In addition to auto-wiring, Spring uses its @Component annotation to create Spring-managed beans without the need for XML for registering. We can inject these beans to any component using the preceding three annotations.

One of the most important parts of this recipe is the introduction of the model layer, which transports our injected beans from the @Controller to their respective views. There are three known Spring APIs that are used widely on this layer and these are ModelAndView, ModelMap, and Model. The recipe only highlighted the current classes, ModelMap and Model. The only difference between the two is the number of helper methods they contain. Model is an interface with only four addAttribute(...) methods and a mergeAttribute()while ModelMap is an implementation of ModelMap with some additional Map-related methods. ModelAndView is been in the Spring framework for a long time and is used to contain both a ModelMap and its view object. When to use any of these three depends on the requirement of the response. If the response needs to perform redirection using the RedirectView API, the ModelAndView is appropriate to use.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.144.65