Saturday, July 25, 2009

EJB3 Interceptor class example on Weblogic server

Annotations Used to Configure Interceptors

Programming the Annotated EJB 3.0 Class

Oracle Weblogic example
public class AccountBean
implements Account

private int balance = 0;

public void deposit(int amount) {
balance += amount;
System.out.println("deposited: "+amount+" balance: "+balance);

public void withdraw(int amount) {
balance -= amount;
System.out.println("withdrew: "+amount+" balance: "+balance);

public int getBalance(){
int bogus;
return balance + 1;

public void sayHelloFromAccountBean() {


public void preDestroy() {
System.out.println("Invoking method: preDestroy()");


Interceptor class
package sessionbean;

import javax.interceptor.AroundInvoke;

import javax.interceptor.InvocationContext;

import javax.ejb.PostActivate;

import javax.ejb.PrePassivate;


* Interceptor class. The interceptor method is annotated with the

* @AroundInvoke annotation.


public class AuditInterceptor {

public AuditInterceptor() {}


public Object audit(InvocationContext ic) throws Exception {

System.out.println("Invoking method: "+ic.getMethod());

return ic.proceed();



public void postActivate(InvocationContext ic) {

System.out.println("Invoking method: "+ic.getMethod());



public void prePassivate(InvocationContext ic) {

System.out.println("Invoking method: "+ic.getMethod());



Inject EJB into servlet
Account remoteAccount;

Invoking method: public java.lang.Object sessionbean.AccountBean_2lqacg_Impl.beaInvoke(java.lang.Object[],int)
deposited: 200 balance: 800

No comments:

Post a Comment