Why do we need an init() method in servlet? Can't we use the constructor to initialization?
Because Servlet is an interface, not an abstract class. Constructor arguments cannot be specified on an interface, so the ServletContext needs to be specified on a normal method signature.
This allows the application server to know how to initialize any Servlet implementation properly.
Another solution would have been to require, but not enforce at compile time, a constructor taking ServletContext. The application server would then call the constructor via reflection. However, the designers of the Servlet specification did not chose this path.
Related
I want to know whether I can use object of service layer marked with #Service annotation and call one of its method in non mvc-spring class ?
Say there is a method getUsers() in service layer which calls getUsers() of Dao layer. In order to use it in contoller I have to add the #Autowired-annotation in the service layer instance. But if I want to use the class method getUsers() in non-mvc class, how can I do that?
In order to use a service, that object must be container managed. That is, this object's life cycle must be managed by Spring (creation, destruction, initialisation,...).
So to inject an instance of your service in an object, it must be a Spring bean too (Service, Component, Controller...).
So, it may be an MVC object, but it doesn't have to.
On the other hand, there is another alternative: use the annotation #Configurable.
An object with this annotation can be application managed but Spring, using byte code aspects, can inject it's dependencies. So although you create the object with a new statement, Spring instruments this call and resolve all the annotated dependencies.
Read this for more detail:
http://docs.spring.io/spring/docs/3.0.0.M3/spring-framework-reference/html/ch08s08.html
This is related to the question
How do i get servlet instance from web.xml in my java class
Folks were not clear as to what is the usecase for this type of question. I have the same question and wanted to give a usecase.
In my application i have a class called Configuration which extends HttpServlet. In my web.xml i have an load-on-startup servlet defined for Configuration. This class reads all the properties necessary for the application, and it is absolutely necessary that this properties are read during startup because there something which i am doing differently for each instance of my webserver based on the properties. Now i need to get a handle of this instance in my spring controllers so that i can get the values of the properties. How do i do it?
Is there any specific reason why Configuration is a Servlet? If this class's sole purpose is to read properties to be used later and it is not serving any requests by itself, it shouldn't be a Servlet.
There are two ways to do these configuration classes.
One, you can annotate this class as a #Component and make this class to be instantiated by Spring during startup. Then you can inject this bean wherever you need it.
Second way, without using Spring, is to register an ServletContextListener in your web.xml. Create a class implement the ServletContextListener and inside the contextInitialized() method, call the method of the Configuration class where your property loading logic resides.
http://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/
I am new to Spring MVC and being stuck with a problem.
I have to call a method only once after my application gets started. is there is any annotation for that. I don't want to use init config and #PostConstruct because for that I need to create an object of the bean. My method should get called only once as soon as my application gets started.
I am using Spring MVC 3
Thanks
If it's a non-static method, then you need an instance to invoke the method on. If the instance depends on beans in the context, then the appropriate way is to define a bean for it and use #PostConstruct. If the instance doesn't have any dependencies on the ApplicationContext, just create it yourself and call its method.
I guess your context is loading in the start up. You can use this to do that
<bean name="YOUR_BEAN_NAME" init-method="YOUR_METHOD_NAME" class="YOUR_CLASS_NAME" lazy="false" />
How can I unload a servlet from its container using the servlet's constructor or init() method?
If I unload it using the servlet constructor, will the init() method still be called?
Should I throw an exception? If so, checked or runtime?
Just throwing an exception in the servlet's constructor or init() method will prevent it from being taken in the servlet mapping of the servletcontainer.
You can not unload it at a later time when it has already been constructed and initialized successfully. Best what you can do is to just throw an exception in any of the HTTP methods based on some condition.
The right approach however depends on the sole functional requirement. Most probably you do not need a servlet at all. Simply because the desire to unload it manually makes design technically no utter sense.
if we call destroy() on servlet then it doesn't mean that our servlet will be unloaded/destroyed. It simply calls destroy leaving servlet untouched, nothing harm to servlet instance. It is still alive because you called destroy method. It is not container mechanism which is calling destroy method.
when container decides to destroy/unload the servlet instance from memory then container runs destruction mechanism and destroy method is one(along with several steps) of steps of the destruction mechanism. Destruction mechanism give chance to user/developer to clean-up resources which were initialized during the construction/initializing of the instance.
I want to call to a method from an EJB in the same instant in which one deploys itself, without using a servlet.
Thanks.
David.
There seems to be no life-cycle methods defined by the EJB spec for this purpose. Individual vendors may provide extensions to allow this. For example Startup Beans in WebSphere would be a place to put the invocation logic you want.
Using techniques such as a static method seem slightly dangerous in that we don't know whether all dependency injection is complete before that static method fires, and hence whether you can safely use the business methods of the EJB.
Persoanlly, if I needed to be portable I would bite the bullet and use a servlet. It costs very little.
Try doing your initialization within a static block. This will run once when the classloader loads the class.
static { System.out.println("static"); }
The PostConstruct hook is right for that.
Find more info on about PostConstruct here:
in the javadoc
lifecycle of EJBs in the JavaEE 5 tutorial
Let's finish with a quick example:
#Stateless
public class TestEJB implements MyEJBInterface{
#PostConstruct
public void doThatAfterInitialization() {
//put your code here to be executed after creation and initialization of your bean
}
}
Static initializer blocks are technically not illegal in EJB but they are used to execute code before any constructor (which might be a problem) when instantiating a class. They are typically used to initialize static fields which may be illegal in EJB if they are not read only. So, what about using ejbCreate(), setSessionContext() or setEntityContext() methods instead (not even sure this would be appropriate without more details on the problem you are trying to solve)?
The EJB container, for a #Singleton bean, shall create the instance of the bean as soon as the application is deploy if it is annotated #Startup.
That will, of course, fire up static initialization blocks, the constructor, dependency injection setters, #PostConstruct methods etc.
Here is the appropriate referente to the Java EE 6 Tutorial.