How to pass parameters dynamically to Spring beans

自闭症网瘾萝莉.ら 提交于 2019-11-30 12:50:10

问题


I am new to Spring.

This is the code for bean registration:

<bean id="user" class="User_Imple"> </bean>
<bean id="userdeff" class="User"> </bean>

and this is my bean class:

public class User_Imple implements Master_interface {

    private int id;
    private User user; // here user is another class

    public User_Imple() {
        super();
    }

    public User_Imple(int id, User user) {
        super();
        this.id = id;
        this.user = user;
    }

    // some extra functions here....
}

and this is my main method to perform action:

public static void main(String arg[]) {

    ApplicationContext context = new ClassPathXmlApplicationContext("/bean.xml");
    Master_interface master = (Master_interface)context.getBean("user");

    // here is my some operations..
    int id = ...
    User user = ...

    // here is where i want to get a Spring bean
    User_Imple userImpl; //want Spring-managed bean created with above params
}

Now I want to call this constructor with parameters, and these parameters are generated dynamically in my main methods. This is what I mean by I want to pass dynamically – not statically, like declared in my bean.config file.


回答1:


Please have a look at Constructor injection.

Also, Have a look at IntializingBean and BeanPostProcessor for other life cycle interception of a springbean.




回答2:


If i get you right, then the correct answer is to use #getBean(String beanName, Object... args) which will pass arguments to bean. I can show you, how it is done for java-based configuration, but you'll have to find how it is done for xml based configuration.

@Configuration
public class ApplicationConfiguration {

  @Bean
  @Scope("prototype") //As we want to create several beans with different args, right?
  String hello(String name) {
    return "Hello, " + name;
  }
}

//and later in your application

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
String helloCat = (String) context.getBean("hello", "Cat");
String helloDog = (String) context.getBean("hello", "Dog");

Is this what are you looking for?

Upd. This answer gets too much upvotes and nobody looks at my comment. Even though it's a solution to problem, it is considered as spring anti-pattern and you shouldn't use it! There are several different ways to do things right using factory, lookup-method, etc..

Please use the following SO post as a point of reference: create beans at runtime




回答3:


I think the answers proposed above to use constructor injection/setter injection doesn't work perfectly for the use case you are looking for. Spring more or less takes static argument values for constructors/setters. I don't see a way to dynamically pass values to get a Bean from Spring Container. However, if you want to get instances of User_Imple dynamically, I would recommend using a factory class User_Imple_Factory

 
    public class User_Imple_factory {
        private static ApplicationContext context =new ClassPathXmlApplicationContext("/bean.xml");

        public User_Imple createUserImple(int id) {
            User user = context.getBean("User");
            return new User_Imple(id, user);
        }
    }




回答4:


Constructor injection can help you. In this case you may need to generate a POJO with ID and user as its attributes and pass POJO to constructor. In constructor injection in config file you can refer this constructor with pojo as reference. So you will be handle the dynamic value of data in ID and User.

Hope this helps !!




回答5:


Perhaps letting the User_Imple be an ordinary Pojo (instead of a Spring bean) will solve your problem?

<!-- Only use User as a Spring Bean -->
<bean id="userdeff" class="User"></bean>

Java:

public static void main(String arg[])
{
    ApplicationContext context =new ClassPathXmlApplicationContext("/bean.xml");
    User user = context.getBean(User.class);

    int id = // dynamic id
    Master_interface master = new User_Imple(id, user);
}


来源:https://stackoverflow.com/questions/16997034/how-to-pass-parameters-dynamically-to-spring-beans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!