Java 如何将参数动态传递给SpringBean
我是春天的新手 这是bean注册的代码:Java 如何将参数动态传递给SpringBean,java,spring,javabeans,Java,Spring,Javabeans,我是春天的新手 这是bean注册的代码: <bean id="user" class="User_Imple"> </bean> <bean id="userdeff" class="User"> </bean> 这是我执行操作的主要方法: public static void main(String arg[]) { ApplicationContext context = new ClassPathXmlApplicationCon
<bean id="user" class="User_Imple"> </bean>
<bean id="userdeff" class="User"> </bean>
这是我执行操作的主要方法:
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
}
现在我想用参数调用这个构造函数,这些参数是在我的主方法中动态生成的。这就是我想要动态传递的意思,而不是静态传递,就像我在bean.config
文件中声明的那样。请看一看
另外,请查看和查找springbean的其他生命周期拦截。也许让
用户成为普通Pojo(而不是springbean)可以解决您的问题
<!-- Only use User as a Spring Bean -->
<bean id="userdeff" class="User"></bean>
构造函数注入可以帮助您。在这种情况下,您可能需要生成一个具有ID和user属性的POJO,并将POJO传递给构造函数。在配置文件中的构造函数注入中,您可以引用pojo作为引用的构造函数。因此,您将在ID和User中处理数据的动态值
希望这有帮助 如果我理解正确,那么正确的答案是使用getBean(stringbeanname,Object…args)
方法,该方法将参数传递给bean。我可以向您展示如何对基于Java的配置执行此操作,但您必须了解如何对基于XML的配置执行此操作
@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");
这就是你要找的吗
更新
这个答案获得了太多的选票,没有人看我的评论。尽管这是问题的解决方案,但它被视为弹簧反模式,您不应该使用它!使用工厂、查找方法等有几种不同的方法来正确处理事情
请使用以下SO帖子作为参考:
我认为上面提出的使用构造函数注入/setter注入的答案对于您正在寻找的用例来说并不完美。
Spring或多或少地接受构造函数/设置器的静态参数值。我看不到动态传递值以从Spring容器获取Bean的方法。
但是,如果您想动态获取User_Imple的实例,我建议使用工厂类User_Imple_工厂
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);
}
}
是的,这很好@matsev,如果我的类较少,我们直接使用POJO,但在MVC中,我们完全隐藏了业务文件。。然后我们看一下bean,然后你需要一个工厂类,你可以从Spring中引用它。Spring只使用(或多或少)SingletonsCan,请引用参考资料了解为什么setter
注入优于构造函数“Spring团队通常提倡构造函数注入,因为它使人们能够将应用程序组件实现为不可变的对象,并确保所需的依赖项不为null……Setter注入应该主要用于可选的依赖项,这些依赖项可以在类内分配合理的默认值”-我认为动态地“用户意味着他希望在主方法中传递参数,或者通常在获取bean时传递参数。调用getBean(…)并直接传递参数是可行的,但是将此调用移动到定位器(服务定位器模式)可以这样做,以避免代码中对Spring框架的依赖。定位器可以作为bean传递,ApplicationContextAware定位器可以调用getBean(bean,arg1,arg2…)。bean范围应该是“原型”“。另请参阅:@Chris如何从applicationContext.xml中删除bean标记中的硬编码值,就像在Singleton的情况下一样-bean会被急切地初始化,并请使用原型范围进行解释。请注意,使用“DI”时,这不是最佳解决方案,因为实际上我们这里有一种“服务定位器”(与DI相反)。通常,当您遇到过多的context.getBean时,这意味着某些事情是以错误的方式完成的。请实现ApplicationContextAware以避免创建可能在其他地方创建的新ApplicationContext。
@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");
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);
}
}