Spring

1,453 views
1,397 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,453
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
Downloads
32
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • 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

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

    ×