Obtain four methods for registering beans in spring, especially the third method, which is simple:
I. method 1 (mostly in the Struts Framework) inherits the basedispatchaction
Import com. Mas. wawacommunity. WAP. Service. usermanager;
Public class basedispatchaction extends dispatchaction {
/**
* Web application context variables
*/
Protected webapplicationcontext CTX;
Protected usermanager usermgr;
/**
* Obtain the registered Bean
* @ Param beanname string registers the bean name
* @ Return
*/
Protected final object getbean (string beanname ){
Return CTX. getbean (beanname );
}
Protected actionforward unspecified (actionmapping mapping, actionform form,
Javax. servlet. http. httpservletrequest request,
Javax. servlet. http. httpservletresponse response ){
Return Mapping. findforward ("Index ");
}
Public void setservlet (actionservlet servlet ){
This. servlet = servlet;
This. CTX = webapplicationcontextutils. getwebapplicationcontext (servlet. getservletcontext ());
This. usermgr = (usermanager) getbean ("usermanager ");
}
}
2. Method 2: Implement beanfactoryaware
Be sure to add the following in spring. xml:
<Bean id = "servicelocator" class = "com. Am. OA. commons. Service. servicelocator" Singleton = "true"/>
Beanfactory is automatically set for servicelocator instances so that beanfactory can be directly used later.
Public class servicelocator implements beanfactoryaware {
Private Static beanfactory = NULL;
Private Static servicelocator servlocator = NULL;
Public void setbeanfactory (beanfactory factory) throws beansexception {
This. beanfactory = factory;
}
Public beanfactory getbeanfactory (){
Return beanfactory;
}
Public static servicelocator getinstance (){
If (servlocator = NULL)
Servlocator = (servicelocator) beanfactory. getbean ("servicelocator ");
Return servlocator;
}
/**
* Obtain the corresponding service class based on the provided bean name
* @ Param servname bean name
*/
Public static object getservice (string servname ){
Return beanfactory. getbean (servname );
}
/**
* Obtain the service class corresponding to the specified type based on the provided bean name.
* @ Param servname bean name
* @ Param clazz: bean type returned. If the type does not match, an exception is thrown.
*/
Public static object getservice (string servname, class clazz ){
Return beanfactory. getbean (servname, clazz );
}
}
Action call:
Public class useraction extends baseaction implements action, modeldriven {
Private users user = new users ();
Protected servicelocator service = servicelocator. getinstance ();
Userservice = (userservice) service. getservice ("userservice ");
Public String execute () throws exception {
Return success;
}
Public object GetModel (){
Return user;
}
Public String getalluser (){
Httpservletrequest request = servletactioncontext. getrequest ();
List ls = userservice. loadallobject (users. Class );
Request. setattribute ("user", ls );
This. seturl ("/yonghu. jsp ");
Return success;
}
}
3. Method 3: Implement applicationcontextaware
Be sure to add the following in spring. xml:
<Bean id = "springcontextutil" class = "com. Am. OA. commons. Service. springcontextutil" Singleton = "true"/>
Applicationcontext is automatically set for the springcontextutil instance so that you can directly use applicationcontext later.
Public class springcontextutil implements applicationcontextaware {
Private Static applicationcontext; // spring application context
/**
* Implements the callback method of the applicationcontextaware interface and sets the context.
* @ Param applicationcontext
* @ Throws beansexception
*/
Public void setapplicationcontext (applicationcontext) throws beansexception {
Springcontextutil. applicationcontext = applicationcontext;
}
/**
* @ Return applicationcontext
*/
Public static applicationcontext getapplicationcontext (){
Return applicationcontext;
}
/**
* Get object
* @ Param name
* @ Return object an instance of the bean registered with the given name
* @ Throws beansexception
*/
Public static object getbean (string name) throws beansexception {
Return applicationcontext. getbean (name );
}
/**
* Get the requiredtype object
* If the bean cannot be converted by the type, the corresponding exception will be thrown (beannotofrequiredtypeexception)
* @ Param name bean Registration Name
* @ Param requiredtype the returned object type
* @ Return object return requiredtype object
* @ Throws beansexception
*/
Public static object getbean (string name, class requiredtype) throws beansexception {
Return applicationcontext. getbean (name, requiredtype );
}
/**
* If beanfactory contains a bean definition that matches the given name, true is returned.
* @ Param name
* @ Return Boolean
*/
Public static Boolean containsbean (string name ){
Return applicationcontext. containsbean (name );
}
/**
* Determine whether the bean registered with a given name is a singleton or prototype.
* If the bean definition corresponding to the given name is not found, a nosuchbeandefinitionexception will be thrown)
* @ Param name
* @ Return Boolean
* @ Throws nosuchbeandefinitionexception
*/
Public static Boolean issingleton (string name) throws nosuchbeandefinitionexception {
Return applicationcontext. issingleton (name );
}
/**
* @ Param name
* @ Return class the type of the object to be registered
* @ Throws nosuchbeandefinitionexception
*/
Public static class GetType (string name) throws nosuchbeandefinitionexception {
Return applicationcontext. GetType (name );
}
/**
* If the given bean name has aliases in the bean definition, these aliases are returned.
* @ Param name
* @ Return
* @ Throws nosuchbeandefinitionexception
*/
Public static string [] getaliases (string name) throws nosuchbeandefinitionexception {
Return applicationcontext. getaliases (name );
}
}
Action call:
Package com. anymusic. OA. webwork;
Import java. util. List;
Import java. util. Map;
Import javax. servlet. http. httpservletrequest;
Import com. anymusic. OA. commons. Service. servicelocator;
Import com. anymusic. OA. hibernate. pojo. role;
Import com. anymusic. OA. hibernate. pojo. users;
Import com. anymusic. OA. Spring. iuserservice;
Import com. opensymphony. webwork. servletactioncontext;
Import com. opensymphony. xwork. Action;
Import com. opensymphony. xwork. actioncontext;
Import com. opensymphony. xwork. modeldriven;
Public class useraction extends baseaction implements action, modeldriven {
Private users user = new users ();
// You do not need to load the springcontext. xml file any more, because it is configured in Web. xml and started in the program.
Userservice = (userservice) springcontextutil. getbean ("userservice ");
Public String execute () throws exception {
Return success;
}
Public object GetModel (){
Return user;
}
Public String getalluser (){
Httpservletrequest request = servletactioncontext. getrequest ();
List ls = userservice. loadallobject (users. Class );
Request. setattribute ("user", ls );
This. seturl ("/yonghu. jsp ");
Return success;
}
}
4. Set spring applicationcontext through servlet or listener for later reference.
Note that extends contextloaderlistener and contextloaderservlet are used respectively. Then springcontext can be used to getbean.
Overwrite the previously configured listener or servlet in Web. xml.
Public class springcontext {
Private Static applicationcontext; // spring application context
*/
Public void setapplicationcontext (applicationcontext) throws beansexception {
Springcontextutil. applicationcontext = applicationcontext;
}
/**
* @ Return applicationcontext
*/
Public static applicationcontext getapplicationcontext (){
Return applicationcontext;
}
*/
Public static object getbean (string name) throws beansexception {
Return applicationcontext. getbean (name );
}
}
Public class springcontextloaderlistener extends contextloaderlistener {//
Public void contextinitialized (servletcontextevent event ){
Super. contextinitialized (event );
Springcontext. setapplicationcontext (
Webapplicationcontextutils. getwebapplicationcontext (event. getservletcontext ())
);
}
}
Public class springcontextloaderservlet extends contextloaderservlet {
Private contextloader;
Public void Init () throws servletexception {
This. contextloader = createcontextloader ();
Springcontext. setapplicationcontext (this. contextloader. initwebapplicationcontext (getservletcontext ()));
}
}