• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Spring
 

Spring

on

  • 1,562 views

 

Statistics

Views

Total Views
1,562
Views on SlideShare
1,553
Embed Views
9

Actions

Likes
1
Downloads
31
Comments
0

2 Embeds 9

http://www.techgig.com 6
http://www.linkedin.com 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • All @Configuration classes are subclassed at startup-time with CGLIB. In the subclass, the child method checks the container first for any cached (scoped) beans before it calls the parent method and creates a new instance.There are a few restrictions due to the fact that CGLIB dynamically adds features at startup-time:Configuration classes should not be finalThey should have a constructor with no arguments
  • Code is cleaner with the DI principle and decoupling is more effective when objects are provided with their dependencies
  • Keep in mind that to make “Constructor argument name” work out of the box your code must be compiled with the debug flag enabled so that Spring can look up the parameter name from the constructor. If you can't compile your code with debug flag (or don't want to) you can use @ConstructorProperties JDK annotation to explicitly name your constructor arguments.
  • In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional.

Spring Spring Presentation Transcript

  • mustafa daşgın
    mdasgin.blogspot.com
    mustafa.dasgin@prime.com.tr
    Spring
  • Expert One-on-One J2EE Design and Development
    (Rod Johnson)
    Sıradan Java sınıflarına yetenekler kazandırır.
    Transactions, Monitoring, Concurrency, Remoting
    Bağımlılık aktarımını üstlenir
    Modüler yapılı
    Core
    Veri erişimi/entegrasyonu
    Web
    AOP
    Test
    Spring’e Giriş
  • Spring Modülleri
  • Konfigürasyona göre “bean” yaratıp yönetimini üstlenir
    XML tabanlı
    Notlandırma (Annotation) tabanlı
    Java tabanlı
    org.springframework.context.ApplicationContext 
    ClassPathXmlApplicationContext
    FileSystemXmlApplicationContext
    XmlWebApplicationContext
    AnnotationConfigApplicationContext
    AnnotationConfigWebApplicationContext
    IoC Container
  • IoC Container (Devam)
  • XML Tabanlı Konfigürasyon
    <beans xmlns=“...>
    <bean id=“userService" class=“com.prime.UserServiceImpl"> <property name=“userDao" ref=“userDao"/>
    <!– diger ek tanımlamalar -->
    </bean>
    <bean id=“userDao" class=“com.prime.UserDaoImpl">
    <!– diger ek tanımlamalar -->
    </bean>
    </beans
  • Notlandırma Tabanlı Konfig.
    <beans xmlns=“...>
    <context:component-scan base-package="org.example"/>
    </beans>
    @Service //@Component
    @Scope("prototype")
    publicclassSimpleMovieLister {
    @Autowired//@Resource
    privateMovieFindermovieFinder;
    publicSimpleMovieLister(MovieFindermovieFinder) { this.movieFinder= movieFinder;
    }
    }
    @Repository //@Component
    publicclassJpaMovieFinderimplementsMovieFinder {
    // implementation elided for clarity
    }
  • AnnotationConfigWebApplicationContext
    CGLIB jar gerekli
    Java Tabanlı Konfigürasyon
    @Configuration
    publicclassAppConfig{
    @Bean(name = "myFoo",initMethod= "init")
    @Scope("prototype")
    publicFoofoo() {
    returnnewFoo(bar());
    }
    @Bean
    publicBar bar() { return
    newBar();
    }
    }
  • IoC Container tarafından yaratılıp, bağımlılıkları ve yaşam döngüsü yönetilen basit Java nesneleri (POJO)
    Tanımlanırken;
    Sınıfı
    id ve/veya ismi
    Scope değeri
    Bağımlılıkları
    Yaşam döngüsü metotları belirtilebilir.
    Container 3 farklı yöntemle beanleri yaratır:
    Sınıfın yapıcısı (constructor)
    static factory metot
    instance factory metot
    Bean Nedir?
  • Yapıcı ile
    Static Factory metot ile
    Bean Yaratma Yöntemleri
    <bean id="exampleBean" class="examples.ExampleBean"/>
    <bean id="clientService" class="examples.ClientFactoryService“
    factory-method="createClient"/>
    publicclassClientFactoryService {
    publicstaticClientServicecreateClient() {
    new ClientService();
    }
    }
  • Instance Factory metot ile
    Bean Yaratma Yöntemleri (Devam)
    <bean id="serviceLocator" class="examples.DefaultServiceLocator“/>
    <bean id="clientService“
    factory-bean="serviceLocator“
    factory-method="createClientServiceInstance"/>
    publicclassDefaultServiceLocator {
    publicClientServicecreateClientServiceInstance() { returnnew ClientService();
    }
    }
  • Bean Scopeları
    <web-app>
    ...
    <listener>
    <listener-class> org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    ...
    </web-app>
  • Singleton Scope
  • Prototype Scope
  • org.springframework.beans.factory.InitializingBean
    org.springframework.beans.factory.DisposableBean
    Bean Yaşam Döngüsü
    <bean id="exampleBean" class="examples.ExampleBean"/>
    publicclassExampleBeanimplementsInitializingBean, DisposableBean{
    publicvoidafterPropertiesSet() {
    // do some initialization work
    }
    publicvoid destroy() {
    // do some destruction work
    }
    }
  • init-method
    destroy-method
    Bean Yaşam Döngüsü (Devam)
    <bean id="exampleBean" class="examples.ExampleBean" init-method="init“ destroy-method=“cleanup”/>
    publicclassExampleBean {
    publicvoid init() {
    // do some initialization work
    }
    publicvoid cleanup() {
    // do some destruction work
    }
    }
  • @PostConstruct
    @PreDestroy
    Bean Yaşam Döngüsü (Devam)
    publicclassExampleBean {
    @PostConstruct
    publicvoidinit() {
    //...
    }
    @PreDestroy
    publicvoidcleanup() {
    //...
    }
    }
  • Yapıcı ile Aktarım
    Bağımlılık Aktarımı
    packagex.y;
    publicclassFoo {
    publicFoo(Bar bar, Bazbaz) {
    // ...
    }
    }
    <beans>
    <bean id="foo" class="x.y.Foo">
    <constructor-arg ref="bar"/>
    <constructor-arg ref="baz"/>
    </bean>
    <bean id="bar" class="x.y.Bar"/>
    <bean id="baz" class="x.y.Baz"/>
    </beans>
  • Yapıcı ile Aktarım (Devam..)
    Bağımlılık Aktarımı (Devam)
    <bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg index="0" value="7500000"/>
    <constructor-arg index="1" value="42"/>
    </bean>
    <bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg name="years" value="7500000"/>
    <constructor-arg name="ultimateanswer" value="42"/> </bean>
  • Setter Metotları ile Aktarım
    Bağımlılık Aktarımı (Devam..)
    <bean id="exampleBean" class="examples.ExampleBean“>
    <property name="beanOne">
    <ref bean="anotherExampleBean"/>
    </property>
    <property name="beanTwo" ref="yetAnotherBean"/>
    <property name="integerProperty" value="1"/>
    </bean>
    <bean id="anotherExampleBean" class="examples.AnotherBean"/> <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
  • Primitive Değerlerin Aktarımı
    packageexamples;
    publicclassExampleBean {
    privateintyears;
    String ultimateAnswer;
    publicExampleBean(int years, String ultimateAnswer) {
    this.years= years;
    this.ultimateAnswer= ultimateAnswer;
    }
    }
    <bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg type="int" value="7500000"/>
    <constructor-arg type="java.lang.String" value="42"/> </bean>
  • Collection Yapıların Aktarımı
    <bean id="moreComplexObject" class="example.ComplexObject">
    <!-- java.util.Properties -->
    <property name="adminEmails">
    <props>
    <prop key="administrator">admin@expl.org</prop>
    <prop key="support">support@expl.org</prop>
    <prop key="dev">development@expl.org</prop> </props>
    </property>
    <!– java.util.List
    <property name="someList">
    <list>
    <value>a list element</value>
    <ref bean="myDataSource" />
    </list>
    </property>
  • Collection Yapılarının Akt. (Devam)
    <!-- java.util.Map
    <property name="someMap">
    <map>
    <entry key="entry" value="just some string"/>
    <entry key ="a ref" value-ref="myDataSource"/>
    </map>
    </property>
    <!-- java.util.Set -->
    <property name="someSet">
    <set>
    <value>just some string</value>
    <ref bean="myDataSource" />
    </set>
    </property>
    </bean>
  • Null ve Boş String Aktarımı
    <!-- Boş String -->
    <bean class="ExampleBean">
    <property name="email" value=""/>
    </bean>
    <!-- Null -->
    <bean class="ExampleBean">
    <property name="email"><null/></property>
    </bean>
  • Modları:
    byName
    byType
    constructor
    Primitive tipler için kullanılamaz
    Aynı tip değerinde birden fazla aday bean varsa hata oluşur.
    Autowire
    <bean id=“userService” class=“services.UserService” autowire=“byName”/>
  • web.xml
    JSF ile Entegrasyon
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>
    ...
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
  • faces-config.xml
    ApplicationContext erişimi:
    JSF ile Entegrasyon (Devam)
    <faces-config>
    <application>
    <el-resolver>
    org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
    ...
    </application>
    </faces-config>
    ApplicationContextctx = FacesContextUtils.getWebApplicationContext(
    FacesContext.getCurrentInstance());
  • Farklı transaction APIleri (JTA, JDBC, Hibernate, JPA) üzerine ortak bir model sunar.
    Transaction gerçekleştirimi değişse bile kodta değişiklik yapmaya gerek yok.
    Programlama veya tanımlama ile transaction tanımları yapılabilir.
    Basit API
    Spring Transaction Desteği
  • JDBC – DataSourceTransactionManager
    JTA – JtaTransactionManager
    Hibernate – HibernateTransactionManager
    JPA - JpaTransactionManager
    PlatformTransactionManager
    <bean id=“txManager"
    class="org.springframework.orm.jpa.JpaTransactionManager“
    <property name=“entityManagerFactory” ref="entityManagerFactory"/>
    </bean>
    <bean id=“entityManagerFactory“
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name=“dataSource” ref=“dataSource”/>
    ...
    </bean>
  • XML ile Transaction Tanımlama
    <aop:config>
    <aop:pointcut id="serviceOperation“
    expression="execution(* x.y.service..*Service.*(..))"/>
    <aop:advisorpointcut-ref="serviceOperation“
    advice-ref="txAdvice"/>
    </aop:config>
    <bean id="fooService" class="x.y.service.DefaultFooService"/>
    <bean id="anotherService" class="org.xyz.SomeService"/>
    <tx:advice id="txAdvice“transaction-manager="txManager" >
    <tx:attributes>
    <tx:method name="get*" read-only="true"/>
    <tx:method name="*"/>
    </tx:attributes>
  • Notlandırma ile Transaction Tanım.
    @Transactional(readOnly = true)
    publicclassDefaultFooServiceimplementsFooService{
    publicFoogetFoo(String fooName) { // do something }
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    publicvoidupdateFoo(Foofoo) { // do something } }
    <bean id="fooService" class="x.y.service.DefaultFooService"/>
    <tx:annotation-driven transaction-manager="txManager"/>
  • LocalEntityManagerFactoryBean
    EntityManagerFactory’i JNDI’dan elde etme
    LocalContainerEntityManagerFactoryBean
    JPA Desteği
  • JPA Desteği (Devam)
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="persistenceUnitName" value="simpleJpa"/>
    <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true"/>
    <property name="generateDdl" value="true"/>
    <property name="database" value="HSQL"/>
    </bean>
    </property>
    </bean>
  • Spring Reference Documantation
    http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
    Kaynak