Monday, September 8, 2014

Pro Spring 3 note

Pro Spring 3
1. Using XML file can externalize all configuration from Java code, while annotations allow the
developer to define and view the DI setup from within the code. Spring also supports a mix of the two
approaches in a single ApplicationContext (the XML file configuration will override the annotation
ones).
One common approach nowadays is to define the application infrastructure (e.g., data source,
transaction manager, JMS connection factory, JMX, etc.) in XML file, while defining the DI configuration
(injectable beans and beans’ dependencies) in annotations.
2.
<context:annotation-config/>
<context:component-scan base-package="com.apress.prospring3.ch4.annotation" />
The <context:annotation-config> tag tells Spring to scan the codebase for dependency
requirements, while the <context:component-scan> tag tells Spring to scan the code for injectable beans
under the package (and all its subpackages) specified. In the <context:component-scan> tag, multiple
packages can be defined by using either a comma, a semicolon, or a space as the delimiter.
3.
you use Spring’s @Service annotation to specify that the bean
provides services that other beans may require, passing in the bean name as the parameter. When
bootstrapping Spring’s ApplicationContext with the XML configuration in Listing 4-15, Spring will seek
out those components and instantiate the beans with the specified names.
4.
Instead of the @Autowired, you can also use the @Resource(name="messageProvider") to achieve the
same result. The @Resource is one of the annotations in the JSR-250 standard that defines a common set of Java
annotations for use on both JSE and JEE platforms. Different from @Autowired, the @Resource annotation
supports the name parameter for more fine-grained DI requirements.
5.
Basically, using
@Component has the same effect as @Service. Both annotations were instructing Spring that the annotated
class is a candidate for autodetection using annotation-based configuration and classpath scanning.
However, since the InjectSimpleConfig class is storing the application configuration, rather than
providing a business service, using @Component makes more sense. Practically, @Service is a
specialization of @Component, which indicates that the annotated class is providing a business service to
other layers within the application.
6.
You may wonder why the annotation @Resource was used instead of @Autowired. It’s because the
@Autowired annotation is semantically defined in a way that it always treats arrays, collections, and maps as
sets of corresponding beans, with the target bean type derived from the declared collection value type. So, for
example, if a class has an attribute of type List<Oracle> and has the @Autowired annotation defined, Spring will
try to inject all beans of type Oracle within the current ApplicationContext into that attribute (instead of the
<util:list> declared in the configuration file), which will result in either the unexpected dependencies being
injected or Spring throwing an exception if no bean of type Oracle was defined. So, for collection type injection,
we have to explicitly instruct Spring to perform injection by specifying the bean name, which the @Resource
annotation supports.
7.
The following are the bean scopes that are supported as of version 3.1:
• Singleton: The default singleton scope.
• Prototype: A new instance will be created by Spring when requested by application.
• Request: For web application use. When using Spring MVC for web application,
beans with request scope will be instantiated for every HTTP request and then
destroyed when the request is completed.
• Session: For web application use. When using Spring MVC for web applications,
beans with session scope will be instantiated for every HTTP session and then
destroyed when the session is over.
8.
@PostConstruct/@PreDestroy. Starting from Spring 2.5, Spring also supports JSR-250 annotations to specify the
method that Spring should call if the corresponding annotation relating to the bean’s life cycle exists in
the class.
The easiest way to take advantage of this mechanism is to use the
AbstractApplicationContext’s registerShutdownHook() method. The method automatically instructs
Spring to register a shutdown hook of the underlying JVM runtime.

No comments:

Post a Comment