Announcement Announcement Module
Collapse
No announcement yet.
How IOC works? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How IOC works?

    I am very new to Spring. Trying to run first example for IOC using Webwork.
    Here is the details
    I have LoginAction an action and LoginBean class (For testinfg perpose haven't bothered creating Interface)

    Code for LoignAction class

    import com.opensymphony.xwork.ActionSupport;

    public class LoginAction extends ActionSupport {
    private String loginId;
    private String password;
    LoginBean beanObject;

    public LoginAction() {
    System.out.println("LoginAction obj created");
    }
    public String getLoginId() {
    return loginId;
    }
    public void setLoginId(String loginId) {
    System.out.println("in setLoginId : " + loginId);
    this.loginId = loginId;
    }
    public String getPassword() {
    return password;
    }
    public void setPassword(String password) {
    System.out.println("in setPassword : " + password);
    this.password = password;
    }
    public String execute() throws Exception {
    System.out.println("in execute");
    LoginBean bean = getBeanObject();
    if (bean != null) {
    System.out.println("bean obj is not null");
    }
    return SUCCESS;
    }
    public void setBeanObject(LoginBean obj) {
    System.out.println("in setBeanObject");
    beanObject = obj;
    }
    public LoginBean getBeanObject() {
    return beanObject;
    }
    }

    Code for LoginBean

    public class LoginBean {
    public LoginBean() {
    System.out.println("LoginBean obj created");
    }
    }


    Application Context.xml file (stored at /WEB-INF/applicationContext.xml)

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="loginAction" class="LoginAction">
    <property name="beanObject"><ref bean="testLoginBean"/></property>
    </bean>
    <bean id="testLoginBean" class="LoginBean"/>
    </beans>

    Web.xml file

    <?xml version="1.0"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

    <web-app>
    <display-name>My WebWork Application</display-name>
    <servlet>
    <servlet-name>webwork</servlet-name>
    <servlet-class>com.opensymphony.webwork.dispatcher.ServletD ispatcher</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoade rServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>webwork</servlet-name>
    <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    </web-app>

    Now I want to create an instance of Bean class in execute method of action calss.
    But when I run this example before an HTTP request an instance of Action class and an Instance of Bean class got generate in memory and it has also called set method.
    After HTTP request it also has created new instance of action class & in that it did not called set method for bean object.

    Can Some one please help me in this?

    Thanks & Regards
    Abhay

  • #2
    I am using WebWork(2.x) and Spring and as a configuration I have used the listener from Spring, then the listener from XWork-optional and only after that the webwork servlet.

    The order is very important because, first the Spring context has to be initialize and then the xwork-optional listener will use the Spring context inside the xwork configuration. otherwise, no wiring is done and the actions will just be instantiated (like in your case).
    Check out your configuration and the xwork-optional project - you can find more samples inside the AppFuse project for example.

    Comment

    Working...
    X