首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

EJB3 学习札记3

2012-11-07 
EJB3 学习笔记31 安装jboss (略)2?创建一个既能本地调用 又可以远程调用的 bean 引入jboss_home/client/下

EJB3 学习笔记3

1 安装jboss (略)

2?创建一个既能本地调用 又可以远程调用的 bean 引入jboss_home/client/下面的jar

public interface HelloWorld {???

}

public interface HelloWorldLocal extends HelloWorld {

}

?

@Stateless???????????????????????????????????????//EJB名称 默认为HelloWorldBean 可以用name="xxx"指定

?????? }

}

?

使用EJB

Properties props = new Properties();

props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");

props.setProperty("java.naming.provider.url","127.0.0.1:1099");

InitialContext ctx = new InitialContext(props );

?

也可以在src下 创建jndi.properties

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=127.0.0.1:1099

?

InitialContext ctx = new InitialContext();


HelloWorld helloworld = (HelloWorld) ctx.lookup("HelloWorldBean/remote");
HelloWorldLocal helloworld = (HelloWorldLocal) ctx.lookup("HelloWorldBean/local");

?

?

3 修改jndi名称:

@RemoteBinding(jndiBinding="xxx/remoteOpp");

ctx.lookup("xxx/remoteOpp");

?

@LocalBinding(jndiBinding="xxx/localOpp");

ctx.lookup("xxx/remoteOpp");

?

RemoteBinding 和?LocalBinding只针对jboss,weblogic10 sunserver glassfish 可以使用 @Stateless(mappedName="xxx/xx")

?

4

无状态bean 使用实例池技术 可以给任何人调用

有状态bean 会钝化(保存到硬盘上)和激活(重新读取)

?

5 会话bean生命周期事件

?@Init 有状态 bean初始化的方法

?@PostConstruct bean实例化后 标注了PostConstruct的方法会立刻执行

?@PreDestroy 在容器销毁 bean之前调用

?@PrePassivate 有状态bean钝化之前调用

?@PostActivate?有状态bean激活之后调用

?@Remove 标注了remove的方法,在调用方法结束后会把bean实例删除

?

6拦截器:

@Interceptors(HelloInterceptor.class)? //@Interceptors({classA,classB,classC})

在bean前使用拦截所有方法

在方法前使用拦截制定方法

public class HelloInterceptor{

?????? public Object log(InvocationContext ctx) throws Exception{

?????????? System.out.println(ctx.getMethod().getName()+"方法被调用");

?????????? return ctx.proceed();

?????? }

}

?

7 bean之间 调用

?? 1使用ctx.lookup()获得bean

???2@EJB(beanName="HelloWorldBean") HelloWorld helloworld;//HelloWorld 由多个实现的EJB时,必须制定beanName

?

8 数据源:

在JBOSS_HOME/docs/examples/jca下找到 对应的数据库的*-ds.xml

修改配置

发不到default/deploy下面(必须是-ds.xml结尾)

<jndi-name>MSSQLDS</jndi-name>

?

9 使用资源(以datasource):

@Resource(mappedName="java:MSSQLDB") DataSource db;

?

10使用timeserver

@Resource private TimerServer timerService;

//millseseconds 指定时间间隔???????????????指定启动时间

timerServer.createTimer(new Date(new Date().getTime()+millseseconds), millseseconds," log....");

?

@Timeout

public void xxx(Timer timer){

? System.out.println(timer.getInfo());

? if(..)

??? timer.cancel();

}

热点排行