Webwork2.1.5 IOC container method using spring

Source: Internet
Author: User

Webwork2.1.5 use spring's IOC container method:

First, create several classes:
Application. Java, container. Java, springcontainer. Java, defaapplapplicationsetuplistener. Java, componentnotfoundexception. Java
Container. Java is the interface, and springcontainer. Java implements the container. Java interface.
The Code is as follows:

Application:
Package util;

/**
* @ Author Davin
* @ Since 2007-12-23
* @ Version $ revision: 1.1 $
*/
Public class application {
Private container;
Private Static application instance;

Private application (){

}

Public static application getinstance (){
If (instance = NULL)
Instance = new application ();
Return instance;
}

Public container getcontainer (){
Return container;
}
Public void setcontainer (container ){
This. Container = container;
}
}

Container:
Package util;

/**
* @ Author Davin
* @ Since 2007-12-23
* @ Version $ revision: 1.1 $
*/
Public interface container {
Public object getcomponent (Object key) throws componentnotfoundexception;

Public void reload ();

Public void autowirecomponent (Object bean );

Public void close ();
}

 

Springcontainer:

Package util;

Import java. util. locale;

Import javax. servlet. servletcontext;

Import org. springframework. Beans. Factory. config. autowirecapablebeanfactory;
Import org. springframework. Context. applicationcontext;
Import org. springframework. Context. Support. abstractapplicationcontext;
Import org. springframework. Web. Context. Support. webapplicationcontextutils;

/**
* @ Author Davin
* @ Since 2007-12-23
* @ Version $ revision: 1.2 $
*/
Public class springcontainer implements container {
Private Static applicationcontext;

Public springcontainer (servletcontext ){
This. applicationcontext = webapplicationcontextutils. getwebapplicationcontext (servletcontext );
}

Public springcontainer (applicationcontext ){
This. applicationcontext = applicationcontext;
}

/**
* @ Param key
* Component class type or component name
* @ Return @ throws
* Componentnotfoundexception
*/
Public object getcomponent (Object key) throws componentnotfoundexception {
If (applicationcontext = NULL)
Throw new illegalstateexception ("Spring application context has not been set ");
If (Key = NULL)
Throw new componentnotfoundexception ("the component key can not be null ");
If (Key instanceof class ){
String Names [] = applicationcontext. getbeannamesfortype (class) Key );
If (names = NULL)
Throw new componentnotfoundexception ("the container is unable to resolve single instance of" + (class) Key). getname ()
+ ", None instances found ");
If (names. Length = 0 | names. length> 1)
Throw new componentnotfoundexception ("the container is unable to resolve single instance of" + (class) Key). getname ()
+ ", Number of instances found was:" + names. Length );
Key = Names [0];
}
Return applicationcontext. getbean (key. tostring ());
}
Public static object getbean (string beanname ){
Return applicationcontext. getbean (beanname );
}

Public static string getmessage (string messagename ){
String MSG = applicationcontext. getmessage (messagename, null, locale. China );
Return MSG;
}
Public void reload (){
Close ();
(Abstractapplicationcontext) applicationcontext). Refresh ();
}

Public void autowirecomponent (Object bean ){
(Abstractapplicationcontext) applicationcontext). getbeanfactory (). autowirebeanproperties (bean, autowirecapablebeanfactory. autowire_by_name, false );
}

Public void close (){
(Abstractapplicationcontext) applicationcontext). Close ();
}

}

Componentnotfoundexception:

Package util;
/**
* @ Author Davin
* @ Since 2007-12-23
* @ Version $ revision: 1.1 $
*/
Public class componentnotfoundexception extends runtimeexception {

Public componentnotfoundexception (string message ){
Super (Message );
}

Public componentnotfoundexception (string message, throwable cause ){
Super (message, cause );
}
}

Defaultapplicationsetuplistener:
Package listener;

Import javax. servlet. servletcontextevent;
Import javax. servlet. servletcontextlistener;

Import util. Application;
Import util. springcontainer;

/**
* Defaultapplicationsetuplistener that sets up the environment so that
* Xwork and webwork can load data and information from spring. relies on
* Spring's {@ link org. springframework. Web. Context. contextloaderlistener} Having
* Been called first.
*
* @ Author Davin
* @ Since 2007-12-22
* @ Version $ revision: 1.1 $
*/
Public class defaultapplicationsetuplistener implements servletcontextlistener {

Public void contextinitialized (servletcontextevent event ){
Springcontainer Container = new springcontainer (event. getservletcontext ());
Application. getinstance (). setcontainer (container );
}

Public void contextdestroyed (servletcontextevent arg0 ){
// Do nothing
}

}

After writing the code, you need to modify the configuration file.
First, add web. xml:
<Listener>
<Listener-class>
Listener. defaultapplicationsetuplistener
</Listener-class>
</Listener>
It must be followed by a spring listener.
Followed by modifying webwork-default.xml
<Interceptor name = "component-autowire" class = "Interceptor. componentautowireinterceptor"/>
Add the listener to the default listening range.

After the preceding configuration, you can write a simple service class.
Package Service. impl;

Import java. util. List;

Import service. loginservice;
Import Dao. abstractservice;
Import domain. admin;

Public class loginserviceimpl extends actservice implements loginservice {
Public Admin findadmin (string username ){
List <admin> List = This. gethibernatetemplate (). Find ("from Admin A where a. Username = '" + username + "'");
If (list. Size ()> 0)
Return list. Get (0 );
Else
Return NULL;
}
}
In the spring configuration file:
<Bean id = "loginservice" parent = "basetxproxy" Singleton = "false">
<Property name = "target">
<Bean class = "service. impl. loginserviceimpl" autowire = "byname"/>
</Property>
</Bean>

Write an action class:
Package action;

 

Import service. loginservice;
Import com. opensymphony. xwork. actionsupport;

Public class baseaction extends actionsupport {

Private loginservice;

Public loginservice getloginservice (){
Return loginservice;
}

Public void setloginservice (loginservice ){
This. loginservice = loginservice;
}

Public String execute () throws exception {
Return success;
}
}

Xwork. xml file
<Action name = "Index" class = "Action. baseaction">
<Result name = "success" type = "freemarker">/login. FTL </result>
</Action>
In this way, you can use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.