53
S Fꜳꝏ S Fꜳꝏ S Fꜳꝏ S Fꜳꝏ S Fꜳꝏ S Fꜳꝏ S Fꜳꝏ S Fꜳꝏ 勉強会 勉強会 勉強会 勉強会 勉強会 勉強会 勉強会 勉強会 2008/03/04 2008/03/04 2008/03/04 2008/03/04 2008/03/04 2008/03/04 2008/03/04 2008/03/04 ꜳꜳ ://.ꜳꜳ../ꜳꜳ/ ꜳꜳ ://.ꜳꜳ../ꜳꜳ/ ꜳꜳ ://.ꜳꜳ../ꜳꜳ/ ꜳꜳ ://.ꜳꜳ../ꜳꜳ/ ꜳꜳ ://.ꜳꜳ../ꜳꜳ/ ꜳꜳ ://.ꜳꜳ../ꜳꜳ/ ꜳꜳ ://.ꜳꜳ../ꜳꜳ/ ꜳꜳ ://.ꜳꜳ../ꜳꜳ/

Spring Framework勉強会

Embed Size (px)

DESCRIPTION

社内で行ったSpring Framework勉強会の資料。 Spring Frameworkの概要やSpring Framework2.5の新機能について。

Citation preview

  • 1. Spring Framework 2008/03/04 matsukaz [http://d.hatena.ne.jp/matsukaz/]

2. Spring Framework Spring Framework 3. Spring Framework Spring Framework 2.5 4. Spring Framework Spring Framework Spring Framework AOP etc... 5. Spring FrameworkSpring Framework Rod JohnsonExpert One-on- One J2EE Design and Development IoCInversion of Control Spring Source(Interface 21) POJO SpringAPI 6. Spring FrameworkSpring Framework : http://springframework.org/ 7. Spring FrameworkSpring Framework CoreIoCDIContext IoCDIDAO JDBCORM JPAJDOHibernateiBATISO/RAOP AOPWeb WebWebWorkStrutsMVC WebMVC 8. Spring Framework ClassPathXmlApplicationContext ApplicationContext ac ==ApplicationContext ac new ClassPathXmlApplicationContext(quot;applicationContext.xmlquot;);new ClassPathXmlApplicationContext(quot;applicationContext.xmlquot;); FileSystemXmlApplicationContext ApplicationContext ac ==ApplicationContext ac new FileSystemClassPathXmlApplicationContext(quot;conf/appContext.xmlquot;);new FileSystemClassPathXmlApplicationContext(quot;conf/appContext.xmlquot;); 9. Spring Framework XmlWebApplicationContext WebApp WebApplicationContext ContextLoader org.springframework.web.context.ContextLoaderListenerorg.springframework.web.context.ContextLoaderListener contextcontextorg.springframework.web.context.ContextLoaderServletorg.springframework.web.context.ContextLoaderServlet11 10. Spring Framework Web org.springframework.web.context.request.RequestContextListenerorg.springframework.web.context.request.RequestContextListener requestContextFilterrequestContextFilterorg.springframework.web.filter.RequestContextFilterorg.springframework.web.filter.RequestContextFilterrequestContextFilterrequestContextFilter/*/* 11. Spring Framework contextConfigLocationcontextConfigLocation/WEB-INF/applicationContext.xml /WEB-INF/applicationContext.xml WebApplicationContext ////ApplicationContextnullApplicationContextnull ApplicationContext ac ==WebApplicationContextUtils ApplicationContext ac WebApplicationContextUtils.getWebApplicationContext(getServletContext()); //ServletContext .getWebApplicationContext(getServletContext()); //ServletContext////ApplicationContextIllegalStateExceptionApplicationContextIllegalStateException ApplicationContext ac ==WebApplicationContextUtils ApplicationContext ac WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); .getRequiredWebApplicationContext(getServletContext()); 12. Spring Framework static 13. Spring Framework public class FugaImpl {{ public class FugaImpl private Hoge hoge;private Hoge hoge; public FugaImpl(Hoge hoge) {{public FugaImpl(Hoge hoge)this.hoge ==hoge; this.hoge hoge; }}}} 14. Spring Framework public class FugaImpl {{ public class FugaImpl private Hoge hoge;private Hoge hoge; public void setHoge(Hoge hoge) {{public void setHoge(Hoge hoge)this.hoge ==hoge; this.hoge hoge; }}}}p-namespace 15. Spring Framework Autowiring public class FugaImpl {{public class FugaImplprivate Hoge hoge; private Hoge hoge;public void setHoge(Hoge hoge) {{ public void setHoge(Hoge hoge) this.hoge ==hoge;this.hoge hoge;}} }} byType byName 16. Spring Framework lookuppublic abstract class FooImpl {{ public abstract class FooImpl protected abstract Hoge createHoge();protected abstract Hoge createHoge();}} 17. Spring Framework ServiceLocatorFactoryBean ObjectFactoryCreatingFactoryBean 18. Spring Framework Collection ListSetMapPropertiespublic class FooImpl {{ public class FooImpl private List messageList;private List messageList; private Map users;private Map users; public void setMessageList(List messageList) {{public void setMessageList(List messageList)this.messageList ==messageList; this.messageList messageList; }} public void setUsers(Map users) {{public void setUsers(Map users)this.users ==users; this.users users; }}}}Foo1Foo1Foo2Foo2 19. Spring Framework 5 singleton 1prototype request HTTP1request.setAttribute(bean, )session HTTP1session.setAttribute(bean,)globalPortletWebsession 20. Spring Framework Bean e.g. singletonBeanrequestBean CGLIBJSE 21. Spring FrameworkAOP Spring AOPpure Java AspectJ join pointmethod execution AOP 2 @AspectJ Bean 22. Spring FrameworkAOP Spring AOPadviceadvice Before advicejoin pointAfter returning advice join pointAfter throwing advicejoin pointAfter advice join pointAround advicejoin point join point 23. Spring FrameworkAOP @Aspect @Aspect @Aspectpublic class HogeAspect {{ public class HogeAspect@Pointcut(quot;execution(public **sample5.HogeImpl.*(..))quot;) @Pointcut(quot;execution(public sample5.HogeImpl.*(..))quot;)private void hogeOperation() {} private void hogeOperation() {}@Before(hogeOperation()) //pointcut@Before(hogeOperation()) //pointcut public void beforeAdvice() {{public void beforeAdvice() }} @AfterReturning(within(sample5.*)) //join point@AfterReturning(within(sample5.*)) //join point public void afterReturingAdvice() {{public void afterReturingAdvice() }}}} 24. Spring FrameworkAOP Beanexpression=quot;execution(public sample5.HogeImpl.*(..))quot;/> 25. Spring FrameworkAOP pointcut execution withinthisProxyadvicetargetadviceargsadvice@target @args @within @annotation beanSpringBeanSpring 2.5 26. Spring FrameworkAOP advice Ordered int getValue() { return 10; } @Order @Order(value=10) Bean advice 27. Spring FrameworkAOP AOP Proxy JSE dynamic proxy [default] CGLIB proxy @AspectJ Bean 28. Spring Framework JTAJDBC HibernateJPAJDOAPI : http://springframework.org/ 29. Spring Framework package sample; package sample; public interface HogeService {{ public interface HogeServiceHoge getHoge(String name);Hoge getHoge(String name);void insertHoge(Hoge hoge);void insertHoge(Hoge hoge);}} public class HogeServiceImpl implements HogeService {{ public class HogeServiceImpl implements HogeService public Hoge getHoge(String name) {{public Hoge getHoge(String name)//// }} public void insertHoge(Hoge hoge) {{public void insertHoge(Hoge hoge)//// }}}} 30. Spring Framework Bean 31. Spring Framework nameYes (*) get*on*Eventpropagation NoREQUIRED REQUIRED SUPPORTSMANDATORYREQUIRES_NEW NOT_SUPPORTEDNEVERNESTEDisolation NoDEFAULTDEFAULT READ_UNCOMMITTEDREAD_COMMITTED REPEATABLE_READSERIALIZABLEtimeout No-1 read-only Nofalserollback-forNo Exception Exception no-rollback-for No Exception RuntimeException 32. Spring Framework @Transactional@Transactional @Transactionalpublic class HogeServiceImpl {{ public class HogeServiceImpl@Transactional(readOnly=true)@Transactional(readOnly=true) public Hoge getHoge(String name) {{public Hoge getHoge(String name)//// }} @Transactional(@Transactional(propagation=Propagation.REQUIRED, propagation=Propagation.REQUIRED,rollbackFor=sample.HogeException) rollbackFor=sample.HogeException) public void insertHoge(Hoge hoge) {{public void insertHoge(Hoge hoge)//// }}}} 33. Spring Framework @Transactional propagation enum:Propagation Bean isolation enum:Isolation Bean timeout intreadOnlybooleanrollbackFor Class rollbackForClas snamenoRollbackFor Class noRollbackForClassname 34. Spring Frameworketc... Bean 35. Spring Frameworketc... Bean InitializingBeanDisposableBean Beaninit-methoddestroy-method Spring 2.5 36. Spring Frameworketc... lazy-init singletonBeanlazy-inittrue 37. Spring Frameworketc... XML util jeeJNDIEJBlookuplang jmsJMStx aopAOPcontextApplicationContext 38. Spring Frameworketc... PropertyPlaceholderConfigurer Bean classpath:com/foo/jdbc.propertiesclasspath:com/foo/jdbc.properties 39. Spring Frameworketc... ResourceBundleMessageSource ResouceBundleMessageSource class=quot;org.springframework.context.support.ResourceBundleMessageSourcequot;> MessageSource messages ==(MessageSource) ac.getBean(quot;messageSourcequot;);MessageSource messages (MessageSource) ac.getBean(quot;messageSourcequot;); System.out.println(messages.getMessage(quot;sample7.hogequot;, new Object[]{quot;Hoge1quot;}, null));System.out.println(messages.getMessage(quot;sample7.hogequot;, new Object[]{quot;Hoge1quot;}, null)); 40. Spring Framework2.5 Bean 41. Spring Framework2.5 @Autowired@Qualifier Bean privateOK setterOKpublic class FugaImpl implements Fuga {{public class FugaImpl implements Fuga@Autowired @Autowired@Qualifier(hoge) //@QualifierbyTypebyName @Qualifier(hoge) //@QualifierbyTypebyNameprivate Hoge hoge; private Hoge hoge; }} 42. Spring Framework2.5 @Resource @Autowired + @Qualifier BeanJNDI JSR-250Common Annotations for the Java Platform public class FugaImpl implements Fuga {{public class FugaImpl implements Fuga@Resource(name=hoge) @Resource(name=hoge)private Hoge hoge; private Hoge hoge; }} 43. Spring Framework2.5 @Required Bean @Resource @Autowiredrequired true public class FugaImpl implements Fuga {{public class FugaImpl implements Fugaprivate Hoge hoge; private Hoge hoge;@Required @Required@Resource(name=hoge) @Resource(name=hoge)public void setHoge(Hoge hoge) {{ public void setHoge(Hoge hoge) this.hoge ==hoge;this.hoge hoge;}} }} 44. Spring Framework2.5Bean @Component Bean Bean Bean@Component(fuga)@Component(fuga) public class FugaImpl implements Fuga {{public class FugaImpl implements Fuga@Autowired @Autowired@Qualifier(hoge) @Qualifier(hoge)private Hoge hoge; private Hoge hoge; }} 45. Spring Framework2.5Bean @Repository @Component PersistenceBean DAODataAccessException DAO @Repository(fugaDAO)@Repository(fugaDAO) public class FugaDAOImpl implements FugaDAO {{public class FugaDAOImpl implements FugaDAO }} 46. Spring Framework2.5Bean @Service @Component ServiceBean @Component @Service(fugaDAO)@Service(fugaDAO) public class FooServiceImpl implements FooService {{public class FooServiceImpl implements FooService }} 47. Spring Framework2.5Bean @Controller @Component Spring Web MVCController Bean 48. Spring Framework2.5Bean @Scope Bean Bean scoped-proxyBean @Component(quot;hogequot;)@Component(quot;hogequot;) @Scope(quot;prototypequot;)@Scope(quot;prototypequot;) public class HogeImpl implements Hoge {{public class HogeImpl implements Hoge }} 49. Spring Framework2.5 @PostConstruct@PreDestroy Bean JSR-250 public class HogeImpl implements Hoge {{public class HogeImpl implements Hoge@PostConstruct @PostConstructpublic void init() {{ public void init()}}@PreDestroy @PreDestroypublic void destroy() {{ public void destroy()}} }} 50. Spring Framework2.5 Bean @PostConstruct InitializingBean afterPropertiesSet() Beaninit-method Bean @PreDestroy DisposableBeandestroy() Beandestroy-method 51. Spring Framework2.5 @Autowired@Resource @Component@Repository@Service base-package component-scan annotation-config 52. Spring Framework2.5 component-scan include-filterexclude-filter Beantypeannotationorg.example.SomeAnnotationassignableorg.example.SomeClassregex org.example.Default.*aspectj org.example..*Service+ 53. Spring 2.5 XML XML POJO Spring