2. Using the Tutorial Examples 3. Getting Started with Web Applications 5. JavaServer Pages Technology 7. JavaServer Pages Standard Tag Library 10. JavaServer Faces Technology 11. Using JavaServer Faces Technology in JSP Pages 12. Developing with JavaServer Faces Technology 13. Creating Custom UI Components 14. Configuring JavaServer Faces Applications 15. Internationalizing and Localizing Web Applications 16. Building Web Services with JAX-WS 17. Binding between XML Schema and Java Classes 19. SOAP with Attachments API for Java 21. Getting Started with Enterprise Beans 23. A Message-Driven Bean Example 24. Introduction to the Java Persistence API 25. Persistence in the Web Tier 26. Persistence in the EJB Tier 27. The Java Persistence Query Language 28. Introduction to Security in the Java EE Platform 29. Securing Java EE Applications 31. The Java Message Service API 32. Java EE Examples Using the JMS API DataSource Objects and Connection Pools Theconfirmer Example Application Running theconfirmer Example Application Building, Packaging, and Deployingconfirmer in NetBeans IDE Building, Packaging, and Deployingconfirmer Using Ant Running theconfirmer Client in NetBeans IDE Running theconfirmer Client Using Ant Further Information about Resources 36. The Coffee Break Application | Resource InjectionThejavax.annotation.Resource annotation is used to declare a reference to a resource.@Resourcecan decorate a class, a field, or a method. The container will injectthe resource referred to by@Resource into the component either at runtime or whenthe component is initialized, depending on whether field/method injection or class injection isused. With field and method-based injection, the container will inject the resource whenthe application is initialized. For class-based injection, the resource is looked up bythe application at runtime. @Resource has the following elements:
Thename element is the JNDI name of the resource, and is optionalfor field- and method-based injection. For field-based injection, the defaultname is thefield name qualified by the class name. For method-based injection, the defaultnameis the JavaBeans property name based on the method qualified by the classname. Thename element must be specified for class-based injection. The type of resource is determined by one of the following:
For class-based injection, thetype element is required. TheauthenticationType element is used only for connection factory resources, and can beset to one of thejavax.annotation.Resource.AuthenticationType enumerated type values:CONTAINER, the default, andAPPLICATION. Theshareable element is used only for ORB instance resources or connection factoryresource. It indicates whether the resource can be shared between this component andother components, and may be set totrue, the default, orfalse. ThemappedName element is a non-portable, implementation-specific name that the resource should bemapped to. Because thename element, when specified or defaulted, is local onlyto the application, many Java EE servers provide a way of referring toresources across the application server. This is done by setting themappedName element.Use of themappedName element is non-portable across Java EE server implementations. Thedescription element is the description of the resource, typically in the defaultlanguage of the system on which the application is deployed. It is usedto help identify resources, and to help application developers choose the correct resource. Field-Based InjectionTo use field-based resource injection, declare a field and decorate it with the@Resource annotation. The container will infer the name and type of the resourceif thename andtype elements are not specified. If you dospecify the type element, it must match the field’s type declaration. package com.example;public class SomeClass { @Resource private javax.sql.DataSource myDB;...}In the code above, the container infers thename of the resource basedon the class name and the field name:com.example.SomeClass/myDB. The inferredtypeisjavax.sql.DataSource.class. package com.example;public class SomeClass { @Resource(name="customerDB") private javax.sql.DataSource myDB;...}In the code above, the JNDI name iscustomerDB, and the inferredtypeisjavax.sql.DataSource.class. Method-Based InjectionTo use method-based injection, declare a setter method and decorate it with the@Resource annotation. The container will infer the name and type of the resourceif thename andtype elements are not specified. The setter methodmust follow the JavaBeans conventions for property names: the method name must beginwithset, have avoid return type, and only one parameter. If youdo specify thetype element, it must match the field’s type declaration. package com.example;public class SomeClass { private javax.sql.DataSource myDB;... @Resource private void setMyDB(javax.sql.DataSource ds) { myDB = ds; }...}In the code above, the container infers thename of the resource basedon the class name and the field name:com.example.SomeClass/myDB. The inferredtypeisjavax.sql.DataSource.class. package com.example;public class SomeClass { private javax.sql.DataSource myDB;... @Resource(name="customerDB") private void setMyDB(javax.sql.DataSource ds) { myDB = ds; }...}In the code above, the JNDI name iscustomerDB, and the inferredtypeisjavax.sql.DataSource.class. Class-Based InjectionTo use class-based injection, decorate the class with a@Resource annotation, andset the requiredname andtype elements. @Resource(name="myMessageQueue", type="javax.jms.ConnectionFactory")public class SomeMessageBean {...}Declaring Multiple ResourcesThe@Resources annotation is used to group together multiple@Resource declarations for class-basedinjection. @Resources({ @Resource(name="myMessageQueue", type="javax.jms.ConnectionFactory"), @Resource(name="myMailSession", type="javax.mail.Session")})public class SomeMessageBean {...}The code above shows the@Resources annotation containing two@Resource declarations. One isa JMS message queue, and the other is a JavaMail session. Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |