Announcement Announcement Module
No announcement yet.
Setting properties after injection Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting properties after injection

    I have read most of the stuff about instantiating beans, injected references and I don't really understand it. So could someone help me.

    I have a class which i need to inject one value from the context and then set another value in the program. The documentation keeps saying "described shortly", but you (seem) never get to the bit which tells you.
    So if I create a bean in the xml and inject it into the call its the same instance of that bean which gets put everywhere.
    If I 'new' an instance of that bean then nothing is injected.
    My bean is inside a serverlet so not sure which FactoryBean to use.

    Have I got this right:
    I must create a bean (beanF) which has a ref to the thing I want to inject.
    I than have to call beanF.myFunction on it passing in my specific parameters and beanF will create an instance that contains all the things I want.

    I've read so much over the last couple of days that I think I am going blind! The docs talk about factory-method attributes of beans but this dosen't really factor into my statement above.

    Can some one get me a hand?

  • #2
    Sorry but you totally lost me in your babble... Can you clearly specify what you want to achieve?!


    • #3
      Sorry about that just getting a bit frustrated!

      I would like to dynamically create several instances of an object which have a bean reference in them.


      • #4
        So then mark the bean you want to create as a prototype... each time you request the bean a new instance is created.


        • #5
          Ok, I understand the prototype bit now, but on what do I call getBean()?
          Am I supposed to create a factory bean and inject org.springframework.beans.factory.ObjectFactory to call getBean on?


          • #6
            No... Simply get a reference to the ApplicationContext (you can have your requesting bean implement ApplicationContextAware) and call getBean on that. Simply define your bean as a prototype.


            • #7
              It sounds like you pass runtime parameters during the creation process. This has been a talked about feature for a long time. In Spring 2.5 it was implemented. Basically you can pass arguments to your "getBean" call. However for it to work you have to create a prototype, and it either uses a factory method or takes constructor arguments.

              Here is the bean definition. Note that its a singleton.
              <bean id="myBean" scope="singleton" class="com.mycompany.MyClass">
                <property name="arg3" value="Hello"/>
              Here is the implementation. Note how it has two constructors.
              package com.mycompany
              public class MyClass implements IntializingBean {
                public MyClass(Integer arg1, String arg2) {
                  this.arg1 = arg1;
                  this.arg2 = arg2;
                public MyClass() {
                  arg1 = null;
                  arg2 = null;
                public void setArg3(String arg3) {
                  this.arg3 = arg3;
                public void afterPropertiesSet() {
                  if (arg3 == null) {
                    throw new IllegalStateException("arg3 is required");
                  if (arg1 == null || arg2 == null) {
                    arg4 = "Only arg3 is set:" + arg3"
                  } else {
                    arg4 = "arg1 = " + arg1 + ", arg2 = " + arg2 + ", arg3=" + arg3;
              Here are a few examples of the bean being instantiated:

              MyClass obj1 = ctx.getBean("myBean");  // arg4 = "Only arg3 is set: Hello"
              MyClass obj2 = ctx.getBean("myBean", new Object[] {new Integer(1), "cool"}); // arg4 = "arg1 = 1, arg2 = cool, arg3 = Hello"
              Does this look like what you were looking for?


              • #8
                I have a class which i need to inject one value from the context and then set another value in the program
                define your bean in the context with the property you want.
                that bean will be instantiated by the framework with the property you want set on it. then simply access that bean in your servlet and set the other value.


                    <bean name="/something.htm" class="web.TestController">
                        <property name="testBean" ref="testBean"/>
                    <bean id="testBean" class="domain.TestBean">
                        <property name="injectedValue" value="injVal"/>

                package web;
                public class TestController implements Controller {
                    private TestBean testBean;
                    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                            throws ServletException, IOException {
                        Map myModel = new HashMap();
                        myModel.put("injected", this.testBean.getInjectedValue());
                        myModel.put("manual", this.testBean.getManualValue());
                        return new ModelAndView("forwardJsp", "model", myModel);
                    public void setTestBean(TestBean testBean) {
                        this.testBean = testBean;
                package domain;
                public Class TestBean implements Serializable {
                    private String injectedValue;
                    private String manualValue;
                    public void setInjectedValue(String injectedValue) {
                        this.injectedValue= injectedValue;
                    public String getInjectedValue() {
                        return injectedValue;
                    public void setManualValue(String manualValue) {
                        this.manualValue= manualValue;
                    public String getManualValue() {
                        return manualValue;
                dunno if this is what you're looking for ...


                • #9
                  Thanks, that was what I was getting at.
                  Your examples were very helpful.