Wednesday, March 26, 2014

Spring in Action Third Edition

Spring in Action Third Edition
1. Source code
 www.manning.com/SpringinActionThirdEdition
2.
The act of creating associations between application components is commonly referred to as wiring.
In a Spring application, an application contextloads bean definitions and wires them
together. The Spring application context is fully responsible for the creation of and
wiring of the objects that make up the application.
In a Spring application, an application contextloads bean definitions and wires them
together. The Spring application context is fully responsible for the creation of and
wiring of the objects that make up the application.
ClassPathXmlApplicationContext.
Applying aspects
3.
Spring rich client
4.
By default, all Spring beans are singletons. When the container dispenses a bean
(either through wiring or as the result of a call to the container’s getBean()method)
it’ll always hand out the exact same instance of the bean.
<bean id="poeticDuke"
class="com.springinaction.springidol.PoeticJuggler">
<constructor-arg value="15" />
<constructor-arg ref="sonnet29" />
</bean>
<bean id="theStage"
class="com.springinaction.springidol.Stage"
factory-method="getInstance" />
<bean id="kenny"
class="com.springinaction.springidol.Instrumentalist">
<property name="song" value="Jingle Bells" />
<property name="age" value="37" />
<property name="instrument" ref="saxophone" />
</bean>
inner bean:'
<bean id="kenny"
class="com.springinaction.springidol.Instrumentalist">
<property name="song" value="Jingle Bells" />
<property name="instrument">
<bean class="org.springinaction.springidol.Saxophone" />
</property>
</bean>
To set a property to null, you simply use the <null/>element.
To force Spring to produce a new bean instance each time one is needed,
you should declare the bean’s scopeattribute to be prototype.
<bean id="ticket"
class="com.springinaction.springidol.Ticket" scope="prototype" />
The astute reader will recognize that Spring’s notion of singletons is limited to the
scope of the Spring context. Unlike true singletons, which guarantee only a single
instance of a class per classloader, Spring’s singleton beans only guarantee a single
instance of the bean definition per the application context
5.
To define setup and teardown for a bean, simply declare the <bean>with initmethodand/or destroy-methodparameters.
If many of the beans in a context definitionfile will have initialization or destroy
methods with the same name, you don’t have to declare init-methodor destroymethodon each individual bean. Instead you can take advantage of the
defaultinit-methodand default-destroy-methodattributes on the <beans>element.
6. wiring collection
<bean id="hank"
 class="com.springinaction.springidol.OneManBand"> <property name="instruments">
<list>
<ref bean="guitar" />
<ref bean="cymbal" />
<ref bean="harmonica" />
</list>
</property>
</bean>
//map
<bean id="hank" class="com.springinaction.springidol.OneManBand">
<property name="instruments">
<map>
<entry key="GUITAR" value-ref="guitar" />
<entry key="CYMBAL" value-ref="cymbal" />
<entry key="HARMONICA" value-ref="harmonica" />
</map>
</property>
</bean>
7. SpEL
<property name="song" value="#{songSelector.selectSong()?.toUpperCase()}"/>
<property name="multiplier" value="#{T(java.lang.Math).PI}"/>
<property name="validEmail" value=
"#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}"/>
8.
Autowiring helps reduce or even eliminate the need for <property>and
<constructor-arg>elements by letting Spring automatically figure out how
to wire bean dependencies.
 Autodiscovery takes autowiring a step further by letting Spring figure out
which classes should be configured as Spring beans, reducing the need for
the <bean>element.
When a bean has been configured to autowire by autodetect, Spring will attempt to
autowire by constructorfirst. If a suitable constructor-to-bean match can’t be found,
then Spring will attempt to autowire by type.
9.
Annotation wiring isn’t turned on in the Spring container by default. <context:annotation-config>tells Spring that you intend to use annotation-based
wiring in Spring
10.
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.
<context:component-scan
base-package="com.springinaction.springidol">
</context:component-scan>


Todo: Aspect-oriented Spring

No comments:

Post a Comment