Java 弹簧&x27;s法注射
我正在阅读Spring当前版本的文档,对本章的理解有问题。就我所知,我们可以在每次使用bean调用方法时提供重新创建bean的能力。文档提供了以下代码示例:Java 弹簧&x27;s法注射,java,spring,Java,Spring,我正在阅读Spring当前版本的文档,对本章的理解有问题。就我所知,我们可以在每次使用bean调用方法时提供重新创建bean的能力。文档提供了以下代码示例: public class CommandManager implements ApplicationContextAware { //Imports and comments were omitted private ApplicationContext applicationContext; public Ob
public class CommandManager implements ApplicationContextAware {
//Imports and comments were omitted
private ApplicationContext applicationContext;
public Object process(Map commandState) {
Command command = createCommand();
command.setState(commandState);
return command.execute();
}
protected Command createCommand() {
return this.applicationContext.getBean("command", Command.class);
}
public void setApplicationContext(
ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
其中,ApplicationContextAware
界面如下所示:
public interface ApplicationContextAware {
//JavaDocs ommited
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
那么,什么是方法
公共对象进程(Map commandState)
?是回拨吗?如果是这样,将在哪里调用它,谁执行该调用?毕竟,不清楚为什么每次需要时都要重新创建bean。命令和命令管理器
类只是示例类,其中process()
是该示例的一部分。它们与ApplicationContextAware
无关
注意CommandManager
示例中的注释:
// a class that uses a stateful Command-style class to perform some processing
process()
将由应用程序在其他地方调用;为了这个例子,这并不重要。如果您的代码中没有使用完全相同的模型,那么应该忽略此方法,只要在适用的地方调用applicationContext.getBean()
最后,应该将yesCommandManager
注册为bean,以便spring调用setApplicationContext()
编辑
为什么spring知道每次都必须用name命令重新创建bean
考虑到示例的内容,它没有。示例代码调用getBean()
,根据以下内容:
返回指定bean的实例,该实例可以是共享的,也可以是独立的
为了确保始终获得一个新实例,您需要使用prototype
范围
<bean id="beanB class="Command" scope="prototype"/>
Command
和CommandManager
类只是示例类,其中process()
是该示例的一部分。它们与ApplicationContextAware
无关
注意CommandManager
示例中的注释:
// a class that uses a stateful Command-style class to perform some processing
process()
将由应用程序在其他地方调用;为了这个例子,这并不重要。如果您的代码中没有使用完全相同的模型,那么应该忽略此方法,只要在适用的地方调用applicationContext.getBean()
最后,应该将yesCommandManager
注册为bean,以便spring调用setApplicationContext()
编辑
为什么spring知道每次都必须用name命令重新创建bean
考虑到示例的内容,它没有。示例代码调用getBean()
,根据以下内容:
返回指定bean的实例,该实例可以是共享的,也可以是独立的
为了确保始终获得一个新实例,您需要使用prototype
范围
<bean id="beanB class="Command" scope="prototype"/>
Command
和CommandManager
类只是示例类,其中process()
是该示例的一部分。它们与ApplicationContextAware
无关
注意CommandManager
示例中的注释:
// a class that uses a stateful Command-style class to perform some processing
process()
将由应用程序在其他地方调用;为了这个例子,这并不重要。如果您的代码中没有使用完全相同的模型,那么应该忽略此方法,只要在适用的地方调用applicationContext.getBean()
最后,应该将yesCommandManager
注册为bean,以便spring调用setApplicationContext()
编辑
为什么spring知道每次都必须用name命令重新创建bean
考虑到示例的内容,它没有。示例代码调用getBean()
,根据以下内容:
返回指定bean的实例,该实例可以是共享的,也可以是独立的
为了确保始终获得一个新实例,您需要使用prototype
范围
<bean id="beanB class="Command" scope="prototype"/>
Command
和CommandManager
类只是示例类,其中process()
是该示例的一部分。它们与ApplicationContextAware
无关
注意CommandManager
示例中的注释:
// a class that uses a stateful Command-style class to perform some processing
process()
将由应用程序在其他地方调用;为了这个例子,这并不重要。如果您的代码中没有使用完全相同的模型,那么应该忽略此方法,只要在适用的地方调用applicationContext.getBean()
最后,应该将yesCommandManager
注册为bean,以便spring调用setApplicationContext()
编辑
为什么spring知道每次都必须用name命令重新创建bean
考虑到示例的内容,它没有。示例代码调用getBean()
,根据以下内容:
返回指定bean的实例,该实例可以是共享的,也可以是独立的
为了确保始终获得一个新实例,您需要使用prototype
范围
<bean id="beanB class="Command" scope="prototype"/>
注入实际上发生在这里:
protected Command createCommand() {
// notice the Spring API dependency!
return this.applicationContext.getBean("command", Command.class);
}
我们只是用这种方法得到了具有不同生命周期的bean。注入实际上发生在这里:
protected Command createCommand() {
// notice the Spring API dependency!
return this.applicationContext.getBean("command", Command.class);
}
我们只是用这种方法得到了具有不同生命周期的bean。注入实际上发生在这里:
protected Command createCommand() {
// notice the Spring API dependency!
return this.applicationContext.getBean("command", Command.class);
}
我们只是用这种方法得到了具有不同生命周期的bean。注入实际上发生在这里:
protected Command createCommand() {
// notice the Spring API dependency!
return this.applicationContext.getBean("command", Command.class);
}
在这个方法中,我们只是得到了具有不同生命周期的bean。那么,为什么spring知道每次被询问时都必须用名称命令重新创建bean呢