Java – how to dynamically pass parameters to spring beans
•
Java
I'm a novice in spring
This is the bean registration code:
<bean id="user" class="User_Imple"> </bean> <bean id="userdeff" class="User"> </bean>
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.... }
This is my main method of performing operations:
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 that are dynamically generated in my main method This is the dynamic I want to pass - not static, just like in my bean As declared in the config file
Solution
See constructor injection
Also, look at the other lifecycles of the intializingbean and the beanpostprocessor to intercept a spring bean
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码