Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 240197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:38:48+00:00 2026-05-11T20:38:48+00:00

I’m working on my first java struts2 webapp and want to be able to

  • 0

I’m working on my first java struts2 webapp and want to be able to inject bean parameters into any arbitrary class that is called. But I find that I can only do this with struts action classes…

Let’s say I have this bean in my applicationContext.xml file:

<bean id="BeanTest" class="BeanTest">
    <property name="test" value="someval" />
</bean>

If I have a struts action class setup called BeanTest (like so), and I add a setter (public void setTest()), then the test parameter will be set and I can access it.

import com.opensymphony.xwork2.ActionSupport;

public class BeanTest extends ActionSupport{
    private String test;

    public String execute(){
        String str = getTest(); // returns "someval"
        return "success";
    }

    public void setTest(String test){
        this.test = test;
    }

    public String getTest(){
        return test;
    }
}

However, let’s say I change the bean to BeanTest2 like so:

<bean id="BeanTest2" class="BeanTest2">
    <property name="test" value="someval" />
</bean>

And I have a standalone class like so:

public class BeanTest2{
    private test;

    public void setTest(String test){
        this.test = test;
    }

    public String getTest(){
        return test;
    }
}

If I create an instance of BeanTest2 in BeanTest, and call getTest, it always returns null.

import com.opensymphony.xwork2.ActionSupport;

public class BeanTest extends ActionSupport{
    public String execute(){

        BeanTest2 bt = new BeanTest2();
        String str = bt.getTest(); //returns null, but I want "someval"

        return "success";
    }
}

What I want to do is set up a bean in applicationContext so that I can point it to an arbitrary class and that class will always get whatever bean parameters I set (assuming I’ve created setters for them). Unfortunately, what happens is that only struts action classes are able to get these bean properties. Everything is not getting set.

Is this question clear? I feel like I’m missing something obvious about the way beans work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:38:48+00:00Added an answer on May 11, 2026 at 8:38 pm

    I think Spring will normally do dependency injection only for those classes that are created by Spring, not for the ones that you create yourself with new operator.

    BeanTest is created by Spring so it will get its dependencies injected, but BeanTest2 is not created by Spring, thus Spring knows nothing about BeanTest2 instances.

    You can add BeanTest2 as a field in BeanTest

    public class BeanTest {
       private BeanTest2 beanTest2;
       public void setBeanTest2(BeanTest2 b) { this.beanTest2 = b; }
       public BeanTest2 getBeanTest2() { return this.beanTest2; };
    }
    

    Then you can inject beanTest2 to beanTest instance.

    <bean id="beanTest2" class="BeanTest2">
        <property name="test" value="someval" />
    </bean>
    
    <bean id="beanTest" class="BeanTest">
        <property name="beanTest2" ref="beanTest2" />
    </bean>
    

    This way beanTest2 should be injected into BeanTest instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The way I would approach this would be to make… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer Use the default for now, with some experience with the… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer Register your handler with signal.signal like this: #!/usr/bin/env python import… May 11, 2026 at 11:55 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.