@Resource注入
2009-08-24 02:53 Jvpy 阅读(264) 评论(0) 编辑 收藏 举报1 package cn.jvpy.ejb3;
2
3 public interface HelloResource {
4 public String sayHello();
5
6 public Integer sayNumber();
7 }
2
3 public interface HelloResource {
4 public String sayHello();
5
6 public Integer sayNumber();
7 }
HelloResourceBean.java
1 package cn.jvpy.ejb3.impl;
2
3 import javax.annotation.Resource;
4 import javax.ejb.Remote;
5 import javax.ejb.Stateless;
6
7 import cn.jvpy.ejb3.HelloResource;
8
9 @Stateless
10 @Remote(HelloResource.class)
11 public class HelloResourceBean implements HelloResource {
12
13 @Resource(name = "SayHelloHello")
14 private String hello;
15
16 @Resource(name = "integer_value")
17 private Integer number;
18
19 public String sayHello() {
20 return hello;
21 }
22
23 public Integer sayNumber() {
24 return number;
25 }
26 }
27
1 package cn.jvpy.ejb3.impl;
2
3 import javax.annotation.Resource;
4 import javax.ejb.Remote;
5 import javax.ejb.Stateless;
6
7 import cn.jvpy.ejb3.HelloResource;
8
9 @Stateless
10 @Remote(HelloResource.class)
11 public class HelloResourceBean implements HelloResource {
12
13 @Resource(name = "SayHelloHello")
14 private String hello;
15
16 @Resource(name = "integer_value")
17 private Integer number;
18
19 public String sayHello() {
20 return hello;
21 }
22
23 public Integer sayNumber() {
24 return number;
25 }
26 }
27
EJBTest.java
1 package cn.jvpy.test;
2
3 import javax.naming.InitialContext;
4 import javax.naming.NamingException;
5
6 import cn.jvpy.ejb3.HelloResource;
7
8 public class EJBTest {
9 public static void main(String[] args) {
10 try {
11 InitialContext ctx = new InitialContext();
12 HelloResource resource = (HelloResource) ctx.lookup("HelloResourceBean/remote");
13 System.out.println(resource.sayHello());
14 System.out.println(resource.sayNumber());
15 } catch (NamingException e) {
16 e.printStackTrace();
17 }
18 }
19
1 package cn.jvpy.test;
2
3 import javax.naming.InitialContext;
4 import javax.naming.NamingException;
5
6 import cn.jvpy.ejb3.HelloResource;
7
8 public class EJBTest {
9 public static void main(String[] args) {
10 try {
11 InitialContext ctx = new InitialContext();
12 HelloResource resource = (HelloResource) ctx.lookup("HelloResourceBean/remote");
13 System.out.println(resource.sayHello());
14 System.out.println(resource.sayNumber());
15 } catch (NamingException e) {
16 e.printStackTrace();
17 }
18 }
19
ejb-jar.xml
1 <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
4 version="3.0">
5 <enterprise-beans>
6 <session>
7 <ejb-name>HelloResourceBean</ejb-name>
8 <remote>cn.jvpy.ejb3.HelloResource</remote>
9 <ejb-class>cn.jvpy.ejb3.impl.HelloResourceBean</ejb-class>
10 <session-type>Stateless</session-type>
11 <transaction-type>Container</transaction-type>
12 <env-entry>
13 <env-entry-name>SayHelloHello</env-entry-name>
14 <env-entry-type>java.lang.String</env-entry-type>
15 <env-entry-value>String injected by resource!</env-entry-value>
16 </env-entry>
17 <env-entry>
18 <env-entry-name>integer_value</env-entry-name>
19 <env-entry-type>java.lang.Integer</env-entry-type>
20 <env-entry-value>70000000</env-entry-value>
21 </env-entry>
22 </session>
23 </enterprise-beans>
24 </ejb-jar
1 <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
4 version="3.0">
5 <enterprise-beans>
6 <session>
7 <ejb-name>HelloResourceBean</ejb-name>
8 <remote>cn.jvpy.ejb3.HelloResource</remote>
9 <ejb-class>cn.jvpy.ejb3.impl.HelloResourceBean</ejb-class>
10 <session-type>Stateless</session-type>
11 <transaction-type>Container</transaction-type>
12 <env-entry>
13 <env-entry-name>SayHelloHello</env-entry-name>
14 <env-entry-type>java.lang.String</env-entry-type>
15 <env-entry-value>String injected by resource!</env-entry-value>
16 </env-entry>
17 <env-entry>
18 <env-entry-name>integer_value</env-entry-name>
19 <env-entry-type>java.lang.Integer</env-entry-type>
20 <env-entry-value>70000000</env-entry-value>
21 </env-entry>
22 </session>
23 </enterprise-beans>
24 </ejb-jar
运行结果: