jsf - Interceptor Method Not Invoked -


the container glassfish. have implemented @postconstruct life-cycle event interceptor method in simple dao bean class, seems reason not intercepting business method @ all. not anywhere manually instantiate bean classes. beans.xml discovery mode , since don't annotate defaultuserdao bean, gets default scope.

public class defaultuserdao implements userdao {      private string username;     private string password;     scanner users = null;     string[] usernamepasswordpairs = null;      public defaultuserdao() {         try {             users = new scanner(paths.get("/home/nhuyvan1106/netbeansprojects/ejbinaction/web/users"));         }           catch (ioexception ex) {             logger.getlogger(defaultuserdao.class.getname()).log(level.severe, null, ex);         }     }     /*the interceptor method defined right below,        interceptor method not called @ all. if insert       system.out.println(usernamepasswordpairs) after split()       method below, prints [username:password] pair twice , there       1 line in text file pair read  in       form admin:admin. notice insert system.out.println()       method in interceptor method if remove print()       method init() method, don't see prints       */     @postconstruct     public void init() {         while (users.hasnextline()) {             string line = users.nextline();             usernamepasswordpairs = line.split(":");             //if uncomment this, see prints [admin:admin] pair             //twice, when comment out, don't see prints             //system.out.println(arrays.tostring(usernamepasswordpairs));             username = usernamepasswordpairs[0];             password = usernamepasswordpairs[1];         }     }      @aroundconstruct     private void printuser(invocationcontext ic) {         //if interceptor invoked, should print @ least "interceptor invoked: "         //but not print this.         system.out.println("interceptor invoked: " + arrays.tostring(usernamepasswordpairs));         try {             ic.proceed();         }           catch (exception ex) {             ex.printstacktrace();         }     } } 

a simple jsf page gathering username , password

@named @requestscoped public class loginbean {      private string username;     private string password;      @inject     private userdao userdao;      public loginbean() {     }      public loginbean(string username, string password) {         this.username = username;         this.password = password;     }      public string validateuser() {          if (userdao.getusername().equals(username) && userdao.getpassword().equals(password)) {         }          else {             username = "error";             password = "error";         }         return "confirmation.xhtml";     }      //getter , setter username , password  } 

first, note @postcontruct lifecycle callback - , not interceptor - while @aroundconstruct interceptor method. have different meanings , declared differently.

also note invocation order typically following:

  1. the constructor interceptor called
  2. the constructor called through proceed() method in interceptor.
  3. injection performed (basically @inject fields/setters set)
  4. the @postconstruct lifecycle callback called.

regarding @postconstruct lifecycle callback, declare correctly. , seems called since prints text.


regarding interceptor, following missing:

  • your interceptor must declared in separate class, not in bean.
  • your interceptor class must declared in meta-inf/beans.xml file under <interceptors> element or must annotated javax.annotations.priority annotation specifying priority value. later option makes interceptor application-wide, basically: apply whole app instead of jar containing meta-inf/beans.xml. start, recommend declare in meta-inf/beans.xml.
  • your interceptor must annotated @interceptor (javax.interceptor.interceptor)
  • you must declare interceptor binding on interceptor , declare beans/methods want intercept it.

to summarize, have create interceptor binding interceptor:

@interceptorbinding @retention(retentionpolicy.runtime) public @interface greet {  } 

then have create class interceptor, using interceptor binding:

@greet @interceptor public class greetinterceptor {      @aroundconstruct     public void aroundconstruct(invocationcontext ic) throws exception {         system.out.println("hello!!");         ic.proceed();     } } 

then declare in meta-inf/beans.xml:

<interceptors>     <class>com.sandbox.greetinterceptor</class> </interceptors> 

finally, annotate bean want intercept calls interceptor binding:

@greet public class defaultuserdao implements userdao {     ... } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -