[英]How do I reference other beans from within a configuration class in spring?
I have various swing actions in a swing/spring application. 我在秋千/弹簧应用中有各种秋千动作。 They are annotated as @Component so they should be visible after component autoscanning. 它们被标注为@Component,因此它们在组件自动扫描后应该可见。 I have a configuration class where I am defining a bean for a menu for the main frame/window. 我有一个配置类,在其中为主框架/窗口的菜单定义一个bean。 The method to create/return the menu object has to reference the actions as necessary. 创建/返回菜单对象的方法必须引用必要的操作。 In a Beans.xml setting, I would just do something like: 在Beans.xml设置中,我将执行以下操作:
<bean id="mainMenu" class="javax.swing.JMenu">
<constructor-arg id="0" value="Schedule" />
<constructor-arg id="1">
<value type="int">0</value>
</constructor>
</bean>
Then, in the setter for the bean in the main form, I would autowire before the setter and add the items. 然后,在主形式的bean的setter中,我将在setter之前自动连线并添加项目。 In swing there's no way to as a property set the menu items - you have to add them. 在摆动中,无法将菜单项作为属性来设置-您必须添加它们。 In a beans.xml setting, I can though reference a bean by id or type in another bean's creation. 在bean.xml设置中,我可以按ID引用一个bean或键入另一个bean的创建内容。 How do I do that in my configuration class? 如何在配置类中做到这一点? Like this is my configuration class: 像这样是我的配置类:
@Configuration
@ComponentScan("net.draconia.ngucc.usher")
public class BeanConfiguration
{
private Action mActCreateSchedule, mActEditSchedule, mActExit, mActRemoveSchedule;
private JMenu mMnuSchedule;
@Bean("scheduleMenu")
public JMenu getScheduleMenu()
{
if(mMnuSchedule == null)
{
mMnuSchedule = new JMenu("Schedule");
mMnuSchedule.setMnemonic(KeyEvent.VK_S);
mMnuSchedule.add(getCreateScheduleAction());
mMnuSchedule.add(getEditScheduleAction());
mMnuSchedule.add(getRemoveScheduleAction());
mMnuSchedule.addSeparator();
mMnuSchedule.add(getExitAction());
}
}
}
I want to be able to instead of the get functions, or maybe have get functions to access the items - in the get functions then there would be something like return((CreateSchedule)(getBean(CreateSchedule.class))) (I think I have enough/right number of parentheses there haha). 我希望能够代替get函数,或者可能具有get函数来访问项目-在get函数中,那么会有诸如return((CreateSchedule)(getBean(CreateSchedule.class)))之类的东西(我想我那里有足够/正确的括号数量哈哈)。 I'd just need access to an application context. 我只需要访问应用程序上下文。 Can I somehow autowire one(an application context) in the configuration class or how would I possibly get access to a getBean to access those component scanned beans? 我可以以某种方式在配置类中自动连接一个(应用程序上下文),还是可以访问getBean来访问那些组件扫描的bean?
Thank you in advance! 先感谢您!
@Autowired
ApplicationContext into the configuration class @Autowired
ApplicationContext进入配置类
@Configuration
@ComponentScan("net.draconia.ngucc.usher")
public class BeanConfiguration
{
private Action mActCreateSchedule, mActEditSchedule, mActExit, mActRemoveSchedule;
private JMenu mMnuSchedule;
@Autowired
ApplicationContext context;
@Bean("scheduleMenu")
public JMenu getScheduleMenu()
{
if(mMnuSchedule == null)
{
mMnuSchedule = new JMenu("Schedule");
mMnuSchedule.setMnemonic(KeyEvent.VK_S);
mMnuSchedule.add(getCreateScheduleAction());
mMnuSchedule.add(getEditScheduleAction());
mMnuSchedule.add(getRemoveScheduleAction());
mMnuSchedule.addSeparator();
mMnuSchedule.add(getExitAction());
}
}
}
Another solution is to make use of ApplicationContextAware interface like : 另一个解决方案是利用ApplicationContextAware接口,例如:
@Component
public class ContextUtil implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
ContextUtil.context=context;
}
public static ApplicationContext getContext() {
return context;
}
public static void setContext(ApplicationContext context) {
ContextUtil.context = context;
}
public static Object getBean(String beanName){
return getContext().getBean(beanName);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.