Jan 12, 2016
Spring DI
Inversion of Control?Dependency Injection?Spring - DI
Interface Hello World! Hi World! .
Rendering .Spring - DI
Spring - DI
Spring - DI
Hello World! Hi World!! World! .
Rendering .
Name .Spring - DI
?Spring - DI
Factory PatternSpring - DI
Spring - DIFactory Pattern
SpringSpring - DI
How?Spring - DI
Metadata(XML Property)Spring Framework(ApplicationContext)Spring - DI
Spring Spring - DI
package spring;
import old.MessageRenderer;
import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.FileSystemResource;
public class DefaultMessageRendererWithSpringTest{
public static void main(String[] args) {BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("conf/spring.xml"));MessageRenderer renderer = (MessageRenderer) beanFactory.getBean("renderer");
renderer.render();}
}Spring Spring - DI
Inversion of Control(IoC)Spring - DI
IoCDIIoC : Inversion of ControlDI : Dependency InjectionDP : Dependency PullDPConstructor InjSetter Inj EJB Spring Spring PicoContainerMethod InjSpring - DI
Spring . Spring , IoC ?
? Spring - DI
Servlet Container Servlet , , , Servlet . Servlet . JSP/Servlet , .
Servlet ContainerServlet AServlet BServlet CServlet DCreateInitServiceDestorySpring - DI
EJB Container EJB(, , MDB) , , , EJB . EJB .Transaction, Security, EJB Pooling .
EJB ContainerEJB AEJB BEJB CEJB DCreateInitServiceDestorySpring - DI
IoC( DI) ContainerPOJO , , POJO . POJO , Container .Transaction, Security . AOP Container .
IoC( DI) ContainerPOJO APOJO BPOJO CPOJO DCreateInitServiceDestorySpring - DI
POJO(Plain Old Java Object) ?Martin Fowler: http://www.martinfowler.com/bliki/POJO.html
Servlet EJB Interface(Contracts) .
POJO .
Spring - DI
Singleton?, Non Singleton?Spring - DI
Singleton Pattern SingletonSpring - DI
public class MessageService { private static MessageService instance;
private MessageService() {}
public static MessageService getInstance() { if(instance == null ) { instance = new MessageService(); } return instance; }}
Singleton Spring - DI
public class MessageProviderFactory { public static final int HI_WORLD_PROVIDER = 0; public static final int HELLO_WORLD_PROVIDER = 1; public static final int ANNYOUNG_WORLD_PROVIDER = 2; private static final Map providers = new HashMap (); static { providers.put(HI_WORLD_PROVIDER, new HiworldMessageProvider()); providers.put(HELLO_WORLD_PROVIDER, new HelloworldMessageProvider()); providers.put(ANNYOUNG_WORLD_PROVIDER, new AnnyoungworldMessageProvider()); } public static MessageProvider getMessageProvider(int status) { return providers.get(status); }}
MessageProviderFactory SingletonSpring - DI
Spring SingletonSpring - DI
Spring - DIServletContext(sigle instance)org.springframework.web.context. WebApplicationContext.ROOT"(key)(value)WebApplicationContext JVM
Spring - DI
Spring 2.0 ScopeSpring - DI
Singleton Spring - DI
public class Person {private String name;
public void setName(String name) {this.name = name;}
public String getName() {return name;}} Thread AThread Bnew Person();new Person();name = nullSingleton InstanceSpring - DI
public class Person {private String name;
public void setName(String name) {this.name = name;}
public String getName() {return name;}} Thread Aperson.setName();name = Singleton InstanceSpring - DI
public class Person {private String name;
public void setName(String name) {this.name = name;}
public String getName() {return name;}} person.setName();name = Thread BSingleton InstanceSpring - DI
public class Person {private String name;
public void setName(String name) {this.name = name;}
public String getName() {return name;}} Thread Aperson.getName(); => name = Singleton InstanceSpring - DI
Static Singleton Dependency .
Singleton Configuration .
interface-unfriendly => Test .
.
Runtime Singleton .
OOP .Spring - DI
Spring
new
Factory method
FactoryBean Interface
ApplicationContext context = new ClassPathXmlApplicationContext(paths);UserService userService = context.getBean(userService);
loggingAdvice emailNotificationThrowsAdvice
ApplicationContext context = new ClassPathXmlApplicationContext(paths);ProxyFactoryBean factoryBean = context.getBean(userService);
loggingAdvice emailNotificationThrowsAdvice
ApplicationContext context = new ClassPathXmlApplicationContext(paths);UserService userService = context.getBean(userService);
java:comp/env/jdbc/petclinic
ApplicationContext context = new ClassPathXmlApplicationContext(paths);JndiObjectFactoryBean factoryBean = context.getBean(dataSource);
java:comp/env/jdbc/petclinic
ApplicationContext context = new ClassPathXmlApplicationContext(paths);DataSource dataSource = (DataSource)context.getBean(dataSource);
Spring FriendsSingleton PatternStrategy PatternFactory PatternObserver PatternTemplate Method PatternSpring - DI
JVM http://hjbang.snut.ac.kr/data/java2005/JAVA06.ppt
http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=1&t=012143Spring - DI
1. Invoke2. 3. setXXX() 4. setBeanName() 5. setBeanFactory() 6. setApplicationContext() 7. afterProperties() 8. init-method POJO POJO POJO 9. destroy() 10. destory-method Spring - DI
POJO InitializingBean, DisposableBean Spring , 123 126 http://wiki.javajigi.net/pages/viewpage.action?pageId=1040 Spring - DI
ApplicationContext BeanNameAware, BeanFactoryAware, ApplicationContextAware
http://wiki.javajigi.net/pages/viewpage.action?pageId=1101Spring - DI
Spring - DI
Why DI?Easy TestDependency Spring - DI