繁体   English   中英

如何在春季从配置类中引用其他bean?

[英]How do I reference other beans from within a configuration class in spring?

我在秋千/弹簧应用中有各种秋千动作。 它们被标注为@Component,因此它们在组件自动扫描后应该可见。 我有一个配置类,在其中为主框架/窗口的菜单定义一个bean。 创建/返回菜单对象的方法必须引用必要的操作。 在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>

然后,在主形式的bean的setter中,我将在setter之前自动连线并添加项目。 在摆动中,无法将菜单项作为属性来设置-您必须添加它们。 在bean.xml设置中,我可以按ID引用一个bean或键入另一个bean的创建内容。 如何在配置类中做到这一点? 像这样是我的配置类:

@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());
            }
    }
}

我希望能够代替get函数,或者可能具有get函数来访问项目-在get函数中,那么会有诸如return((CreateSchedule)(getBean(CreateSchedule.class)))之类的东西(我想我那里有足够/正确的括号数量哈哈)。 我只需要访问应用程序上下文。 我可以以某种方式在配置类中自动连接一个(应用程序上下文),还是可以访问getBean来访问那些组件扫描的bean?

先感谢您!

@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());
            }
    }
}

另一个解决方案是利用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM