CDI
CDI predefined beans, resource injection and JNDI lookups in Jakarta EE.
1 min read
References
Predefined Beans
| Predefined Bean | Resource or CDI Bean | Injection Example |
|---|---|---|
| UserTransaction | Resource | @Resource UserTransaction transaction; |
| Principal | Resource | @Resource Principal principal; |
| Validator | Resource | @Resource Validator validator; |
| ValidatorFactory | Resource | @Resource ValidatorFactory factory; |
| HttpServletRequest | CDI bean | @Inject HttpServletRequest req; |
| HttpSession | CDI bean | @Inject HttpSession session; |
| ServletContext | CDI bean | @Inject ServletContext context; |
Predefined beans are injected with dependent scope and the predefined default qualifier @Default.
Resource Injection
Resource injection enables you to inject any resource available in the JNDI namespace into any container-managed object, such as a servlet, an enterprise bean, or a managed bean. For example, you can use resource injection to inject data sources, connectors, or custom resources available in the JNDI namespace.
For example, the following code injects a data source object that provides connections to the default Java DB database shipped with GlassFish Server:
public class MyServlet extends HttpServlet { @Resource(name="java:comp/DefaultDataSource") private javax.sql.DataSource dsc; ...}JNDI Example
import jakarta.annotation.PostConstruct;import jakarta.annotation.Resource;import jakarta.enterprise.context.ApplicationScoped;
import java.util.HashMap;import java.util.Map;import java.util.Set;
@ApplicationScopedpublic class JndiLLMConfig extends LLMConfig {
@Resource(lookup = "java:global/llm.api.key") private String apiKey;
@Resource(lookup = "java:global/llm.model") private String model;
private Map<String, String> properties;
@PostConstruct public void init() { properties = new HashMap<>(); properties.put("llm.api.key", apiKey); properties.put("llm.model", model); }
@Override public Set<String> getPropertyKeys() { return properties.keySet(); }
@Override public String getValue(String key) { return properties.get(key); }}Using asadmin CLI
asadmin create-custom-resource \--restype java.lang.String \--factoryclass org.glassfish.resources.custom.factory.PrimitivesAndStringFactory \--property value=YOUR_API_KEY \java:global/llm.api.key
asadmin create-custom-resource \--restype java.lang.String \--factoryclass org.glassfish.resources.custom.factory.PrimitivesAndStringFactory \--property value=gpt-4 \java:global/llm.modelUsing domain.xml
<custom-resource jndi-name="java:global/llm.api.key" res-type="java.lang.String" factory-class="org.glassfish.resources.custom.factory.PrimitivesAndStringFactory"> <property name="value" value="YOUR_API_KEY"/></custom-resource>
<custom-resource jndi-name="java:global/llm.model" res-type="java.lang.String" factory-class="org.glassfish.resources.custom.factory.PrimitivesAndStringFactory"> <property name="value" value="gpt-4"/></custom-resource>Then restart domain
asadmin restart-domain