Outline: Topics To Be Covered
Outline: Topics To Be Covered
Group of
four
This calls for loose coupling. Coding to an interface, using
spring and accessing database through DOA helps in
achiving this goal
INJECTING VALUES
Injecting through constructors
<bean id="movieBean3"
class="com.contata.springtutorials.inventory.Movie">
<constructor-arg value="Movie: The Insider"/>
<constructor-arg value="Russel Crove Film"/>
<constructor-arg value="300"/>
<constructor-arg ref="bookBean1"/>
</bean>
The <constructor-arg> element is used to give Spring
additional information to
use when constructing a bean. If no <constructor-arg>s are
given the default constructor is used.
INJECTING COLLECTIONS
XML ELEMENTS:
<list> Wiring a list of values, allowing duplicates.
<set> Wiring a set of values, ensuring no duplicates
<map> Wiring a collection of name-value pairs where name
and value can be of any type
<props> Wiring a collection of name-value pairs where the
name and value are both Strings
EXPLANATIONS:
The <list> and <set> elements are useful when configuring
properties that are either arrays or some implementation of
java.util.Collection.
As for <map> and <props>, these two elements correspond
to collections that
are java.util.Map and java.util.Properties, respectively.
The key difference between the two is that when using
<props>, both the keys and values are Strings, while <map>
allows keys and values of any type.
Company confidential – please do not share © 2007
Contata Solutions
INJECTING MAP
<beanid="hank"class="com.springinaction.springidol.OneMa
nBand">
<property name="instruments">
<map>
<entry key="GUITAR" value-ref="guitar" />
<entry key="CYMBAL" value-ref="cymbal" />
</map></property></bean>
key Specifies the key of the map entry is not a spring bean
value the value of the map entry is not a spring bean
key-ref Specifies the key of the map entry as a reference to a
bean in the Spring context
value-ref Specifies the value of the map entry as a reference
to a bean in the Spring context
INJECTING java.util.Properties
<bean id="hank"
class="com.springinaction.springidol.OneManBand">
<property name="instruments">
<props>
<prop key="GUITAR">STRUM STRUM STRUM</prop>
<prop key="CYMBAL">CRASH CRASH CRASH</prop>
</props>
</property>
</bean>
Collections contd...
<property name="instruments">
<value>
GUITAR=STRUM STRUM STRUM
CYMBAL=CRASH CRASH CRASH
</value>
</property>
AUTOWIRING
This mechanism helps to inject spring beans into other beans automatically.
It is of four types
BEAN SCOPING
By default, all Spring beans are singletons. That is, when the
container dispenses a bean (either through wiring or as the
result of a call to the container’s getBean() method) it will
always hand out the exact same instance of the bean.
<bean id="demo"
class="com.springinaction.springidol.ToyClass
scope="prototype" />
Bean Naming
There are three ways of declaring a bean
Bean Naming
String s1 = (String)factory.getBean("name1");
String s2 = (String)factory.getBean("name2");
String s3 = (String)factory.getBean("name3");
String s4 = (String)factory.getBean("name4");
String s5 = (String)factory.getBean("namex1");
String s6 = (String)factory.getBean("namex2");
Resolving Dependencies :
Bean Inheritance
<bean id="kenny"
class="com.springinaction.springidol.Instrumentalist">
<property name="song" value="Jingle Bells" />
<property name="instrument" ref="saxophone" />
</bean>
<bean id="david"
class="com.springinaction.springidol.Instrumentalist">
<property name="song" value="Jingle Bells" />
<property name="instrument" ref="saxophone" />
</bean>
<bean id="baseSaxophonist"
class="com.springinaction.springidol.Instrumentalist"
abstract="true">
<property name="instrument" ref="saxophone" />
<property name="song" value="Jingle Bells" />
</bean>
Using FactoryBean
AOP
Whereas DI helps you decouple your application objects
from each other,
AOP helps you decouple cross-cutting concerns from the
objects that they affect.
Types Of Advice :
Types Of Advice
• Before advice:
• INTERFACE --- MethodBeforeAdvice
• METHOD –- public void before(Method method, Object[] args, Object
target)throws Throwable;
• Around advice:
INTERFACE --- MethodInterceptor
METHOD –-public Object invoke(MethodInvocation invocation)
throws Throwable
<bean id="audienceAdvisor"
class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="audienceAdvice" />
<property name="pointcut" ref="performancePointcut" />
</bean>
<bean id="audienceAdvisor"
class="org.springframework.aop.support.
➥RegexpMethodPointcutAdvisor">
<property name="advice" ref="audienceAdvice" />
<property name="pattern" value=".*perform" />
</bean>
<bean id="performancePointcut"
class="org.springframework.aop.aspectj.
AspectJExpressionPointcut">
<property name="expression" value=”execution(* *.perform(..))" > </bean>
<bean id="audienceAdvisor"
class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="audienceAdvice" />
<property name="pointcut" ref="performancePointcut" />
</bean>
<bean id="audienceAdvisor"
class="org.springframework.aop.aspectj.
➥ AspectJExpressionPointcutAdvisor">
<property name="advice" ref="audienceAdvice" />
<property name="expression" value="execution(* *.perform(..))" />
</bean>
<property name="interceptorNames">
<list>
<value>audienceAdvisor</value>
</list>
</property>
<bean id="audienceProxyBase"
class="org.springframework.aop.framework.ProxyFactoryBean"
abstract="true">
<property name="proxyInterfaces"
value="com.springinaction.springidol.Performer" />
<property name="interceptorNames" value="audienceAdvisor" />
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.Defau
ltAdvisorAutoProxyCreator" />
END OF AOP
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.Hiber
nateTemplate">
<property name="sessionFactory"
ref="sessionFactory" />
</bean>
<bean id="mailMessage"
class="org.springframework.mail.SimpleMailMessage">
<property name="from">
<value><![CDATA[Sohil Dev Singh <sohils@contata.co.in>]]></value>
</property>
<property name="subject" value="Mail sent through Spring" />
<property name="text">
<value>
This mail was sent to you using spring framework.
</value>
</property>
</bean>
Thank you
Company confidential – please do not share © 2007