Version 3

    JBoss EJB 3.0 and Extensions

     

    &12488;&12524;&12452;&12523;&65303;&65306;&12469;&12540;&12499;&12473;&12458;&12502;&12472;&12455;&12463;&12488;&12398;&27880;&20837;&65288;Inject Service Objects&65289;

     

    &12399;&12376;&12417;&12395;&65288;Introduction&65289;

     

    So far, you have learned how to build various service objects and use them from the client side -- primarily through JNDI lookup of the service stub objects. However, if you have many service objects, the lookup code quickly becomes tedious and messy: In order to lookup the service, the server and client have to agree on a name for each service, which the developer must keep track of. After the lookup, the service stub objects have to be manually casted to the appropriate interface types -- a mis-matched type could cause runtime exceptions. In addition, the service lookup code has to be duplicated and littered around in the entire application.

     

    &12371;&12371;&12414;&12391;&12289;&12356;&12429;&12356;&12429;&12394;&12469;&12540;&12499;&12473;&12458;&12502;&12472;&12455;&12463;&12488;&12398;&20316;&25104;&26041;&27861;&12392;&12289;&12393;&12398;&12424;&12358;&12395;&12463;&12521;&12452;&12450;&12531;&12488;&20596;&12363;&12425;&20351;&29992;&12377;&12427;&12363;&12434;&23398;&12435;&12391;&12365;&12414;&12375;&12383;&12290;&22522;&26412;&30340;&12395;&12399;JNDI&12434;&21033;&29992;&12375;&12390;&12469;&12540;&12499;&12473;&12398;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&12523;&12483;&12463;&12450;&12483;&12503;&12375;&12414;&12377;&12290;&12375;&12363;&12375;&12289;&12469;&12540;&12499;&12473;&12458;&12502;&12472;&12455;&12463;&12488;&12364;&22679;&12360;&12390;&12367;&12427;&12392;&12289;&12377;&12368;&12395;&12523;&12483;&12463;&12450;&12483;&12503;&12398;&12467;&12540;&12489;&12399;&38263;&12367;&20081;&38609;&12395;&12394;&12387;&12390;&12375;&12414;&12356;&12414;&12377;&12290;&12414;&12383;&12289;&12469;&12540;&12499;&12473;&12434;&12523;&12483;&12463;&12450;&12483;&12503;&12377;&12427;&12395;&12399;&12289;&12469;&12540;&12496;&20596;&12392;&12463;&12521;&12452;&12450;&12531;&12488;&20596;&12391;&20107;&21069;&12395;&21508;&12469;&12540;&12499;&12473;&12398;&21517;&21069;&12434;&21462;&12426;&27770;&12417;&12390;&12362;&12367;&24517;&35201;&12364;&12354;&12426;&12289;&38283;&30330;&32773;&12364;&12381;&12428;&12425;&12434;&35352;&37682;&12375;&12390;&12362;&12367;&24517;&35201;&12364;&12354;&12426;&12414;&12377;&12290;&12523;&12483;&12463;&12450;&12483;&12503;&24460;&12399;&12289;&12469;&12540;&12499;&12473;&12398;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&36969;&20999;&12394;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12398;&22411;&12395;&25163;&21205;&12391;&12461;&12515;&12473;&12488;&12377;&12427;&24517;&35201;&12364;&12354;&12426;&12289;&22411;&12364;&36949;&12387;&12390;&12356;&12383;&22580;&21512;&12395;&12399;&23455;&34892;&26178;&12395;&20363;&22806;&12364;&30330;&29983;&12375;&12390;&12375;&12414;&12356;&12414;&12377;&12290;&12373;&12425;&12395;&12289;&12469;&12540;&12499;&12473;&12398;&12523;&12483;&12463;&12450;&12483;&12503;&12398;&12467;&12540;&12489;&12399;&12289;&20840;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12395;&28193;&12387;&12390;&37325;&35079;&12373;&12428;&25955;&12425;&12363;&12387;&12390;&12356;&12414;&12377;&12290;

     

    Compared with JNDI lookup, a far better and simpler way to obtain the objects inside an EJB 3.0 application is to use the dependency injection design pattern. In this this trail, we will discuss how EJB 3.0 dependency injection works.

     

    EJB 3.0&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&20869;&12391;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&21462;&24471;&12377;&12427;&22580;&21512;&12289;JNDI&12398;&12523;&12483;&12463;&12450;&12483;&12503;&12424;&12426;&12418;&12399;&12427;&12363;&12395;&33391;&12356;&31777;&21336;&12394;&26041;&27861;&12399;&12289;&20381;&23384;&24615;&27880;&20837;&12398;&12487;&12470;&12452;&12531;&12497;&12479;&12540;&12531;&12434;&20351;&12358;&12371;&12392;&12391;&12377;&12290;&12371;&12398;&12488;&12524;&12452;&12523;&12391;&12399;&12289;EJB 3.0&12398;&20381;&23384;&24615;&27880;&20837;&12395;&12388;&12356;&12390;&23398;&12403;&12414;&12377;&12290;

     

    The idea behind dependency injection (a.k.a. Inversion of Control, IoC) is that each application component declares what type of service objects it requires (a.k.a its dependency). The container resolves the dependency between application components, instantiates service objects, and then injects service stubs into the application at runtime via automatic JavaBean setter calls or direct data field value assignment.

     

    &20381;&23384;&24615;&27880;&20837;&65288;Inversion of Control: IoC&12392;&12418;&35328;&12356;&12414;&12377;&65289;&12398;&32972;&24460;&12395;&12354;&12427;&32771;&12360;&26041;&12399;&12289;&21508;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12467;&12531;&12509;&12540;&12493;&12531;&12488;&12364;&12393;&12435;&12394;&12479;&12452;&12503;&12398;&12469;&12540;&12499;&12473;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&24517;&35201;&12392;&12375;&12390;&12356;&12427;&12363;&65288;&12371;&12428;&12434;&20381;&23384;&24615;&12392;&35328;&12356;&12414;&12377;&65289;&12434;&23459;&35328;&12377;&12427;&12392;&12356;&12358;&12371;&12392;&12391;&12377;&12290;&12467;&12531;&12486;&12490;&12364;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12467;&12531;&12509;&12540;&12493;&12531;&12488;&38291;&12398;&20381;&23384;&24615;&12434;&35299;&27770;&12375;&12289;&12469;&12540;&12499;&12473;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&12452;&12531;&12473;&12479;&12531;&12473;&21270;&12375;&12289;&12469;&12540;&12499;&12473;&12398;&12473;&12479;&12502;&12434;&23455;&34892;&26178;&12395;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12395;&27880;&20837;&12375;&12414;&12377;&12290;&27880;&20837;&12399;&33258;&21205;&30340;&12395;JavaBean&12398;&12475;&12483;&12479;&12540;&12513;&12477;&12483;&12489;&12434;&21628;&12403;&20986;&12377;&12363;&12289;&12354;&12427;&12356;&12399;&30452;&25509;&12487;&12540;&12479;&12501;&12451;&12540;&12523;&12489;&12408;&20516;&12434;&20195;&20837;&12377;&12427;&12371;&12392;&12391;&34892;&12356;&12414;&12377;&12290;

     

    EJB 3.0 supports an especially elegant way to implement the dependency injection pattern: use annotations. Let's see how it works.

     

    EJB 3.0&12399;&12289;&20381;&23384;&24615;&27880;&20837;&12398;&12487;&12470;&12452;&12531;&12497;&12479;&12540;&12531;&12398;&23455;&35013;&12434;&12392;&12390;&12418;&12456;&12524;&12460;&12531;&12488;&12394;&26041;&27861;&12289;&12388;&12414;&12426;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12391;&25552;&20379;&12375;&12390;&12356;&12414;&12377;&12290;&12391;&12399;&12289;&12393;&12398;&12424;&12358;&12395;&20351;&29992;&12377;&12427;&12363;&35211;&12390;&12356;&12365;&12414;&12375;&12423;&12358;&12290;

     

    Please note that EJB 3.0 dependency injection annotations only works in EJB objects -- not generic POJOs. For instance, you cannot use the dependency injection annotations in a servlet to obtain session bean objects. In addition, EJB 3.0 dependency injection only works in the local JNDI. Hence you cannot inject objects from remote servers.

     

    EJB 3.0&12398;&20381;&23384;&24615;&27880;&20837;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;EJB&12458;&12502;&12472;&12455;&12463;&12488;&12391;&12398;&12415;&20351;&29992;&12391;&12365;&12427;&12371;&12392;&12395;&27880;&24847;&12375;&12390;&12367;&12384;&12373;&12356;&12290;&19968;&33324;&30340;&12394;POJO&12391;&12399;&21033;&29992;&12391;&12365;&12414;&12379;&12435;&12290;&12383;&12392;&12360;&12400;&12289;&12469;&12540;&12502;&12524;&12483;&12488;&12395;&20381;&23384;&24615;&27880;&20837;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&20351;&29992;&12375;&12390;&12475;&12483;&12471;&12519;&12531;Bean&12458;&12502;&12472;&12455;&12463;&12488;&12434;&21462;&24471;&12377;&12427;&12371;&12392;&12399;&12391;&12365;&12414;&12379;&12435;&12290;&12373;&12425;&12395;&12289;EJB 3.0&12398;&20381;&23384;&24615;&27880;&20837;&12399;&12525;&12540;&12459;&12523;JNDI&12391;&12398;&12415;&20351;&29992;&12391;&12365;&12414;&12377;&12290;&12388;&12414;&12426;&12289;&12522;&12514;&12540;&12488;&12469;&12540;&12496;&12363;&12425;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&27880;&20837;&12377;&12427;&12371;&12392;&12399;&12391;&12365;&12414;&12379;&12435;&12290;

     

    &12469;&12531;&12503;&12523;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&65288;The Sample Application&65289;

     

    In this trail, we go through a sample application that makes extensive use of injected resources. At a glance, the application is very similar to the message driven bean sample application. The CalculatorMDB bean is the end point of the message queue queue/injection. However, instead of performing the service itself, the CalculatorMDB bean delegates the calculation and result storage tasks to two stateless session beans CalculatorBean and RecordManagerBean. The stub objects of both beans are injected into the CalculatorMDB bean.

     

    &12371;&12398;&12488;&12524;&12452;&12523;&12391;&12399;&12289;&27880;&20837;&12522;&12477;&12540;&12473;&12434;&12383;&12367;&12373;&12435;&21033;&29992;&12377;&12427;&12469;&12531;&12503;&12523;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12434;&35211;&12390;&12356;&12365;&12414;&12377;&12290;&27010;&35251;&12399;&12513;&12483;&12475;&12540;&12472;&39366;&21205;&22411;Bean&12398;&12469;&12531;&12503;&12523;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12392;&12384;&12356;&12383;&12356;&21516;&12376;&12391;&12377;&12290;CalculatorMDB&12399;&12289;&12513;&12483;&12475;&12540;&12472;&12461;&12517;&12540;queue/injection&12398;&32066;&31471;&12391;&12377;&12364;&12289;&12469;&12540;&12499;&12473;&12381;&12398;&12418;&12398;&12399;&23455;&34892;&12379;&12378;&12289;&35336;&31639;&12392;&32080;&26524;&20445;&23384;&12398;&20181;&20107;&12434;&65298;&12388;&12398;&12473;&12486;&12540;&12488;&12524;&12473;&12475;&12483;&12471;&12519;&12531;Bean&12289;CalculatorBean&12392;RecordManagerBean&12289;&12395;&22996;&20219;&12375;&12414;&12377;&12290;&12371;&12428;&12425;&65298;&12388;&12398;Bean&12398;&12473;&12479;&12502;&12458;&12502;&12472;&12455;&12463;&12488;&12364;CalculatorMDB&12395;&27880;&20837;&12373;&12428;&12414;&12377;&12290;

     

    The RecordManagerBean bean further makes use of the server's embedded database to store the calculation results for the check.jsp page to look up. The RecordManagerBean bean access the database via JDBC resources injected from the JNDI.

     

    RecordManagerBean&12399;&12289;check.jsp&12506;&12540;&12472;&12364;&12523;&12483;&12463;&12450;&12483;&12503;&12391;&12365;&12427;&12424;&12358;&12395;&12289;&35336;&31639;&32080;&26524;&12434;&12469;&12540;&12496;&12398;&32068;&12415;&36796;&12415;&12487;&12540;&12479;&12505;&12540;&12473;&12395;&20445;&23384;&12375;&12414;&12377;&12290;RecordManagerBean&12399;JNDI&12391;&27880;&20837;&12373;&12428;&12383;JDBC&12522;&12477;&12540;&12473;&32076;&30001;&12391;&12487;&12540;&12479;&12505;&12540;&12473;&12395;&12450;&12463;&12475;&12473;&12375;&12414;&12377;&12290;

     

    @EJB&12391;EJB&12434;&27880;&20837;&12377;&12427;&65288;Injecting EJBs with @EJB&65289;

     

    The @EJB tag can be used to inject EJB stubs. It replaces the session bean lookup code you saw in the JSP pages in the previous trails. In the following example, the rm field variable in the CalculatorMDB class has the type RecordManager, which is the local interface of the RecordManagerBean stateless session bean. So, the container figures out how to retrieve a stub of the RecordManagerBean and assign it to rm before it is first used. From the developer's point of view, rm automatically obtains its value without any lookup code.

     

    EJB&12398;&12473;&12479;&12502;&12434;&27880;&20837;&12377;&12427;&12395;&12399;@EJB&12479;&12464;&12434;&20351;&29992;&12375;&12414;&12377;&12290;&20197;&21069;&12398;&12488;&12524;&12452;&12523;&12391;&35211;&12383;JSP&12506;&12540;&12472;&12398;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12523;&12483;&12463;&12450;&12483;&12503;&12398;&12467;&12540;&12489;&12399;&19981;&35201;&12395;&12394;&12426;&12414;&12377;&12290;&19979;&12398;&20363;&12391;&12399;&12289;CalculatorMDB&12463;&12521;&12473;&12398;&12501;&12451;&12540;&12523;&12489;&22793;&25968;rm&12399;RecordManager&22411;&12391;&12289;&12371;&12428;&12399;RecordManagerBean&12473;&12486;&12540;&12488;&12524;&12473;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12525;&12540;&12459;&12523;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;&12377;&12290;&12388;&12414;&12426;&12289;&12467;&12531;&12486;&12490;&12399;RecordManagerBean&12398;&12473;&12479;&12502;&12434;&21462;&24471;&12377;&12427;&26041;&27861;&12434;&35211;&12388;&12369;&12289;&26368;&21021;&12395;&20351;&29992;&12373;&12428;&12427;&21069;&12395;rm&12395;&12473;&12479;&12502;&12434;&21106;&12426;&24403;&12390;&12414;&12377;&12290;&38283;&30330;&32773;&12398;&35222;&28857;&12391;&35211;&12427;&12392;&12289;&12523;&12483;&12463;&12450;&12483;&12503;&12434;&12375;&12394;&12367;&12390;&12418;rm&12395;&33258;&21205;&30340;&12395;&20516;&12364;&20195;&20837;&12373;&12428;&12414;&12377;&12290;

     

     public class CalculatorMDB implements MessageListener {
     
       @EJB RecordManager rm;
       
       // use the rm variable
       // ... ...
     }
    

     

    Alternatively, the dependency injection annotation can also be applied to setter method. So, the following code would have the exact same effect as the above. The developer does not need to call the setCal() method -- the container does that automatically.

     

    &12354;&12427;&12356;&12399;&12289;&20381;&23384;&24615;&27880;&20837;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&12475;&12483;&12479;&12540;&12513;&12477;&12483;&12489;&12395;&20351;&29992;&12377;&12427;&12371;&12392;&12418;&12391;&12365;&12414;&12377;&12290;&12388;&12414;&12426;&12289;&19979;&12395;&31034;&12377;&12467;&12540;&12489;&12399;&19978;&35352;&12398;&12467;&12540;&12489;&12392;&20840;&12367;&21516;&12376;&21205;&20316;&12395;&12394;&12426;&12414;&12377;&12290;&12467;&12531;&12486;&12490;&12364;&33258;&21205;&30340;&12395;&34892;&12387;&12390;&12367;&12428;&12427;&12398;&12391;&12289;&38283;&30330;&32773;&12399;setCal()&12513;&12477;&12483;&12489;&12434;&21628;&12406;&24517;&35201;&12399;&12354;&12426;&12414;&12379;&12435;&12290;

     

     public class CalculatorMDB implements MessageListener {
     
       RecordManager rm;
       
       @EJB
       public void setRm (RecordManager rm) {
         this.rm = rm;
       }
       
       // use the rm variable
       // ... ...
     }
    

     

    Under some scenarios, the @EJB annotation can also take optional attributes to constrain how the container locates the bean instance.

     

    &12354;&12427;&29366;&27841;&19979;&12391;&12399;&12289;&12467;&12531;&12486;&12490;&12364;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12434;&35211;&12388;&12369;&12427;&26041;&27861;&12434;&25351;&23450;&12377;&12427;&12383;&12417;&12395;&12289;@EJB&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12395;&12356;&12367;&12388;&12363;&12398;&23646;&24615;&12434;&25351;&23450;&12377;&12427;&12371;&12392;&12418;&12391;&12365;&12414;&12377;&12290;

     

    • The beanName attribute specifies the EJB's class name (without package names). Or, if the EJB has an XML descriptor, it refers to the ejb-name attribute.

     

    • beanName&23646;&24615;&12399;EJB&12398;&12463;&12521;&12473;&21517;&65288;&12497;&12483;&12465;&12540;&12472;&21517;&28961;&12375;&65289;&12434;&25351;&23450;&12375;&12414;&12377;&12290;&12354;&12427;&12356;&12399;EJB&12395;XML&35352;&36848;&23376;&12364;&12354;&12427;&22580;&21512;&12399;&12289;ejb-name&23646;&24615;&12434;&25351;&23450;&12375;&12414;&12377;&12290;

     

    • The beanInterface attribute specifies the bean's local or remote business interface, if the variable's type is a super-class of the business interface. The bean instance is injected and automatically casted to the field variable's type.

     

    • beanInterface&23646;&24615;&12399;&12289;&22793;&25968;&12398;&22411;&12364;&12499;&12472;&12493;&12473;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12398;&35242;&12463;&12521;&12473;&12384;&12387;&12383;&22580;&21512;&12395;&12289;Bean&12398;&12525;&12540;&12459;&12523;&12499;&12472;&12493;&12473;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12414;&12383;&12399;&12522;&12514;&12540;&12488;&12499;&12472;&12493;&12473;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12434;&25351;&23450;&12375;&12414;&12377;&12290;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12364;&27880;&20837;&12373;&12428;&12289;&33258;&21205;&30340;&12395;&12501;&12451;&12540;&12523;&12489;&22793;&25968;&12398;&22411;&12395;&12461;&12515;&12473;&12488;&12373;&12428;&12414;&12377;&12290;

     

    • The mappedName attribute specifies the JNDI name for the bean instance. By default it is "EAR-FILE-BASE-NAME/BEAN--CLASS-NAME/local" (or remote).

     

    • mappedName&23646;&24615;&12399;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12398;JNDI&21517;&12434;&25351;&23450;&12375;&12414;&12377;&12290;&12487;&12501;&12457;&12523;&12488;&12398;JNDI&21517;&12399;&12289;"EAR&12501;&12449;&12452;&12523;&12398;&22522;&26412;&21517;"/"Bean&12463;&12521;&12473;&21517;"/local&65288;&12414;&12383;&12399;remote&65289;&12391;&12377;&12290;

     

    The following listing shows how to use the beanName and mappedName attribute to locate the StatelessCalculator session bean in the injection.

     

    &19979;&12395;&31034;&12377;&12467;&12540;&12489;&12399;&12289;beanName&23646;&24615;&12392;mappedName&23646;&24615;&12434;&20351;&29992;&12375;&12390;StatelessCalculator&12475;&12483;&12471;&12519;&12531;Bean&12434;&35211;&12388;&12369;&12289;&27880;&20837;&12377;&12427;&20363;&12391;&12377;&12290;

     

     public class CalculatorMDB implements MessageListener {
     
       @EJB (beanName="StatelessCalculator")
       // or @EJB (mappedName="EJB3Trail/StatelessCalculator/local")
       Calculator cal;
       
       // use the cal variables
       // ... ...
     }
    

     

    Finally, you can use the @EJBs annotation to inject multiple EJB instances to an array.

     

    &26368;&24460;&12398;&27880;&20837;&26041;&27861;&12399;&12289;@EJBs&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&20351;&29992;&12375;&12390;&12289;&37197;&21015;&12395;&35079;&25968;&12398;EJB&12452;&12531;&12473;&12479;&12531;&12473;&12434;&27880;&20837;&12377;&12427;&12371;&12392;&12391;&12377;&12290;

     

    @Resource&12391;&20219;&24847;&12398;&12522;&12477;&12540;&12473;&12434;&27880;&20837;&12377;&12427;&65288;Injecting arbitrary resources with @Resource&65289;

     

    The @EJB annotation only injects EJB stubs. A more generic dependency injection annotation is @Resource. The RecordManagerBean uses @Resource to inject the server's default DataSource, which points to the embedded HSQL database. The bean saves the each calculation result in that database via a JDBC connection obtained from the DataSource. The check.jsp page in the web layer checks the database periodically to display new calculation results when they become available. The annotation's name attribute "java:/DefaultDS" is the global JNDI name of the DataSource.

     

    @EJB&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;EJB&12473;&12479;&12502;&12398;&12415;&12434;&27880;&20837;&12375;&12414;&12377;&12290;&12373;&12425;&12395;&27726;&29992;&30340;&12394;&20381;&23384;&24615;&27880;&20837;&29992;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;@Resource&12391;&12377;&12290;RecordManageBean&12399;@Resource&12434;&20351;&12387;&12390;&12469;&12540;&12496;&12398;&12487;&12501;&12457;&12523;&12488;&12398;DataSource&12391;&12354;&12427;&32068;&12415;&36796;&12415;&12398;HSQL&12487;&12540;&12479;&12505;&12540;&12473;&12434;&27880;&20837;&12375;&12414;&12377;&12290;Bean&12399;&21508;&35336;&31639;&32080;&26524;&12434;&12289;DataSource&12363;&12425;&21462;&24471;&12375;&12383;JDBC&12467;&12493;&12463;&12471;&12519;&12531;&12434;&20351;&12387;&12390;&12487;&12540;&12479;&12505;&12540;&12473;&12395;&20445;&23384;&12375;&12414;&12377;&12290;Web&23652;&12398;check.jsp&12506;&12540;&12472;&12399;&12487;&12540;&12479;&12505;&12540;&12473;&12434;&23450;&26399;&30340;&12395;&12481;&12455;&12483;&12463;&12375;&12289;&26032;&12383;&12394;&35336;&31639;&32080;&26524;&12364;&21462;&24471;&12391;&12365;&12383;&12425;&34920;&31034;&12375;&12414;&12377;&12290;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&23646;&24615;&12398;"java:/DafaultDS"&12364;DataSource&12398;&12464;&12525;&12540;&12496;&12523;JNDI&21517;&12434;&34920;&12375;&12414;&12377;&12290;

     

     public class RecordManagerBean implements RecordManager {
     
       // Inject resources based on jndi names
       @Resource (mappedName="java:/DefaultDS")
       DataSource myDb;
       
       // use myDb to obtain JDBC connection
       // and operate the database ...
     }
    

     

    In general, you can inject any service object from the JNDI using the object's JNDI name. If the JNDI object is in the local (java:comp/env) JNDI trees, you can simply give its mapped Name without the prefix -- the local tree is automatically searched. The following examples injects a messaging connection factory and a messaging queue.

     

    &19968;&33324;&30340;&12395;&12399;&12289;&12458;&12502;&12472;&12455;&12463;&12488;&12398;JNDI&21517;&12434;&20351;&12360;&12400;&12393;&12398;&12469;&12540;&12499;&12473;&12458;&12502;&12472;&12455;&12463;&12488;&12391;&12418;JNDI&12363;&12425;&27880;&20837;&12391;&12365;&12414;&12377;&12290;JNDI&12458;&12502;&12472;&12455;&12463;&12488;&12364;&12525;&12540;&12459;&12523;&12394;JNDI&12484;&12522;&12540;&65288;java:comp/env&65289;&12395;&12354;&12427;&22580;&21512;&12399;&12289;&12510;&12483;&12503;&12373;&12428;&12383;&21517;&21069;&12434;&25509;&38957;&36766;&12394;&12375;&12395;&25351;&23450;&12377;&12428;&12400;&12289;&33258;&21205;&30340;&12395;&12525;&12540;&12459;&12523;&12484;&12522;&12540;&12364;&26908;&32034;&12373;&12428;&12414;&12377;&12290;&12513;&12483;&12475;&12540;&12472;&12398;&12467;&12493;&12463;&12471;&12519;&12531;&12501;&12449;&12463;&12488;&12522;&12392;&12513;&12483;&12475;&12540;&12472;&12461;&12517;&12540;&12434;&27880;&20837;&12377;&12427;&20363;&12434;&19979;&12395;&31034;&12375;&12414;&12377;&12290;

     

     @Resource (mappedName="ConnectionFactory")
     QueueConnectionFactory factory;
     
     @Resource (mappedName="queue/A")
     Queue queue;
    

     

    For "well-known" objects, the @Resource annotation can inject them without the JNDI name. It just figures out the object name from its type. Here are some examples.

     

    &12300;&12424;&12367;&30693;&12425;&12428;&12383;&12301;&12458;&12502;&12472;&12455;&12463;&12488;&12391;&12354;&12428;&12400;&12289;JNDI&21517;&12364;&12394;&12367;&12390;&12418;@Resource&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12391;&27880;&20837;&12391;&12365;&12414;&12377;&12290;&12458;&12502;&12472;&12455;&12463;&12488;&12398;&22411;&12363;&12425;&12458;&12502;&12472;&12455;&12463;&12488;&21517;&12434;&25512;&28204;&12375;&12414;&12377;&12290;&12383;&12392;&12360;&12400;&12289;&27425;&12398;&12424;&12358;&12394;&12418;&12398;&12364;&12354;&12426;&12414;&12377;&12290;

     

     @Resource
     TimerService tms;
     
     @Resource
     SessionContext ctx;
    

     

    Similar to the @EJB annotation, the @Resource annotation can be applied to setter methods and the @Resources annotation can be applied to arrays.

     

    @EJB&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12392;&21516;&27096;&12289;@Resource&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;&12475;&12483;&12479;&12540;&12513;&12477;&12483;&12489;&12395;&12418;&37197;&21015;&12395;&12418;&20351;&29992;&12391;&12365;&12414;&12377;&12290;

     

    &12381;&12398;&20182;&12398;&20381;&23384;&24615;&27880;&20837;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&65288;Other dependency injection annotation&65289;

     

    Both the @EJB and @Resource annotations are specifically tailored to the resources they inject. They simplify the developer's work. Another important dependency injection annotation is to inject EntityManager objects, which handle POJO data persistence operations in EJB 3.0 containers. We will cover EntityManager and its injection annotation in a later trail.

     

    @EJB&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12418;@Resource&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12418;&12289;&27880;&20837;&12377;&12427;&12522;&12477;&12540;&12473;&29992;&12395;&29305;&27880;&12373;&12428;&12390;&12356;&12414;&12377;&12290;&38283;&30330;&32773;&12398;&20316;&26989;&12434;&31777;&21336;&12395;&12377;&12427;&12383;&12417;&12391;&12377;&12290;&12418;&12358;&65297;&12388;&12398;&37325;&35201;&12394;&20381;&23384;&24615;&27880;&20837;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;EntityManager&12458;&12502;&12472;&12455;&12463;&12488;&12398;&27880;&20837;&12391;&12377;&12290;EntityManager&12458;&12502;&12472;&12455;&12463;&12488;&12399;EJB 3.0&12467;&12531;&12486;&12490;&12391;POJO&12487;&12540;&12479;&12398;&27704;&32154;&24615;&20966;&29702;&12434;&34892;&12356;&12414;&12377;&12290;EntityManager&12392;&12381;&12398;&20381;&23384;&24615;&27880;&20837;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12395;&12388;&12356;&12390;&12399;&24460;&12398;&12488;&12524;&12452;&12523;&12391;&23398;&32722;&12375;&12414;&12377;&12290;

     

    &12477;&12540;&12473;&12467;&12540;&12489;&21442;&29031;&65288;Complete source code&65289;

     

    &12469;&12540;&12496;&65288;Server&65289;

     

    • CalculatorMDB.java: The message driven bean that processes the message at queue/injection. Two session beans are injected into it.

     

    • CalculatorMDB.java: queue/injection&12461;&12517;&12540;&12398;&12513;&12483;&12475;&12540;&12472;&12434;&20966;&29702;&12377;&12427;&12513;&12483;&12475;&12540;&12472;&39366;&21205;&22411;Bean&12290;&65298;&12388;&12398;&12475;&12483;&12471;&12519;&12531;Bean&12364;&27880;&20837;&12373;&12428;&12427;&12290;

     

    • CalculationRecord.java: A data object class to store one calculation result from CalculatorMDB. The object is uniquely identified by the service request message's "sent" time stamp.

     

    • CalculationRecord.java: CalculatorMDB&12398;&20491;&12293;&12398;&35336;&31639;&32080;&26524;&12434;&20445;&23384;&12377;&12427;&12487;&12540;&12479;&12458;&12502;&12472;&12455;&12463;&12488;&12463;&12521;&12473;&12290;&12458;&12502;&12472;&12455;&12463;&12488;&12399;&12469;&12540;&12499;&12473;&35201;&27714;&12513;&12483;&12475;&12540;&12472;&12398;&12479;&12452;&12512;&12473;&12479;&12531;&12503;"sent"&12391;&19968;&24847;&12395;&35672;&21029;&12373;&12428;&12427;&12290;

     

    • RecordManager.java: Interface of a session bean to save and retrieve calculation records in the server's embedded database.

     

    • RecordManager.java: &12469;&12540;&12496;&32068;&12415;&36796;&12415;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&35336;&31639;&32080;&26524;&12524;&12467;&12540;&12489;&12434;&20445;&23384;&12375;&12383;&12426;&21462;&24471;&12375;&12383;&12426;&12377;&12427;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12290;

     

    • RecordManagerBean.java: Implementation of a session bean to save and retrieve calculation records in the server's embedded database. The database's corresponding DataSource is injected into it.

     

    • RecordManagerBean.java: &12469;&12540;&12496;&32068;&12415;&36796;&12415;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&35336;&31639;&32080;&26524;&12524;&12467;&12540;&12489;&12434;&20445;&23384;&12375;&12383;&12426;&21462;&24471;&12375;&12383;&12426;&12377;&12427;&12475;&12483;&12471;&12519;&12531;Bean&12398;&23455;&35013;&12290;DataSource&12395;&23550;&24540;&12375;&12383;&12487;&12540;&12479;&12505;&12540;&12473;&12364;&27880;&20837;&12373;&12428;&12427;&12290;

     

    &12463;&12521;&12452;&12450;&12531;&12488;&65288;Client&65289;

     

    • calculator.jsp: The client JSP that takes user input and sends the calculation service request message.

     

    • calculator.jsp: &12518;&12540;&12470;&12363;&12425;&12398;&20837;&21147;&12434;&21463;&12369;&21462;&12426;&12289;&25237;&36039;&35336;&31639;&12398;&12469;&12540;&12499;&12473;&35201;&27714;&12513;&12483;&12475;&12540;&12472;&12434;&36865;&20449;&12377;&12427;&12463;&12521;&12452;&12450;&12531;&12488;JSP&12290;

     

    • check.jsp: The client JSP that uses the RecordManager to check whether the CalculatorBean has finished processing a certain service request message identified by the message's "sent" time stamp.

     

    • check.jsp: RecordManager&12434;&20351;&29992;&12375;&12390;CalculatorBean&12398;&20966;&29702;&12364;&32066;&20102;&12377;&12427;&12398;&12434;&24453;&12388;&12463;&12521;&12452;&12450;&12531;&12488;JSP&12290;&24453;&27231;&12377;&12427;&12469;&12540;&12499;&12473;&35201;&27714;&12513;&12483;&12475;&12540;&12472;&12399;&12289;&12513;&12483;&12475;&12540;&12472;&12398;&12479;&12452;&12512;&12473;&12479;&12531;&12503;"sent"&12391;&35672;&21029;&12373;&12428;&12427;&12290;

     

    &12414;&12392;&12417;&65288;Summary&65289;

     

    In this trail, I have shown you how to use the dependency injection annotations to inject service objects into your application. The dependency injection design pattern could help you simplify your code and improve your application architecture. That concludes our trails on EJB 3.0 service object management.

     

    &12371;&12398;&12488;&12524;&12452;&12523;&12391;&12399;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12395;&12469;&12540;&12499;&12473;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&27880;&20837;&12377;&12427;&20381;&23384;&24615;&27880;&20837;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12398;&20351;&12356;&26041;&12395;&12388;&12356;&12390;&12362;&25945;&12360;&12375;&12414;&12375;&12383;&12290;&20381;&23384;&24615;&27880;&20837;&12398;&12487;&12470;&12452;&12531;&12497;&12479;&12540;&12531;&12399;&12289;&12467;&12540;&12489;&12434;&21336;&32020;&12395;&12375;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;&27083;&36896;&12434;&25913;&21892;&12375;&12414;&12377;&12290;&12371;&12428;&12391;EJB 3.0&12469;&12540;&12499;&12473;&12458;&12502;&12472;&12455;&12463;&12488;&31649;&29702;&12398;&12488;&12524;&12452;&12523;&12399;&32066;&20102;&12391;&12377;&12290;

     

    The RecordManagerBean session bean in this trail uses an injected DataSource and JDBC to save and retrieve data from a relational database. But a much better way to manage database from a Java application is to use data POJOs. In the next hike, you go through EJB 3.0 entity beans to learn how POJO data persistence and data access work.

     

    &12371;&12398;&12488;&12524;&12452;&12523;&12398;RecordManagerBean&12475;&12483;&12471;&12519;&12531;Bean&12399;&12289;&27880;&20837;&12373;&12428;&12383;DataSource&12392;JDBC&12434;&20351;&12356;&12522;&12524;&12540;&12471;&12519;&12490;&12523;&12487;&12540;&12479;&12505;&12540;&12473;&12363;&12425;&12487;&12540;&12479;&12434;&21462;&24471;&12539;&20445;&23384;&12375;&12390;&12356;&12414;&12377;&12290;&12375;&12363;&12375;Java&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12487;&12540;&12479;&12505;&12540;&12473;&12434;&31649;&29702;&12377;&12427;&12373;&12425;&12395;&33391;&12356;&26041;&27861;&12399;&12289;&12487;&12540;&12479;POJO&12434;&20351;&12358;&12371;&12392;&12391;&12377;&12290;&27425;&12398;&12495;&12452;&12461;&12531;&12464;&12391;&12399;&12289;EJB 3.0&12398;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12395;&12388;&12356;&12390;&23398;&32722;&12375;&12289;POJO&12487;&12540;&12479;&12398;&27704;&32154;&24615;&12392;&12487;&12540;&12479;&12450;&12463;&12475;&12473;&12395;&12388;&12356;&12390;&23398;&12403;&12414;&12377;&12290;

     

    (c) 2005 JBoss, Inc. All Rights Reserved