Your SlideShare is downloading. ×
Java EE 6 CDI Integrates with Spring & JSF
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Java EE 6 CDI Integrates with Spring & JSF

4,085

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
4,085
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
39
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Java EE 6 CDI Integrates with Spring & JSF on Java EE 5 Jiayun Zhou jiayun@jiayun.org 2012/03/17 TWJUG
  • 2. 先鋒的第一次 迎新再對折
  • 3. CDI• JSR 299: Contexts and Dependency Injection• Java EE 6 3
  • 4. Copyright by IISI. All rights reserved 5
  • 5. Copyright by IISI. All rights reserved 6
  • 6. • http://www.slideshare.net/johaneltes/java-ee6-cdi
  • 7. Inversion of Control vsDependency Injection
  • 8. IoCInversion of Control 9
  • 9. IoCInversion of Control Flow 10
  • 10. Martin Fowler 11
  • 11. Command Line#rubyputs What is your name?name = getsprocess_name(name)puts What is your quest?quest = getsprocess_quest(quest) 12
  • 12. GUIrequire tkroot = TkRoot.new()name_label = TkLabel.new() {text "What is Your Name?"}name_label.packname = TkEntry.new(root).packname.bind("FocusOut") {process_name(name)}quest_label = TkLabel.new() {text "What is Your Quest?"}quest_label.packquest = TkEntry.new(root).packquest.bind("FocusOut") {process_quest(quest)}Tk.mainloop() 13
  • 13. Hollywood PrincipleDon’t call us, we’ll call you. 14
  • 14. Ex. HttpSessionListener• sessionCreated()• sessionDestroyed() 15
  • 15. Ex. Template Method Pattern 16
  • 16. Dependency Injection One Form of IoC 17
  • 17. class MovieLister... public Movie[] moviesDirectedBy(String arg) { List allMovies = finder.findAll(); for (Iterator it = allMovies.iterator(); it.hasNext();) { Movie movie = (Movie) it.next(); if (!movie.getDirector().equals(arg)) it.remove(); } return (Movie[]) allMovies.toArray(new Movie[allMovies.size()]); } 18
  • 18. public interface MovieFinder { List findAll();} 19
  • 19. class MovieLister... private MovieFinder finder; public MovieLister() { finder = new ColonDelimitedMovieFinder("movies1.txt"); } 20
  • 20. 21
  • 21. 22
  • 22. Spring Stereotype• @Component• @Repository• @Service• @Controller 23
  • 23. @Repositoryclass ColonMovieFinder... public void setFilename(String filename) { this.filename = filename; } 24
  • 24. Spring @Autowiredclass MovieLister... private MovieFinder finder; @Autowired public void setFinder(MovieFinder finder) { this.finder = finder; } 25
  • 25. CODI• Apache MyFaces Extensions CDI project 26
  • 26. 同時作業Demo 27
  • 27. 同時作業Demo 28
  • 28. Weld• CDI Implementation• Included in WebLogic 12c• Running on WebLogic 11g 31
  • 29. Libraries<dependency> <groupId>org.jboss.weld.servlet</groupId> <artifactId>weld-servlet</artifactId></dependency><dependency> <groupId>org.apache.myfaces.extensions.cdi.bundles</groupId> <artifactId>myfaces-extcdi-bundle-jsf20</artifactId></dependency><dependency> <groupId>org.apache.myfaces.extensions.cdi.modules.jee5-support</groupId> <artifactId>myfaces-extcdi-jee5-weld-support</artifactId> <scope>runtime</scope></dependency>
  • 30. Libraries<dependency> <groupId>org.cdisource.springbridge</groupId> <artifactId>springbridge</artifactId></dependency><dependency> <groupId>org.cdisource.beancontainer</groupId> <artifactId>beancontainer-weld-impl</artifactId></dependency>
  • 31. web.xml<listener> <listener-class>org.cdisource.springintegration.servletsupport.ApplicationContextFinderServletContextListener</listener-class></listener><listener> <listener-class>org.apache.myfaces.extensions.cdi.weld.startup.WeldAwareConfigurationListener</listener-class></listener>
  • 32. beans.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"></beans>• src/main/webapp/WEB-INF• src/main/resources都要放
  • 33. Spring Bridge• src/main/resources/META- INF/services/javax.enterprise.inject.spi.Extension• 內容: org.cdisource.springintegration.SpringIntegration Extention• Service Provider Interface (SPI) http://docs.oracle.com/javase/7/docs/api/java/ util/ServiceLoader.html
  • 34. Spring 掃描排除 CDI Bean<context:component-scan base-package="com.xxx"> <context:exclude-filter type="regex"expression="com.xxx.*.web.*" /></context:component-scan>
  • 35. Controller 引用 Service@WindowScoped@XxxExceptionCatcher@Named("xxxController")public class XxxController implements Serializable { @Inject @Spring(name = "registerService") private transient RegisterService registerService; 38
  • 36. CDI AOP (annotation)@InterceptorBinding@Target({ ElementType.TYPE,ElementType.METHOD })@Retention(RetentionPolicy.RUNTIME)public @interface XxxExceptionCatcher {}
  • 37. CDI AOP (interceptor)@Interceptor@RisExceptionCatcherpublic class XxxExceptionInterceptor implementsSerializable { @AroundInvoke public Object catchException(InvocationContextctx) throws Exception {
  • 38. CDI AOP (beans.xml)<interceptors> <class>com.xxx.XxxExceptionInterceptor</class></interceptors>
  • 39. Logger@WindowScoped@XxxExceptionCatcher@Named("xxxController")public class XxxController implements Serializable { @Inject private Logger logger;• http://www.slf4j.org/faq.html#declared_static 42
  • 40. LoggerFactoryimport javax.enterprise.inject.Produces;import javax.enterprise.inject.spi.InjectionPoint;import org.slf4j.Logger;public class LoggerFactory { @Produces Logger createLogger(InjectionPoint injectionPoint) { String name =injectionPoint.getMember().getDeclaringClass().getName(); return org.slf4j.LoggerFactory.getLogger(name); }} 43
  • 41. References• http://seamframework.org/Weld/WeldDocum entation• https://cwiki.apache.org/confluence/display/E XTCDI/Documentation 44
  • 42. Sample Code• https://github.com/jiayun/cdisource• https://github.com/jiayun/java_misc_samples 45
  • 43. WebLogic 12c• 必須解壓 CODI jar 到 WEB-INF/classes• 已回報給 Oracle
  • 44. - Thank You -

×