Yes, you can use descriptor alone to inject into a field/property. That
would save you the code doing lookup.
What's your sun-ejb-jar.xml like? There normally should be a
<resource-ref> element to map the local name (jdbc/testIP) to its global
jndi name (the one you created in appserver). If the global jndi name
is the same as resource reference name (jdbc/testIP), then you may skip
this mapping.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application
Server 9.0 EJB 3.0//EN"
"
http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
<sun-ejb-jar>
<enterprise-beans>
<unique-id>0</unique-id>
<ejb>
<ejb-name>xxx</ejb-name>
<jndi-name>xxx</jndi-name>
<resource-ref>
<res-ref-name>xxx</res-ref-name>
<jndi-name>xxx</jndi-name>
</resource-ref>
Cheng
AKostylev wrote:
>But as I understand I can use injection without using annotation,
>defining it in ejb-jar.xml.
>Here is the sample:
>
>@Stateless
>public class FacadeBean implements Facade
>{
> private DataSource ds;
> public void testDataSource()
> {
> try
> {
> ds.getConnection();
> }
> catch (SQLException e)
> {
> throw new EJBException(e);
> }
> }
>...
>}
>
><?xml version="1.0" encoding="UTF-8"?>
><ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
>http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
> <description>J2EE5 test application </description>
> <display-name>Facade</display-name>
> <enterprise-beans>
> <session>
> <ejb-name>Facade</ejb-name>
> <remote>ru.amfitel.test.bean.Facade</remote>
> <ejb-class>ru.amfitel.test.bean.FacadeBean</ejb-class>
> <session-type>Stateless</session-type>
> <resource-ref>
> <res-ref-name>jdbc/testIP</res-ref-name>
> <res-type>javax.sql.DataSource</res-type>
> <injection-target>
> <injection-target-class>ru.amfitel.test.bean.FacadeBean</injection-target-class>
> <injection-target-name>ds</injection-target-name>
> </injection-target>
> </resource-ref>
> </session>
> </enterprise-beans>
></ejb-jar>
>
>Should this work due to EJB 3.0 specs?
>At this time I get a NullPointerException (ds is not initialized):
>Caused by: java.lang.NullPointerException
> at ru.amfitel.test.bean.FacadeBean.testDataSource(FacadeBean.java:90)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
>java:39)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
>sorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:585)
> at com.sun.enterprise.security.application.EJBSecurityManager.runMethod(
>EJBSecurityManager.java:1050)
> at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:165
>)
> at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseConta
>iner.java:2788)
> at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:387
>0)
> at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInv
>ocationHandler.java:190)
>
>What is wrong?
>Thank you.
>
>
>
>
>
>>If you need to declare a resource-ref in ejb-jar.xml, then annotation in
>>java code doesn't simplify anything. In that case, I would suggest you
>>not use @Resource in bean class.
>>
>>
>
>
>
>>To use <resource-ref> in ejb-jar.xml, it's the same as in previous
>>versions of ejb. Your FacadeBean will need to lookup the declared
>>resource:
>>
>>
>
>
>
>>DataSource ds = (DataSource) sessionContext.lookup("jdbc/my-ds");
>>
>>
>
>
>
>><?xml version="1.0" encoding="UTF-8"?>
>><ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
>>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0"
>>xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
>>http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
>><enterprise-beans>
>> <session>
>> <ejb-name>FacadeBean</ejb-name>
>> <business-remote>com.xxx.FacadeRemote</business-remote>
>> <ejb-class>com.xxx.FacadeBean</ejb-class>
>> <session-type>Stateless</session-type>
>> <transaction-type>Container</transaction-type>
>> <resource-ref>
>> <description>description</description>
>> <res-ref-name>jdbc/my-ds</res-ref-name>
>> <res-type>javax.sql.DataSource</res-type>
>> <res-auth>Container</res-auth>
>> <res-sharing-scope>Shareable</res-sharing-scope>
>> </resource-ref>
>> </session>
>></enterprise-beans>
>>
>>
>
>
>
>
>>Cheng
>>
>>
>
>
>