Java EE 7 - 日本オラクル | Integrated Cloud Applications … Common Annotation (EE環境) ※...

174

Transcript of Java EE 7 - 日本オラクル | Integrated Cloud Applications … Common Annotation (EE環境) ※...

  • Java EE 7

    Fusion Middleware Java

    Java Day Tokyo 2015 201548

    Copyright 2015, Oracle and/or its affiliates. All rights reserved. |

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. |

    Oracle Confiden@al Internal/Restricted/Highly Restricted

    4

    OracleJavaOracle Corpora@on

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 5

    1. 2. Managed Bean 3. Interceptor 4. Bean Validation 5. DI 6. CDI

    @torazuka

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 6

    1. 2. Managed Bean 3. Interceptor 4. Bean Validation 5. DI 6. CDI

    @torazuka

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. |

    JPE 1.0 J2EE 1.2 J2EE 1.3 J2EE 1.4 Java EE 5 Java EE 6 Java EE 7

    20065 200912 20135

    J2EE (>_

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 8

    ( () ()

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 9

    Java EE 7

    Java EE 7

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 10

    JSP 2.3 /JSTL JSON-P 1.0 EL 3.0 Servlet 3.1

    Java EE 7

    WebSocket 1.0 JAX-RS 2.0 JSF 2.2

    DI 1.0 / CDI 1.1 / EJB 3.2

    JPA 2.1

    JTA 1.2

    JMS 2.0

    JCA 1.7

    Batch 1.0 JavaMail 1.5

    Intercep

    tors 1.2

    Bean

    ValidaO

    on 1.1

    Common

    Ann

    otaO

    on 1.2

    Man

    aged

    Bean 1.0

    Concurrency UOl fo

    r EE 1.0

    Java EE 7

    Java EE 7

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 11

    DB

    Queue

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 12

    DB

    Queue

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 13

    Java Common Annotation JSR-250 Managed Bean JSR-316 Interceptor JSR-318

    Bean Validation JSR-349 DI JSR-330 CDI JSR-346 EJB JSR-345

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 14

    1. 2. Managed Bean 3. Interceptor 4. Bean Validation 5. DI 6. CDI

    @torazuka

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 15

    Java Common Annotation JSR-250 Managed Bean JSR-316 Interceptor JSR-318

    Bean Validation JSR-349 DI JSR-330 CDI JSR-346 EJB JSR-345

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 16

    JSR-250 Common Annotation (EE)

    Web EJB JSR-250 EE.6.29 Common Annotations for the JavaTM Platform 1.2 Requirements

    Resource Resources PostConstructPreDestroy RunAs DeclareRolesRolesAllowed PermitAll DenyAllManagedBean DataSourceDefiniOon DataSourceDefiniOonsPriority

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 17

    JSR-316 Managed Bean

    JSF Managed Bean

    CDI EJB

    EE @ManagedBean

    Managed Bean

    JSR-316 Managed Bean

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 18

    Web

    CDI

    EJB

    Managed Bean

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 19

    Managed Bean #1 : JMS (Queue, Topic) ORB

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 20

    Ini@alContext ic = new Ini@alContext(); ManagedExecutorService managedExecSvc = (ManagedExecutorService ) ic.lookup (" java:comp/DefaultManagedExecutorService"); mangedExecSvc.submit(() -> System.out.println(New Thread));

    Java EE

    JNDI

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 21

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 22

    @Resource(name = concurrent/DefaultManagedExecutorService") ManagedExecutorService mangedExecSvc; public void foo(){ mangedExecSvc.submit(() -> System.out.println(New Thread)); }

    Java EE @Resource

    @Resource

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 23

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 24

    javax.annota@on @Resource JNDI

    JMS ORB

    @Resource JNDI

    JSR-250 Common Annotation

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 25

    #2

    Managed Bean

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 26

    public class Foo() { @Resource(name = concurrent/DefaultManagedExecutorService") ManagedExecutorService mangedExecSvc; public Foo(){ mangedExecSvc.submit(() -> System.out.println(Init something)); } }

    java.lang.NullPointerExcepOon

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 27

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 28

    @PostConstruct

    public class Foo() { @Resource(name = concurrent/DefaultManagedExecutorService") ManagedExecutorService mangedExecSvc; @PostConstruct public void init (){ mangedExecSvc.submit(() -> System.out.println(Init something)); } }

    EE @PostConstruct

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 29

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 30

    javax.annota@on @PostConstruct

    @PreDestroy

    CDI EJB Proxy Proxy (2) Why is the constructor invoked twice when a normal scoped bean is created? http://www.cdi-spec.org/faq/#accordion7

    JSR-250 Common Annotation

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 31

    Web

    CDI

    EJB

    : @Resource : @PostConstruct : @PreDestroy

    Managed Bean

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 32

    1. 2. Managed Bean 3. Interceptor 4. Bean Validation 5. DI 6. CDI

    @torazuka

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 33

    Java Common Annotation JSR-250 Managed Bean JSR-316 Interceptor JSR-318

    Bean Validation JSR-349 DI JSR-330 CDI JSR-346 EJB JSR-345

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 34

    A B C D E

    Interceptor :

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 35

    Java EE 5 (EJB 3.0) @AroundInvoke @Intercerptors @ExcludeDefaultInterceptors @ExcludeClassInterceptors

    EJB

    Bean XML

    Java EE 6 (EJB 3.1) CDI @InterceptorBinding @Interceptor @AroundTimeout

    XML

    Java EE 7 (EJB 3.1 : Maintenance Release) Interceptor.Priority @AroundConstruct

    Interceptor

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 36

    #3

    Interceptor

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 37

    public void executeSomeMethod1 () { long start = System.currentTimeMillis(); // // long end = System.currentTimeMillis(); // long Ome = end - start; logger.log(Level.DEBUG, (ms) : + Ome); }

    ?

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 38

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 39

    public class SomeLogic { @MySimpleProfilerInterceptor public void executeSomeMethod1 () { // } }

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 40

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 41

    1. Interceptor

    2. Interceptor

    3. Interceptor

    Java EE 7 : Interceptor

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 42

    @Inherited @InterceptorBinding @Target({ElementType.TYPE, ElementType.METHOD}) @Reten@on([email protected]) public @interface MySimpleProfilerInterceptor { }

    1.

    @InterceptorBinding

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 43

    @Priority(Interceptor.Priority.APPLICATION + 10) @MySimpleProfilerInterceptor // @Interceptor public class MyInterceptor { @AroundInvoke public Object calcExecTime (InvocaOonContext ic) throws Excep@on { // } }

    2. Interceptor

    Java EE 7 @Priority beans.xml

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 44

    @AroundInvoke public Object calcExecTime (InvocaOonContext ic) throws ExcepOon { long start = System.currentTimeMillis(); // try { return ic.proceed(); // } catch (Excep@on e) { throw e; } finally { long end = System.currentTimeMillis(); // long Ome = end - start ; String className = ic.getTarget().getClass().getSuperclass().getName(); String methodName = ic.getMethod().getName() ; logger.log(Level.INFO, "{0}#{1} took {2} (ms)", new Object[]{ className, methodName, Ome }); } }

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 45

    @MySimpleProfilerInterceptor // public void executeSomeMethod() { // do Something }

    3. Interceptor

    : jp.co.oracle.cdis.HomePage#executeSomeMethod took 300 (ms)

    or

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 46

    A B C D E

    @MyInteceptor @MyInteceptor @MyInteceptor @MyInteceptor @MyInteceptor

    Interceptor :

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 47

    1. 2. Managed Bean 3. Interceptor 4. Bean Validation 5. DI 6. CDI

    @torazuka

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 48

    Java Common Annotation JSR-250 Managed Bean JSR-316 Interceptor JSR-318

    Bean Validation JSR-349 DI JSR-330 CDI JSR-346 EJB JSR-345

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 49

    DB

    Queue

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 50

    #:

    Bean Validation

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 51

    public class Person { private String name; private String jpZipCode; public String doSomething() { if (name == null || name.equals("")) { validaOonFailed();}; if (jpZipCode == null || jpZipCode.equals("")) { validaOonFailed();}; Pajern pajern = Pajern.compile("^\\d{3}-\\d{4}$"); Matcher matcher = pajern.matcher(jpZipCode); if (!matcher.find()){validaOonFailed();} // }}

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. |

    public class Person { private String name; private String jpZipCode; public String doSomething() { if (name == null || name.equals("")) { validaOonFailed();}; if (jpZipCode == null || jpZipCode.equals("")) { validaOonFailed();}; Pajern pajern = Pajern.compile("^\\d{3}-\\d{4}$"); Matcher matcher = pajern.matcher(jpZipCode); if (!matcher.find()){validaOonFailed();} // } }

    52

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 53

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 54

    public class Person { @NotNull @Size(min=1) private String name; @NotNull @Size(min=8,max=8) @Pajern("^\\d{3}-\\d{4}$") private String jpZipCode; public String doSomething() { //

    Bean Validation

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 55

    1. 2. Managed Bean 3. Interceptor 4. Bean Validation 5. DI 6. CDI

    @torazuka

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 56

    Java Common Annotation JSR-250 Managed Bean JSR-316 Interceptor JSR-318

    Bean Validation JSR-349 DI JSR-330 CDI JSR-346 EJB JSR-345

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 57

    DB

    Queue

    new? Factory?

    ?

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 58

    @Inject @Qualifier @Named @Scope @Singleton

    javax.inject

    EE

    JSR-330 : Dependency Injection for Java

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 59

    #

    Dependency Injection

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 60

    public class PersonDAOFromCSV implements PersonDAO { public List getPersons() throws IOExcepOon{ FileSystem fs = FileSystems.getDefault(); Path file = fs.getPath("/tmp/listPerson.csv"); return Files.newBufferedReader(file).lines().map(str -> { String[] array = str.split(","); Person person = new Person(); person.setName(array[0]); person.setAge(Integer.parseInt(array[1])); return person; }).collect(Collectors.toList()); } }

    CSV List

    CSV

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 61

    public class PersonController { public void listPersonName() throws IOExcep@on { // Person DAO PersonDAO personDAO = new PersonDAOFromCSV(); List persons = personDAO.getPersons(); persons.forEach(person -> System.out.println(person.getName())); } }

    newFactoryAbstract FactoryService Locator

    DB

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 62

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 63

    public class PersonController {

    @Inject PersonDAO personDAO ; // public void listPersonName() throws IOExcep@on { List persons = personDAO.getPersons(); persons.forEach(person -> System.out.println(person.getName())); } }

    new -> Factory -> Abstract Factory -> Service Locator

    ,, !!

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 64

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 65

    PersonController PersonDAO

    PersonDAO

    DI

    PersonDAOnew PersonDAO()

    dao.getPersons()

    PersonController@Inject PersonDAO

    dao.getPersons()

    PersonDAO PersonDAO

    PersonDAO

    new @Inject

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 66

    #

    Dependency Injection

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 67

    public interface Service { public void doSomething (); }

    public class DukeService implements Service { @Override public void doSomething () { // System.out.println(Duke Service); } }

    public class DuchessService implements Service { @Override public void doSomething () { // System.out.println(Duchess Service); } }

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 68

    public class Foo { @Inject private Service service; public void execService(){ service.doSomething(); } }

    OR ???

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 69

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 70

    @Inject @DukeQualifier Service service1; public void execMul@pleService(){ service1.doSomething(); }

    @Inject @DuchessQualifier Service service2; public void execMul@pleService(){ service2.doSomething(); }

    (Qualifier)

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 71

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 72

    (@Qualifier)

    Qualifier

    Qualifier :

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 73

    @Dependent @Qualifier @Reten@on([email protected]) @Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER}) public @interface DukeQualifier {}

    (@Qualifier)

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 74

    @DukeQualifier public class DukeServiceImpl implements Service { @Override public void doSomething() { System.out.println("Duke Service"); } }

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 75

    @Inject @DukeQualifier Service service1; public void execMul@pleService(){ service1.doSomething(); }

    Qualifier

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 76

    (Qualifier) / com.yoshio3.qualifiers.* @DukeQualifier, @DatabaseQualifier

    Qualifier

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 77

    # ()

    Dependency Injection

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 78

    public interface Service { public void doSomething (); }

    @DukeQualifier public class DukeService implements Service{ @Override public void doSomething () { // System.out.println(Duke Service); } }

    @Dependent @Qualifier @Reten@on([email protected]) @Target({ElementType.FIELD,

    ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})

    public @interface DukeQualifier { }

    Duke

    : @Qualifier

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 79

    Duke

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 80

    public class Foo { @Inject @DukeQualifier private final Service service; public void execService(){ service.doSomething(); } }

    final

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 81

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 82

    @Dependent public class Foo { private final Service service; @Inject public Foo(@DukeQualifier Service service) { this.service = service; } public void execService(){ service.doSomething(); }}

    final

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 83

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 84

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 85

    #

    Dependency Injection

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 86

    public interface Service { public void doSomething (); }

    @Duke@Duchess

    @Printer @IoT@MyEnterprise

    @Hoge@Foo @Bar@Oracle@MySQL

    @GlassFish

    ?!

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 87

    ?!

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 88

    public interface Service { public void doSomething (); }

    @Named(value=duke) public class DukeService implements Service{ @Override public void doSomething () { // System.out.println(Duke Service); } }

    OK !!

    @Named

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 89

    public interface Service { public void doSomething (); }

    @Named(value=duke) public class DukeService implements Service{ @Override public void doSomething () { // System.out.println(Duke Service); } }

    @Inject @Named(value=duke) Service service1; public void execMul@pleService(){ service1.doSomething(); }

    !!

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 90

    !!

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 91

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 92

    public interface Service { public void doSomething (); }

    @Named(value=duke) public class DukeService implements Service{ @Override public void doSomething () { // System.out.println(Duke Service); } }

    @Inject @Named(value=duke) Service service1; public void execMul@pleService(){ service1.doSomething(); }

    (Weld)

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 93

    DI CDI

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 94

    JSF(EL ) DI

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 95

    1. 2. Managed Bean 3. Interceptor 4. Bean Validation 5. DI 6. CDI

    @torazuka

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 96

    Java Common Annotation JSR-250 Managed Bean JSR-316 Interceptor JSR-318

    Bean Validation JSR-349 DI JSR-330 CDI JSR-346 EJB JSR-345

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. |

    97

    @Resource DI/CDI

    EJB

    @Inject

    @EJBManaged Bean

    Java EE

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 98

    EJBCDI

    16:15- 17:05

    CDI EJB (Out of Scope)

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 99

    javax.decorator javax.enterprise.context javax.enterprise.event javax.enterprise.inject javax.enterprise.uOl

    JSR-346 : Contexts and Dependency Injection for Java EE 1.2

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 100

    CDI Managed Bean

    Java EE

    EL

    Observer

    Java EE CDI

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 101

    Contexts&Dependency Injection

    #CDI CDI

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 102

    Java EE 7

    CDI Managed Bean

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 103

    /WEB-INF/beans.xml

    It is strongly recommended you use "annotated

    (Java EE 6)

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 104

    CDI Container

    Java Class

    Java Class

    Java Class

    Java Class Java Class

    Java Class

    Java Class

    Java Class

    Java Class

    Java Class

    Java Class

    CDI

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 105

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. |

    CDI DI

    106

    CDI Managed Bean Bean @NormalScope

    @ApplicaOonScoped @SessionScoped @ConversaOonScoped @RequestScoped ()

    @Dependent () @Interceptor @Decorator @Stereotype

    UnsaOsfied dependencies for type NGClass with qualifiers @Default

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 107

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 108

    CDI Managed Bean ?

    CDI Container

    Contextual Reference

    Proxy

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 109

    @Inject Proxy

    CDI Container

    Contextual Reference

    Proxy

    @Inject SomeManagedBean bean;

    bean Proxy

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 110

    CDI Managed Bean (Proxy)

    Qualifiers (Set) Scope Bean Stereotypes (Set) Type Bean (Set) AlternaOve Bean Alternative

    : @Named

    CDI Managed Bean

    javax.enterprise.inject.spi.Bean

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 111

    Java EE / Web Container

    CDI Container

    Contextual Reference

    CDI Managed Bean

    Bean Proxy

    UserTransacOon Principal Validator ValidatorFactory HjpServletRequest HjpSession ServletContext

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 112

    #10

    Contexts&Dependency Injection

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. |

    DI : javax.inject.Scope Singleton

    CDI : javax.enterprise.context Dependent

    CDI CDI : javax.enterprise.context. NormalScoped () ApplicationScoped SessionScoped RequestScoped ConversationScoped

    JSF : javax.faces. view.ViewScoped flow.FlowScoped

    113

    Java EE

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 114

    15:10- 16:00

    CDI JSF

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 115

    javax.inject.Singleton ?

    javax.enterprise.context.ApplicaOonScoped ?

    Java EE

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 116

    @javax.inject.Singleton public class DukeCounter { private int counter; public int getCounter() { return counter++; } }

    @Singleton Managed Bean

    @javax.inject.Singleton

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 117

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 118

    @javax.enterprise.context.ApplicaOonScoped public class DukeCounter implements Serializable { private int counter; public int getCounter() { return counter++; } }

    ApplicationScoped !!

    Java EE

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 119

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 120

    javax.inject.Singlton

    CDI Container

    Contextual Reference

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 121

    @SessionScoped // @ConversaOonScoped public class CounterHoldInSession implements Serializable{ @Inject DukeCounter singleton; }

    Java EE 6 (Singleton & Serializable) Weld(CDI )

    @Singleton

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 122

    #11 :

    Contexts&Dependency Injection

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 123

    public interface Service { public void doSomething (); }

    public class DukeSvc implements Service { @Override public void doSomething () { // System.out.println(Duke Service); } }

    DukeAlpha DukeBeta DukeProd

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 124

    public interface DataAccessService { public List getData (); }

    class implements DataAccessService { @Override public List getData () { // DB } }

    class implements DataAccessService { @Override public List getData () { // DB } }

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 125

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 126

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 127

    @Dependend @AlternaOve public class DukeAlpha implements Service{ }

    @Dependend @AlternaOve public class DukeBeta implements Service{ }

    @Alternative()

    Alternative XML

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 128

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 129

    @Dependend @AlternaOve public class DukeAlpha implements Service{ }

    @Dependend @AlternaOve public class DukeBeta implements Service{ }

    annotated CDI @Dependent

    @Alternative()

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 130

    @Dependend @AlternaOve public class DukeAlpha implements Service{ }

    @Dependend @AlternaOve public class DukeBeta implements Service{ }

    DukeBeta

    beans.xml

    (CDI 1.0)

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 131

    @Dependend @AlternaOve @Priority(Interceptor. Priority.APPLICATION + 10) public class DukeAlpha implements Service{ }

    @Dependend @AlternaOve @Priority(Interceptor. Priority.APPLICATION + 20) public class DukeBeta implements Service{ }

    jar

    (CDI 1.1)

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 132

    @Inject Service service ; @Dependend @AlternaOve @Priority(Interceptor. Priority.APPLICATION + 20) public class DukeBeta implements Service{ }

    DukeBeta

    @Inject XML

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 133

    !!

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 134

    @Inject Service @DukeServiceQualifier service ; @DukeServiceQualifier public class DukeServiceWithQualifier implements Service { }

    @AlternaOve !!

    (Qualifier)

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 135

    @Inject Service @DukeServiceQualifier service ; @DukeServiceQualifier @Specializes public class SpecializedDuke extends DukeServiceWithQualifier { @Override public void doSomething() { } }

    @Specialized (CDI 1.1)

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 136

    #12 CDI Managed Bean

    Contexts&Dependency Injection

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 137

    Oracle

    MySQL

    PostgreSQL

    DB

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 138

    @Stateless public class SomeLogic1 { PersitenceContext (unitName=ORACLE_PU) EnOtyManager em; @Override public void doSomething () { // System.out.println(Duke Service); } }

    @Stateless public class SomeLogic2 { PersitenceContext (unitName=MYSQL_PU) EnOtyManager em; @Override public void doSomething () { // System.out.println(Duke Service); } } EJB EJB

    : EJB + JPA

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 139

    JPA

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 140

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 141

    @Inject @DatabaseQualifier(value= DatabaseList.Oracle) EnOtyManager em; @Inject @DatabaseQualifier(value= DatabaseList.MySQL) EnOtyManager em;

    @Produces

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 142

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 143

    @Produces

    (enum)

    (Qualifier)

    @Produces

    DB

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 144

    (enum) public enum DatabaseList {

    Oracle, MySQL, PostgreSQL

    }

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 145

    (Qualifier) @Dependent @Qualifier @Reten@on([email protected]) @Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER}) public @interface DatabaseQualifier { DatabaseList value() default DatabaseList.Oracle ; }

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 146

    @Produces !! @Dependent public class DatabaseProducer { @Produces @DatabaseQualifier(value = DatabaseList.Oracle) @PersistenceContext(unitName = "ORACLE_PU") private En@tyManager emOracle ; @Produces @DatabaseQualifier(value = DatabaseList.MySQL) @PersistenceContext(unitName = "MySQL_PU") private En@tyManager emMySQL; }

    Oracle

    MySQL

    !! !!

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 147

    DB @Dependent @TransacOonal public class SomeBusinessLogic { @Inject @DatabaseQualifier(value= DatabaseList.MySQL) EnOtyManager em; public List getPersons(){ em.createQuery(..); } }

    value Oracle

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 148

    #14 :

    Contexts&Dependency Injection

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 149

    @Named @RequestScope @SecurityChekIntercepter @LoggerIntercepter @TransacOonal public class IndexPage { @Override public void doSomething () { // System.out.println(Duke Service); }}

    !! @Named @RequestScope @SecurityChekIntercepter @LoggerIntercepter

    @TransacOonal public class

    UserRegPage {

    }

    @Named @RequestScope @SecurityChekIntercepter @LoggerIntercepter

    @TransacOonal public class

    UserConfirmPage {

    }

    @Named @RequestScope @SecurityChekIntercepter @LoggerIntercepter

    @TransacOonal public class

    UserComplPage {

    }

    !!

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 150

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 151

    @MyReqestedScopeStereotype public class IndexPage { @Override public void doSomething () { // System.out.println(Duke Service); }}

    @MyRequestedScopeStereotype

    public class UserRegPage {

    }

    @MyRequestedScopeStereotype

    public class UserConfirmPage {

    }

    @MyRequestedScopeStereotype

    public class UserComplPage {

    }

    Proj

    @Stereotype

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 152

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 153

    @Stereotype @Named @RequestScope @SecurityChekIntercepter @LoggerIntercepter @TransacOonal @Stereotype @Target(TYPE) @Reten@on(RUNTIME) public @interface MyReqestedScopeStereotype {}

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 154

    @Named @RequestScope @SecurityChekIntercepter @LoggerIntercepter @TransacOonal public class IndexPage { @Override public void doSomething () { // System.out.println(Duke Service); }}

    @MyReqestedScopeStereotype public class IndexPage { @Override public void doSomething () { // System.out.println(Duke Service); }}

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 155

    @Stereotype : @Model @Named @ReauestScope public class IndexPage { @Override public void doSomething () { // System.out.println(Duke Service); }}

    @javax.enterprise.inject.Model public class IndexPage { @Override public void doSomething () { // System.out.println(Duke Service); }}

    Web @Named, @RequestedScope @Model

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 156

    Stereotype Stereotype / com.yoshio3.stereotypes.* @DukeStereotype, @DatabaseStereotype

    Stereotype

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 157

    #13 : Observer

    Contexts&Dependency Injection

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 158

    Observer

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 159

    @RequestScoped @Named(value = userReg") public class UserRegistra@onPage { @Inject Event mailEvent; // public void execUserRegistra@on() { // mailEvent.fire(new Mail(mailaddredd, name, message)); }

    CDI

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 160

    @Applica@onScoped public class MailEventHandler { @Resource(name = "mail/MyMailSession") Session mailSession; public void receiveEvent(@Observes Mail mailEvent) { sendMessage(mailEvent); } }

    CDI

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 161

    !!

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 162

    @Applica@onScoped public class MailEventHandler { @Resource(name = "mail/MyMailSession") Session mailSession; public void receiveEvent(@Observes Mail mailEvent) { sendMessage(mailEvent); // } }

    NG

    CDI

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 163

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 164

    EJB JMS !! @Stateless public class MailEventDispatcherBean { @Inject Event mailEvent; // @Asynchronous public void produceEvent(final Mail message) { // mailEvent.fire(message); }

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 165

    EJB JMS !! @javax.ejb.Singleton // javax.inject.Singleton EJB public class EventConsumer { @Asynchronous @Lock(LockType.READ) public void consumeEvent(@Observes Mail message) throws InterruptedExcep@on { // }

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 166

    @RequestScoped @Named(value = userReg") public class UserRegistra@onPage { @EJB MailEventDispatcherBean dispather; public void execUserRegistra@on() { // dispather.produceEvent(new Mail(mailaddredd, name, message)); }

    EJB JMS !!

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 167

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 168

    CDI Event

    CDI 2.0 Asynchronous (https://issues.jboss.org/browse/CDI-499)

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 169

    @torazuka

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 170

    : Managed Bean CDI EJB

    DI/CDI, EJB

    Managed Bean

    Managed Bean

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. |

    171

  • Copyright 2015, Oracle and/or its affiliates. All rights reserved. | 172