- Hizmet alan ve hizmet veren sınıflar arasında Java arayüzü (=interface) aracılığı ile tanımlanan bağımlılıkların yürütme zamanında çözülmesi
- Yazılımın yapılandırılmasında kullanılan parametrelerin, kodun dışına çıkarılması. Böylelikle, bu parametrelerin, kodun yeniden derlenmesi gerekmeksizin değiştirilebilmesini sağlamak.
1. XML tabanlı Spring yapılandırma dosyası kullanmak:
com.example.SimpleService.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.example.service; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service() public class SimpleService { @Value("${user}") private String username; @Value("${password}") private String password; public SimpleService() { } public String getUsername() { return username; } public String getPassword() { return password; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } } |
spring-config.xml:
1 2 3 4 5 6 7 8 9 | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:annotation-config/> <context:property-placeholder location="classpath*:resources/config.properties" /> <context:component-scan base-package="com.example.service"/> </beans> |
resources/config.properties:
1 2 | user=root password=secret |
com.example.TestProperties.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.example.service; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestProperties { public static void main(String[] args) { ConfigurableApplicationContext context= new ClassPathXmlApplicationContext("spring-config.xml"); SimpleService srv= context.getBean(SimpleService.class); System.out.println(srv.getUsername()); System.out.println(srv.getPassword()); } } |
Uygulamayı çalıştırdığımızda aşağıdaki çıktıyı elde ediyoruz:
1 2 3 4 5 6 | Feb 08, 2014 7:34:07 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@22a92801: startup date [Sat Feb 08 07:34:07 EET 2014]; root of context hierarchy Feb 08, 2014 7:34:07 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15a7f14f: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,appConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,simpleService,placeHolderConfigurer]; root of factory hierarchy root secret |
2. Spring damgalarını kullanarak salt Java sınıfları ile yapılandırma
com.example.config.AppConfig.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.example.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration @PropertySource(value = "classpath:resources/config.properties") @ComponentScan(value = { "com.example.service" }) public class AppConfig { @Bean public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } } |
com.example.TestConfigProperties.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.example.service; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.example.config.AppConfig; public class TestConfigProperties { public static void main(String[] args) { ConfigurableApplicationContext context= new AnnotationConfigApplicationContext(AppConfig.class); SimpleService srv= context.getBean(SimpleService.class); System.out.println(srv.getUsername()); System.out.println(srv.getPassword()); } } |
Uygulamayı çalıştırdığımızda aşağıdaki çıktıyı elde ediyoruz:
1 2 3 4 5 6 | Feb 08, 2014 11:20:40 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@16030e71: startup date [Sat Feb 08 23:20:40 EET 2014]; root of context hierarchy Feb 08, 2014 11:20:41 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15a7f14f: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,appConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,simpleService,placeHolderConfigurer]; root of factory hierarchy root secret |
No comments:
Post a Comment