0% found this document useful (0 votes)
111 views

Outline: Topics To Be Covered

Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

Outline: Topics To Be Covered

Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

Contata Solutions

Outline : Topics To Be Covered

 Spring Dependency interjection (IOC)


 Spring AOP
 Spring integration with hibernate.
 Mail Sending Through Spring

Company confidential – please do not share © 2007


Contata Solutions

VERY IMP0RTANT DESIGN PRINCIPLE

CODE SHOULD BE CLOSED FOR


MODIFICATION BUT IT SHOULD
BE OPEN FOR EXTENSION

Group of
four
 This calls for loose coupling. Coding to an interface, using
spring and accessing database through DOA helps in
achiving this goal

Company confidential – please do not share © 2007


Contata Solutions

What And Why Of Spring


 Spring is alightweight dependency injection and aspect-
oriented container and framework
 .Dependency Injection—Spring promotes loose coupling through a technique known as
dependency injection (DI). DI helps you decouple your
application objects from each other,
 Aspect-oriented—Spring comes with rich support for
aspect-oriented programming (AOP) which helps you
decouple cross-cutting concerns from the objects that they
affect.
 Container—Spring is a container in the sense that it
contains and manages the lifecycle and configuration of
application objects.
 Framework—Spring makes it possible to configure and
compose complex applications from simpler components.
In Spring, application objects are composed declaratively,
typically in an XML file. Spring also provides much
infrastructure functionality

Company confidential – please do not share © 2007


Contata Solutions

Bean Creation Using Spring


 It involves java code and XML.
 XML Part:
 <bean id="bookBean1"
 class="com.contata.springtutorials.inventory.Book">
 The <bean> element is the most basic configuration unit in Spring. It tells Spring to create an object
for us. Here we’ve declared Duke as a Spring-managed bean using what is nearly the simplest <bean>
declaration possible.

 The id attribute gives the bean a name by which it will be


referred to in the Spring container. This
 bean will be known as bookBean1.

 The class attribute tells Spring what type


 the bean will be. Here bean will be of class
com.contata.springtutorials.inventory.Book

Company confidential – please do not share © 2007


Contata Solutions

Bean Creation Using Spring


 JAVA PART
 private static final ApplicationContext apc = new
ClassPathXmlApplicationContext("beans.xml");
 private ShoppingPortal cart = (ShoppingPortal )
apc.getBean("bookBean1");

 ApplicationContext This is an abstract class.. Among the


many implementations of ApplicationContext are three that
arecommonly used:
 ■ClassPathXmlApplicationContext—Loads bean
definations from an XML file located in the classpath.
 ■ FileSystemXmlApplicationContext—Loads a bean
definations from anXML file in the file system.
 ■ XmlWebApplicationContext—Loads context definitions
from an XML file contained within a web application.
 getBean() This Method loads the bean of specific id.
Company confidential – please do not share © 2007
Contata Solutions

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.

 USE TYPE AND INDEX ATTRIBUTES IF THERE ARE OVER


LOADED CONSTRUCTORS

Company confidential – please do not share © 2007


Contata Solutions

Injecting into bean properties

 Bean properties can be configured in Spring using the <property> element.


 <property> injects by calling a property’s setter method.
 <bean id="movieBean2"
 class="com.contata.springtutorials.inventory.Movie">
 <property name="itemName" value="Movie: Vivaah" />
 <property name="itemDescription" value=" A Sahid Kapoor Film" />
 <property name="itemPrice" value=" 300" />
 <property name="book" ref="bookBean1"/>
 </bean>
 value attribute injects simple values that are not spring beans
 ref attribute injects other spring beans

Company confidential – please do not share © 2007


Contata Solutions

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 LIST/ SET/ARRAYS


 <bean id="itemInventoryBean"
 class="com.contata.springtutorials.store.ItemInventory">
 <property name="quantityItem" >
 <list>
 <value type="java.lang.Integer">10</value>
 <value type="java.lang.Integer">20</value>
 <value type="java.lang.Integer">30</value>
 </list>
 </property>
 </bean>
 Class ItemInventory{
 private List<Integer> quantityItem ;
 public List<Integer> getQuantityItem() {
 return quantityItem;}
 }

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

Company confidential – please do not share © 2007


Contata Solutions

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>

public class OneManBand {



 private Properties instruments;
 public void setInstruments(Properties instruments) {
 this.instruments = instruments;}}

Company confidential – please do not share © 2007


Contata Solutions

Collections contd...
<property name="instruments">
<value>
GUITAR=STRUM STRUM STRUM
CYMBAL=CRASH CRASH CRASH
</value>
</property>

Company confidential – please do not share © 2007


Contata Solutions

AUTOWIRING
  This mechanism helps to inject spring beans into other beans automatically.
 It is of four types

 byName—Attempts to find a bean in the container whose


name (or ID) is the same as the name of the property being
wired. If a matching bean is not found, the property will
remain unwired.
 ■ byType—Attempts to find a single bean in the container
whose type matches the type of the property being wired. If
no matching bean is found, the property will not be
wired.Can throw EXception
 ■ constructor—Tries to match up one or more beans in the
container with the parameters of one of the constructors of
the bean being wired. Can throw EXception
 ■ autodetect—Attempts to autowire by constructor first and
then usingbyType. Ambiguity is handled the same way as
with constructor and byType wiring.

Company confidential – please do not share © 2007


Contata Solutions

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" />

Company confidential – please do not share © 2007


Contata Solutions

Bean Naming
 There are three ways of declaring a bean

 <bean id="string1" class="java.lang.String"/>


 <bean name="string2" class="java.lang.String"/>
 <bean class="java.lang.String"/>

3rd is least recommended


If no id then first name is default unique identifier.
Else full class name is unique identifier

 <bean id="name1" name="name2,name3,name4" class="java.lang.String"/>

 <bean id="name1" name="name2,name3,name4" class="java.lang.String"/>


<alias name="name2" alias="namex1"/>
<alias name="name1" alias="namex2"/>

Company confidential – please do not share © 2007


Contata Solutions

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 id="b" class="com.apress.prospring2.ch03.beandependency.B" depends-on="a"/>

Company confidential – please do not share © 2007


Contata Solutions

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>

<bean id="kenny" parent="baseSaxophonist" />


<bean id="david" parent="baseSaxophonist" />

parent—Indicates the id of a <bean> that will be the parent of the <bean>


Company
with the parent attribute. confidential
The parent attribute –isplease do not
to <bean> share
what extends is © 2007
Contata Solutions

Using FactoryBean

 To create and inject dependencies that cannot be created


simply by using the new operator.

 FactoryBean implementations are configured within your


BeanFactory like any normal bean.

 However Spring does not return the FactoryBean;


instead, it returns the result of invocation
FactoryBean.getObject() method

 The FactoryBean interface declares three methods:


getObject(), getObjectType(), isSingleton().

 The isSingleton() property allows you to inform Spring


whether the FactoryBean is managing a singleton instance or
not. Remember that by setting the scope="singleton" of the
FactoryBean’s <bean> tag, you tell Spring about the singleton
status of the FactoryBean itself, not the Objects it is
returning.
Company confidential – please do not share © 2007
Contata Solutions

END OF DEPENDENCY INTERJECTION

 THIS FINISHES DEPENDENCY INTERJECTION OR IOC

 COMING NEXT ASPECT ORIENTED PROGRAMMING

Company confidential – please do not share © 2007


Contata Solutions

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.

 Cross-cutting concern: can be described as any functionality


that affects multiple points of an application.
 With AOP, you still define the common functionality in
 one place, but you can declaratively define how and where
this functionality is applied without having to modify the
class to which you are applying the new feature.

Company confidential – please do not share © 2007


Contata Solutions

Types Of Advice :

Company confidential – please do not share © 2007


Contata Solutions

Types Of Advice
• Before advice:
• INTERFACE --- MethodBeforeAdvice
• METHOD –- public void before(Method method, Object[] args, Object
target)throws Throwable;

• After returning advice:


• INTERFACE --- AfterReturningAdvice
METHOD –-public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) )throws Throwable;

• After throwing advice:


INTERFACE --- ThrowsAdvice
METHOD –-public void afterThrowing([method], [args], [target], throwable);
public void afterThrowing(Throwable throwable);

• Around advice:
INTERFACE --- MethodInterceptor
METHOD –-public Object invoke(MethodInvocation invocation)

throws Throwable

Company confidential – please do not share © 2007


Contata Solutions

Pointcut and Advisor


<bean id="performancePointcut"
class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*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.support.
➥RegexpMethodPointcutAdvisor">
<property name="advice" ref="audienceAdvice" />
<property name="pattern" value=".*perform" />
</bean>

Company confidential – please do not share © 2007


Contata Solutions

Pointcut and Advisor using AspectJ expressions

<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>

Company confidential – please do not share © 2007


Contata Solutions

Proxy Factory Bean contd.....


<bean id="duke"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="dukeTarget" />
<property name="interceptorNames" value="audienceAdvisor" />
<property name="proxyInterfaces"
value="com.springinaction.springidol.Performer" />
</bean>

<property name="interceptorNames">
<list>
<value>audienceAdvisor</value>
</list>
</property>

Company confidential – please do not share © 2007


Contata Solutions

....contd Proxy Factory Bean


<bean id="stevie"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="stevieTarget" />
<property name="proxyInterfaces"
value="com.springinaction.springidol.Performer" />
<property name="interceptorNames" value="audienceAdvisor" />
</bean>

<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 id="stevie" parent="audienceProxyBase">


<property name="target" ref="stevieTarget" />
</bean>
<bean id="duke" parent="audienceProxyBase">
<property name="target" ref="dukeTarget" />
</bean>

Company confidential – please do not share © 2007


Contata Solutions

AOP RELATED BEANS


 <bean id="itemInventoryAdvice"
 class="com.contata.springtutorials.inventory.InventoryTrack
Advisor" autowire="byType">
 </bean>
 --Main buisness logic implements one or more of following
interfaces org.springframework.aop package

 MethodBeforeAdvice
 AfterReturningAdvice
 ThrowsAdvice

Company confidential – please do not share © 2007


Contata Solutions

AOP RELATED BEANS


 <bean id="itemInventoryAdvisor"
 class="org.springframework.aop.support.RegexpMethodPoin
tcutAdvisor">
 <property name="advice" ref="itemInventoryAdvice" />
 <property name="pattern" value=".*buyItem" />
 </bean>
 Joins the buisness logic of a cross cutting concern to
methords to which these concerns relate

 <bean
class="org.springframework.aop.framework.autoproxy.Defau
ltAdvisorAutoProxyCreator" />

 Automatically finds the class containing the methods


specified above

Company confidential – please do not share © 2007


Contata Solutions

END OF AOP

 THIS FINISHES AOP

 COMING NEXT HIBERNATE INTEGRATION AND MAIL


SENDING WITH SPRING

Company confidential – please do not share © 2007


Contata Solutions

Hibernate Integration Beans Involved


 <bean id="dataSource"
 class="org.apache.commons.dbcp.BasicDataSource">
 <property name="driverClassName"
value="com.mysql.jdbc.Driver" />
 <property name="url"
 value="jdbc:mysql://localhost:3306/springtutorialdb" />
 <property name="username" value="root" />
 <property name="password" value="root" />
 <property name="initialSize" value="5" />
 <property name="maxActive" value="10" />
 </bean>

 Bean dataSource is used to specify database


schema

Company confidential – please do not share © 2007


Contata Solutions

Hibernate Integration Beans Involved


 <!-- sessionFactory bean sets Session Factory This bean
configures the hibernate environment and it's properties -->
 <bean id="sessionFactory"
 class="org.springframework.orm.hibernate3.LocalSessionFa
ctoryBean">
 <property name="dataSource" ref="dataSource" />
 <property name="mappingResources">
 <list>
 <value>com/contata/springtutorial/hibernate/UserData.hbm.x
ml</value> </list></property>
 <property name="hibernateProperties">
 <props>
 <prop key="hibernate.hbm2ddl.auto">update</prop>
 <prop
key="hibernate.dialect">org.hibernate.dialect.MySQLDialect<
/prop>
 </props></property> </bean>

Company confidential – please do not share © 2007


Contata Solutions

HIBERNATE INTEGRATIONS BEAN INVOLVED


 Bean hibernateTemplate provides a wrapper over
hibernate session
 It manages the transactions

 <bean id="hibernateTemplate"
 class="org.springframework.orm.hibernate3.Hiber
nateTemplate">
 <property name="sessionFactory"
ref="sessionFactory" />
 </bean>

Company confidential – please do not share © 2007


Contata Solutions

HIBERNATE INTEGRATION FINISHED

 THIS FINISHES HIBERNATE INTEGRATION

 COMING NEXT MAIL SENDING WITH SPRING

Company confidential – please do not share © 2007


Contata Solutions

MAIL SENDING BEANS INVOLVED


 <beans>
 <bean id="mailSender"
 class="org.springframework.mail.javamail.JavaMailSenderImpl">
 <property name="host" value="192.168.2.6" />
 </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>

Company confidential – please do not share © 2007


Contata Solutions

SPRING TRAINING ENDS

This finishes the spring training

Thank you
Company confidential – please do not share © 2007

You might also like