Tuesday, May 26, 2015

SpringInAction_note

1. The act of creating associations between appli-
cation components is commonly referred to as wiring.
2. There’s no single Spring container. Spring comes with several container implementa-
tions that can be categorized into two distinct types. Bean factories (defined by the
org.springframework.beans.factory.BeanFactory interface) are the simplest of
containers, providing basic support for DI. Application contexts (defined by the
org.springframework.context.ApplicationContext interface) build on the notion
of a bean factory by providing application framework services, such as the ability to
resolve textual messages from a properties file and the ability to publish application
events to interested event listeners.
3. By default, all Spring beans are singletons.
To force Spring to produce a new bean instance each time one is needed,
you should declare the bean’s scope attribute to be prototype.
<bean id="ticket"
class="com.springinaction.springidol.Ticket"scope="prototype"/>
To set a property to null, you simply use the <null/> element.
4. Spring provides four flavors of autowiring:
 byName—Attempts to match all properties of the autowired bean with beans
that have the same name (or ID) as the properties. Properties for which there’s
no matching bean will remain unwired.
 byType—Attempts to match all properties of the autowired bean with beans
whose types are assignable to the properties. Properties for which there’s no
matching bean will remain unwired.
 constructor—Tries to match up a constructor of the autowired bean with
beans whose types are assignable to the constructor arguments.
 autodetect—Attempts to apply constructor autowiring first. If that fails,
byType will be tried.
Autowiring using byType works in a similar way to byName, except that instead of con-
sidering a property’s name, the property’s type is examined.
5. Since Spring 2.5, one of the most interesting ways of wiring beans in Spring has been
to use annotations to automatically wire bean properties.
Annotation wiring isn’t turned on in the Spring container by default. So, before we
can use annotation-based autowiring, we’ll need to enable it in our Spring configura-
tion. The simplest way to do that is with the <context:annotation-config>
6. Spring 3 supports a few different annotations for autowiring:
 Spring’s own @Autowired annotation
 The @Inject annotation from JSR-330
 The @Resource annotation from JSR-250
7.
@Autowired(required=false)
private Instrumentinstrument;
Here, Spring will try to wire the instrument property. But if no bean of type
Instrument can be found, then no problem. The property will be left null.
The centerpiece of JSR-330 is the @Inject annotation. This annotation is an almost
complete drop-in replacement for Spring’s @Autowired annotation. Unlike @Autowired, @Inject doesn’t have a required attribute.
8.
The <context:component-scan> element
does everything that <context:annotation-config> does, plus it configures
Spring to automatically discover beans and declare them for you. What this means is
that most (or all) of the beans in your Spring application can be declared and wired
without using <bean>.
By default, <context:component-scan> looks for classes that are annotated with one
of a handful of special stereotype annotations:
 @Component—A general-purpose stereotype annotation indicating that the class
is a Spring component
 @Controller—Indicates that the class defines a Spring MVC controller
 @Repository—Indicates that the class defines a data repository
 @Service—Indicates that the class defines a service
 Any custom annotation that is itself annotated with @Component

No comments:

Post a Comment