Successfully reported this slideshow.
Your SlideShare is downloading. ×

Spring framework core

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 93 Ad

More Related Content

Slideshows for you (20)

Viewers also liked (20)

Advertisement

Similar to Spring framework core (20)

Recently uploaded (20)

Advertisement

Spring framework core

  1. 1. Spring Framework Core 2st WEEK 30 Jun GEEKY ACADEMY 2013
  2. 2. 2 | GEEK ACADEMY 2013 INSTRUCTOR TEAM SALAHPIYA Salah (Dean) Chalermthai Siam Chamnan Kit SChalermthai@sprint3r.com Piya (Tae) Lumyong - piya.tae@gmail.com
  3. 3. 3 | GEEK ACADEMY 2013 Topic • Introduction to Spring • Basic bean • IoC Container • Bean Lifecycle • Annotation
  4. 4. Introduction to Spring GEEK ACADEMY 2013
  5. 5. 5 | GEEK ACADEMY 2013 Introduction to Spring • What's Spring • Dependency Injection • Module of Spring • Usage scenarios
  6. 6. 6 | GEEK ACADEMY 2013 What's Spring • Goals – make Enterprise Java easier to use – promote good programming practice – enabling a POJO-based programming model that is applicable in a wide range of environments • Some said Spring is just a “glue” for connecting all state of the art technologies together (a.k.a Integration Framework) via it’s Application Context. • Heart and Soul of Spring is Dependency Injection and Aspect Oriented Programming.
  7. 7. 7 | GEEK ACADEMY 2013 What is Spring Framework today? • an open source application framework • a lightweight solution for enterprise applications • non-invasive (POJO based) • is modular • extendible for other frameworks • de facto standard of Java Enterprise Application
  8. 8. 8 | GEEK ACADEMY 2013 Dependency Injection/Inversion of Control
  9. 9. 9 | GEEK ACADEMY 2013 Dependency Injection/Inversion of Control • ทั่วไป • DI Hollywood Principle: "Don't call me, I'll call you."
  10. 10. 10 | GEEK ACADEMY 2013 Module of Spring
  11. 11. 11 | GEEK ACADEMY 2013 Full-fledged Spring web application
  12. 12. 12 | GEEK ACADEMY 2013 Using a third-party web framework
  13. 13. 13 | GEEK ACADEMY 2013 Remoting usage
  14. 14. 14 | GEEK ACADEMY 2013 EJBs - Wrapping existing POJOs
  15. 15. 15 | GEEK ACADEMY 2013 LAB1 Setup/Basic DI
  16. 16. 16 | GEEK ACADEMY 2013 public interface MovieFinder { public List<Movie> findMovie(); } public class MovieLister { private MovieFinder finder; public MovieLister(MovieFinder finder) { this.finder = finder; } public void listMovie() { ... } } public class MovieFinderImpl2 implements MovieFinder { private List<Movie> movies; public MovieFinderImpl2(InputStream is) { movies = new ArrayList<Movie>(); try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ((line=br.readLine())!=null) { String[] csv = line.split("||"); movies.add(new Movie(csv[0], csv[1], Float.parseFloat(csv[2]))); } } catch (IOException e) {} } @Override public List<Movie> findMovie() { return movies; } } public class Assembler { public static void main(String[] args) { InputStream is = Assembler.class.getClass() .getResourceAsStream("/movie.csv"); MovieFinder finder = new MovieFinderImpl2(is); MovieLister lister = new MovieLister(finder); lister.listMovie(); } } public class MovieListerTest { @Test public void testListMovie() { //given MovieFinder finder = mock(MovieFinder.class); when(finder.findMovie()).thenReturn(new ArrayList<Movie>()); MovieLister lister = new MovieLister(finder); //when lister.listMovie(); //then verify(finder).findMovie(); } } public class MovieFinderImpl2Test { @Test public void testFindMovie() { //given InputStream is = this.getClass().getResourceAsStream("/test-movie.csv"); MovieFinder finder = new MovieFinderImpl2(is); //when List<Movie> results = finder.findMovie(); //then assertEquals(6, results.size()); } } LAB1 Setup/Basic DI
  17. 17. Basic Bean GEEK ACADEMY 2013
  18. 18. 18 | GEEK ACADEMY 2013 Basic bean • What is bean? • Creating bean • Bean scope • IoC Container • Additional Features • Bean Life cycle
  19. 19. 19 | GEEK ACADEMY 2013 What is bean? • The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. • A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. • These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML <bean/> definitions which you have already seen in previous chapters.
  20. 20. 20 | GEEK ACADEMY 2013 Creating bean • Declaring a simple bean. <bean id="hello" class="mypackage.HelloWorld" /> HelloWorld hello = new mypackage.HelloWorld();
  21. 21. 21 | GEEK ACADEMY 2013 Creating bean • Injecting through constructors. <bean id="hello" class="mypackage.HelloWorld"> <constructor-arg value="Hello World" /> </bean> HelloWorld hello = new mypackage.HelloWorld(“Hello World”);
  22. 22. 22 | GEEK ACADEMY 2013 Creating bean • Creating bean form factory. <bean id="md5Digest" class="java.security.MessageDigest" factory-method="getInstance"> <constructor-arg value="MD5"/> </bean> MessageDigest md5Digest = new java.security.MessageDigest .getInstance(“MD5”);
  23. 23. 23 | GEEK ACADEMY 2013 Injecting into bean properties • Injecting simple values. <bean id="hello" class="mypackage.HelloWorld"> <property name=”message” value="Hello World" /> </bean> HelloWorld hello = new mypackage.HelloWorld(); hello.setMessage(“Hello World”);
  24. 24. 24 | GEEK ACADEMY 2013 Injecting into bean properties • References to other beans (collaborators). <bean id="msgService" class="mypackage.MessageService"> <property name=”messageProvider” ref="hello" /> </bean> HelloWorld hello = … MessageService msgService = new mypackage.MessageService(); msgService.setMessageProvider(hello);
  25. 25. 25 | GEEK ACADEMY 2013 Injecting into bean properties • Inner beans <bean id="outer" class="..."> <!-- instead of using a reference to a target bean, simply define the target bean inline --> <property name="target"> <!-- this is the inner bean --> <bean class="com.mycompany.Person"> <property name="name" value="Fiona Apple"/> <property name="age" value="25"/> </bean> </property> </bean>
  26. 26. 26 | GEEK ACADEMY 2013 Injecting into bean properties • Collections – java Collection type (List, Set, Map, and Properties) – <list/>, <set/>, <map/>, <props/> <list> <value> <bean … /> </value> <ref bean="somebean" /> </list> <props> <prop key="admin">admin@company.org</prop> <prop key="support">support@company.org</prop> <prop key="develop">develop@company.org</prop> </props>
  27. 27. 27 | GEEK ACADEMY 2013 Injecting into bean properties • Nulls <property name=“target”> <null /> </property>
  28. 28. 28 | GEEK ACADEMY 2013 Injecting into bean properties • p-namespace – Using xml schema "http://www.springframework.org/schema/p" <bean name="john-classic" class="com.mycompany.Person"> <property name="name" value="John Doe"/> <property name="spouse" ref="jane"/> </bean> <bean name="john-modern" class="com.mycompany.Person“ p:name="John Doe" p:spouse-ref="jane"/>
  29. 29. 29 | GEEK ACADEMY 2013 Injecting into bean properties • Spring Expression Language – SpEL (v3.0) <bean id="numberGuess" class="org.spring.samples.NumberGuess"> <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }" /> <!-- other properties --> </bean> <bean id="shapeGuess" class="org.spring.samples.ShapeGuess"> <!-- refer to other bean properties by name --> <property name="initialShapeSeed" value="#{ numberGuess.randomNumber }" /> <!-- other properties --> </bean>
  30. 30. 30 | GEEK ACADEMY 2013 Bean Scope • Control bean creation in spring context. • Default scope is singleton.
  31. 31. 31 | GEEK ACADEMY 2013 Bean Scope • Singleton scope
  32. 32. 32 | GEEK ACADEMY 2013 Bean Scope • Prototype scope
  33. 33. 33 | GEEK ACADEMY 2013 Bean Scope • Other scopes – request, session, and global session are only used in web-based applications. • Custom scopes – Spring Framework Reference
  34. 34. IoC Container GEEK ACADEMY 2013
  35. 35. 35 | GEEK ACADEMY 2013 IoC Container • Lightweight Container • Application lifecycle • Container Overview • Configuration metadata • Instantiating a container
  36. 36. 36 | GEEK ACADEMY 2013 Lightweight Container • Lifecycle management • Lookup • Configuration • Dependency resolution
  37. 37. 37 | GEEK ACADEMY 2013 Lightweight Container(Feature Added) • Transaction • Thread management • Object pooling • Clustering • Management • Remoting • Exposing remote services • Consuming remote services • Customization and extensibility • AOP
  38. 38. 38 | GEEK ACADEMY 2013 Application lifecycle
  39. 39. 39 | GEEK ACADEMY 2013 Container Overview • Essence of IoC container
  40. 40. 40 | GEEK ACADEMY 2013 Configuration metadata • XML-based configuration metadata • Annotation-based configuration metadata (v2.5) – Java-based configuration (3.0) provided by Spring JavaConfig project
  41. 41. 41 | GEEK ACADEMY 2013 Configuration metadata • XML-based configuration <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions go here --> </beans>
  42. 42. 42 | GEEK ACADEMY 2013 Configuration metadata • Java-based configuration (v3.0) @Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } } <beans> <bean id="myService" class="com.acme.services.MyServiceImpl"/> </beans>
  43. 43. 43 | GEEK ACADEMY 2013 Instantiating a container • ApplicationContext represents the Spring IoC container ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"}); ApplicationContext ctx = new AnnotationConfigApplicationContext(MyService.class, Dependency1.class, Dependency2.class); GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.getEnvironment().setActiveProfiles("production", "no-jms"); context.load("spring-master.xml"); context.refresh();
  44. 44. 44 | GEEK ACADEMY 2013 Using the container // retrieve configured instance PetStoreService service = context.getBean("petStore", PetStoreService.class); // use configured instance List userList = service.getUsernameList();
  45. 45. 45 | GEEK ACADEMY 2013 Additional features
  46. 46. 46 | GEEK ACADEMY 2013 Additional features • Bean definition inheritance • Importing configuration files • Using depends-on • Lazy-initialized beans • Property Placeholder
  47. 47. 47 | GEEK ACADEMY 2013 Bean definition inheritance • Declaring parent and child beans
  48. 48. 48 | GEEK ACADEMY 2013 Bean definition inheritance <bean id="inheritedTestBeanWithoutClass" abstract="true"> <property name="name" value="parent"/> <property name="age" value="1"/> </bean> <bean id="inheritsWithClass" class="org.springframework.beans.DerivedTestBean" parent="inheritedTestBeanWithoutClass" init-method="initialize"> <property name="name" value="override"/> <!-- age will inherit the value of 1 from the parent bean definition--> </bean> <bean id="inheritsWithClass2" class="org.springframework.beans.DerivedTestBean" parent="inheritedTestBeanWithoutClass" init-method="initialize"> ... </bean> <bean id="inheritsWithClass3" class="org.springframework.beans.DerivedTestBean" parent="inheritedTestBeanWithoutClass" init-method="initialize"> ... </bean> • Declaring parent and child beans
  49. 49. 49 | GEEK ACADEMY 2013 Importing configuration files • xml-context-config.xml • xml-service-config.xml • xml-repository-config.xml <beans> <import resource="classpath:xml-repository-config.xml"/> <import resource="classpath:xml-service-config.xml"/> </beans> <beans> <bean id="finder" class="mypackage.MovieFinder"/> </beans> <beans> <bean id="lister" class="mypackage.MovieLister"> <constructor-arg ref="finder"/> </bean> </beans>
  50. 50. 50 | GEEK ACADEMY 2013 Using depends-on • Bean dependency – one bean is set as a property of another – accomplish this with the <ref/> element in XML-based configuration metadata – sometimes dependencies between beans are less direct • For example, a static initializer in a class needs to be triggered, such as database driver registration.
  51. 51. 51 | GEEK ACADEMY 2013 Using depends-on <bean id="beanOne" class="ExampleBean" > <property name="manager" ref="manager" /> </bean> <bean id="manager" class="ManagerBean" /> <bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao"> <property name="manager" ref="manager" /> </bean> <bean id="manager" class="ManagerBean" /> <bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" /> • Direct Dependency • Indirect Dependency
  52. 52. 52 | GEEK ACADEMY 2013 Lazy-initialized beans • IoC container create a bean instance when it is first requested <bean id="lazy" class="ExpensiveToCreateBean" lazy-init="true"/>
  53. 53. 53 | GEEK ACADEMY 2013 Property Placeholder <context:property-placeholder location="classpath:com/foo/jdbc.properties"/> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> jdbc.driverClassName=org.hsqldb.jdbcDriver jdbc.url=jdbc:hsqldb:hsql://production:9002 jdbc.username=sa jdbc.password=root
  54. 54. 54 | GEEK ACADEMY 2013 LAB2 Wiring Bean
  55. 55. 55 | GEEK ACADEMY 2013 LAB2 Wiring Bean MovieFinder MovieFinderImpl2 MovieLister CheckSum Spring Container Assembler
  56. 56. Bean Lifecycle GEEK ACADEMY 2013
  57. 57. 57 | GEEK ACADEMY 2013 Bean Lifecycle • Bean Lifecycle • Lifecycle Callback • Bean Aware • FactoryBean • Extension Points
  58. 58. 58 | GEEK ACADEMY 2013 Bean Life cycle
  59. 59. 59 | GEEK ACADEMY 2013 Lifecycle Callback • Implement Interface – InitializingBean.afterPropertiesSet() – DisposableBean.destroy() • <bean/> element – Init-method attribute – destroy-method attribute <bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/> <bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/> @PostConstruct @PreDestroy Internally, the Spring Framework uses BeanPostProcessor implementations to process any callback interfaces it can find and call the appropriate methods.
  60. 60. 60 | GEEK ACADEMY 2013 Bean Aware • Spring offers a range of Aware interfaces that allow beans to indicate to the container that they require a certain infrastructure dependency. – ApplicationContextAware - Declaring ApplicationContext – BeanFactoryAware - Declaring BeanFactory – BeanNameAware - Name of the declaring bean – ResourceLoaderAware - Configured loader for low-level access to resources – ServletConfigAware - Current ServletConfig the container runs in. Valid only in a web-aware Spring ApplicationContext – ServletContextAware - Current ServletContext the container runs in. Valid only in a web-aware Spring ApplicationContext – Etc.
  61. 61. 61 | GEEK ACADEMY 2013 Customizing instantiation logic with a FactoryBean • FactoryBean interface is a point of pluggability into the Spring IoC container's instantiation logic for complex initialization bean. public interface FactoryBean<T> { /** Return an instance of the object managed by this factory. */ T getObject() throws Exception; /** Return the type of object that this FactoryBean creates. */ Class<?> getObjectType(); /** Is the object managed by this factory a singleton? */ boolean isSingleton(); } <bean id="bean" class="MyFactoryBean"/>
  62. 62. 62 | GEEK ACADEMY 2013 Container Extension Points • Container Extension Points – Spring Framework Reference – BeanPostProcessor • InitDestroyAnnotationBeanPostProcessor • RequiredAnnotationBeanPostProcessor – BeanFactoryPostProcessor • PropertyPlaceholderConfigurer • PropertyOverrideConfigurer
  63. 63. 63 | GEEK ACADEMY 2013 Container Extension Points • Becareful – Note also that BeanPostProcessors registered programmatically are always processed before those registered through auto-detection, regardless of any explicit ordering. – Note that if you have beans wired into your BeanPostProcessor using autowiring or @Resource (which may fall back to autowiring), Spring might access unexpected beans when searching for type-matching dependency candidates, and therefore make them ineligible for auto-proxying or other kinds of bean post-processing. For example, if you have a dependency annotated with @Resource where the field/setter name does not directly correspond to the declared name of a bean and no name attribute is used, then Spring will access other beans for matching them by type. • This means that if you have @Transactional in a bean that is loaded in reference to a BeanPostProcessor, that annotation is effectively ignored. • The solution generally would be that if you need transactional behavior in a bean that has to be loaded in reference to a BeanPostProcessor, you would need to use non-AOP transaction definitions - i.e. use TransactionTemplate. • Container Extension Points – Spring Framework Reference
  64. 64. 64 | GEEK ACADEMY 2013 Forget It !!!
  65. 65. Annotation GEEK ACADEMY 2013
  66. 66. 66 | GEEK ACADEMY 2013 Annotation • Annotation-based container configuration • Classpath scanning and managed components • Java-based container configuration
  67. 67. 67 | GEEK ACADEMY 2013 Annotation-based container configuration • Stereotypical @Component @Repository @Service @Controller @Configuration Any Component Data access Service classes Spring MVC Java Config
  68. 68. 68 | GEEK ACADEMY 2013 Annotation-based container configuration • Basic annotations (1) – Spring Annotations • @Autowired • @Qualifier • @Required • @Value – JSR 250 (Common Annotations for the Java) • @Resource • @PostConstruct • @PreDestroy – JSR 330 (Dependency Injection for Java) • @Inject • @Named • @Scope • @Singleton • @Named
  69. 69. 69 | GEEK ACADEMY 2013 Annotation-based container configuration • Basic annotations (2) – Spring Context • @Scope • @Bean • @DependsOn • @Lazy – Transactional • @Transactional
  70. 70. 70 | GEEK ACADEMY 2013 @Value @Component("ejbAccessService") public class EjbAccessService { /** The provider api connection url. */ @Value("${provider.api.url}") private String apiUrl; /** The provider api connection username. */ @Value("${provider.api.username}") private String apiUsername; /** The provider api connection password. */ @Value("${provider.api.password}") private String apiPassword; ... } provider.api.url=corbaloc:iiop:localhost:2809 provider.api.username=Administrator provider.api.password=password <context:property-placeholder location="classpath:META-INF/provider.properties" properties-ref="providerProps" /> <util:properties id="providerProps"> <prop key="provider.api.url">t3://127.0.0.1:7001</prop> <prop key="provider.api.username">weblogic</prop> <prop key="provider.api.password">weblogic</prop> </util:properties> Properties placeholders
  71. 71. 71 | GEEK ACADEMY 2013 @Autowired (1) @Service("accountService") public class AccountServiceImpl { @Autowired private AccountRepository repository; @Autowired public AccountServiceImpl(AccountRepository repository) {} @Autowired(required = false) public void setRepository(AccountRepository repository) {} @Autowired public void populate(Repository r, OrderService s) {} ... } Matches by Type → Restricts by Qualifiers → Matches by Name
  72. 72. 72 | GEEK ACADEMY 2013 @Autowired (2) @Service public class AccountServiceImpl { @Autowired private AccountRepository[] repositories; @Autowired public AccountServiceImpl(Set<AccountRepository> set) {} @Autowired(required = false) public void setRepositories(Map<String,AccountRepository> map){} ... }
  73. 73. 73 | GEEK ACADEMY 2013 @Required public class AccountServiceImpl { private AccountRepository repository; @Required public void setRepository(AccountRepository repository) {} ... } <bean name="accountService" class="AccountServiceImpl"> <property name="repository" ref="accountRepository"/> </bean>
  74. 74. 74 | GEEK ACADEMY 2013 @Qualifier public class ReportServiceImpl { @Autowired @Qualifier("main") private DataSource mainDataSource; @Autowired @Qualifier("freeDS") private DataSource freeDataSource; ... } <beans> <bean class="org.apache.commons.dbcp.BasicDataSource"> <qualifier value="main"/> </bean> <bean id="freeDS" class="org.apache.commons.dbcp.BasicDataSource"/> ... </beans>
  75. 75. 75 | GEEK ACADEMY 2013 @Scope @Service @Scope(BeanDefinition.SCOPE_PROTOTYPE) public class AccountServiceImpl implements AccountService { ... }
  76. 76. 76 | GEEK ACADEMY 2013 @Lazy & @DependsOn @Lazy @Service @DependsOn({"orderService", "currencyService"}) public class AccountServiceImpl implements AccountService { ... }
  77. 77. 77 | GEEK ACADEMY 2013 Factory method component @Component public class CurrencyRepositoryFactory { @Bean @Lazy @Scope("prototype") @Qualifier("public") public CurrencyRepository getCurrencyRepository() { return new CurrencyMapRepository(); } }
  78. 78. 78 | GEEK ACADEMY 2013 JSR 250 @Resource public class AccountServiceImpl { @Resource(name = "orderService") private OrderService orderService; @Resource(name = "orderService") public void setOrderService(OrderService orderService) {} @Resource private AuditService auditService; @Resource public void setAuditService(AuditService auditService) {} ... } If no name is specified explicitly, the default name is derived from the field name or setter method. If no explicit name specified, and similar to @Autowired, @Resource finds a primary type match instead of a specific named bean. Matches by Name → Matches by Type → Restricts by Qualifiers (ignored if match is found by name)
  79. 79. 79 | GEEK ACADEMY 2013 Spring Annotation vs JSR-330 Spring JSR-330 @Autowired @Inject Has no 'required' attribute @Component @Named @Scope @Scope Only for meta-annotations and injection points @Scope @Singleton Default scope is line 'prototype' @Qualifier @Named @Value X @Required X @Lazy X
  80. 80. 80 | GEEK ACADEMY 2013 JSR 330 @Named @Singleton public class AccountServiceImpl implements AccountService { @Inject private AccountRepository repository; @Inject public AccountServiceImpl(@Named("default") AccountRepository r) {} @Inject public void setAccountRepository(AccountRepository repository) {} ... } @Inject Matches by Type → Restricts by Qualifiers → Matches by Name
  81. 81. 81 | GEEK ACADEMY 2013 Classpath scanning and managed components • Basic Configuration <!-- looks for annotations on beans --> <context:annotation-config/> <!-- scan stereotyped classes and register BeanDefinition --> <context:component-scan base-package="org.bank.service,org.bank.repository">
  82. 82. 82 | GEEK ACADEMY 2013 LAB3 Annotation-based
  83. 83. 83 | GEEK ACADEMY 2013 Java-based container configuration • Annotation-based configuration metadata @Configuration public class BankConfg { @Bean public TransferService createTransferService() { return new TransferServiceImpl(); } @Bean(name = "exchangeService", initMethod = "init") public Exchange createExchangeService() { return new ExchangeServiceImpl(); } }
  84. 84. 84 | GEEK ACADEMY 2013 Java-based container configuration ApplicationContext context = new AnnotationConfigApplicationContext(BankConfig.class); • Instantiating a container – Scanning ApplicationContext context = new AnnotationConfigApplicationContext(); context.scan("org.bank.config"); context.refresh();
  85. 85. 85 | GEEK ACADEMY 2013 @Import @Configuration @Import({RepositoryConfig.class, ServiceConfig.class}) public class BankConfig { ... } @Configuration public class RepositoryConfg { ... } @Configuration public class ServiceConfig { ... }
  86. 86. 86 | GEEK ACADEMY 2013 @ImportResources @Configuration @ImportResource("classpath:spring-master.xml") public class BankConfig { @Value("${jdbc.url}") private String jdbcUrl; @Bean public DataSourse dataSource() { return new SimpleDataSource(jdbcUrl); } } jdbc.url=jdbc:derby://localhost:1527/test jdbc.user=admin jdbc.password=password <context:property-placeholder location="classpath:META-INF/jdbc.properties" /> Properties placeholders
  87. 87. 87 | GEEK ACADEMY 2013 Dependency injection @Configuration public class BankServiceConfig { @Autowired private CurrencyRepository currencyRepository; @Bean public CurrencyService currencyService() { return new CurrencyServiceImpl(currencyRepository); } @Bean(name = {"orderBuilder", "builder"}) public OrderBuilder orderBuilder() { return new OrderBuilder(currencyService()); } }
  88. 88. 88 | GEEK ACADEMY 2013 Approach to configuration • XML – infrastructure beans • Annotations – working beans • Java – an alternative to the FactoryBean
  89. 89. 89 | GEEK ACADEMY 2013 LAB4 Java-based
  90. 90. 90 | GEEK ACADEMY 2013 Links • SpringSource • Spring Reference 3.2 • Thank for – Dmitry Noskov
  91. 91. 91 | GEEK ACADEMY 2013 Books
  92. 92. GEEK ACADEMY 2013
  93. 93. THANK YOU FOR YOUR TIME GEEK ACADEMY 2013

×