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:
1
2
3
4
5
|
public class MyServlet extends HttpServlet {
@Resource(name="java:comp/DefaultDataSource")
private javax.sql.DataSource dsc;
...
}
|
JNDI Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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;
@ApplicationScoped
public 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
1
2
3
4
5
6
7
8
9
10
11
|
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.model
|
Using domain.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<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