From the course: Java EE: Contexts and Dependency Injection

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Intercepting constructors

Intercepting constructors

From the course: Java EE: Contexts and Dependency Injection

Start my 1-month free trial

Intercepting constructors

- [Instructor] As well as intercepting methods, I can also intercept constructors. But, for this I need to implement another interceptor method that encapsulates this logic. I also have a different annotation to use. The annotation I use is called AroundConstruct. I start with AroundConstruct and make this method private as before. It's going to return an object. I'm going to give this method a name, so I'm going to just call it something sensible like doClassLogging. This method also accepts an InvocationContext, something like this, which also throws an exception. So, what am I going to log out for my constructor? Well, something that is very useful to log out is to log how long it took to execute the method. So, I'm going to start by creating a variable that contains the current time in milliseconds. I'm then going to proceed to provoke the constructor by calling the proceed method like so. I'm also now, once the method has been executed, I want to know what the end time is. So…

Contents