简体   繁体   English

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

[英]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.

相关问题 Spring配置:2个具有相同类引用的bean - Spring Configuration: 2 beans with same class reference 从非常相同的 @Configuration 类习语中注入 @Beans - Injecting @Beans from within a very same @Configuration class idioms Spring Boot Application如何在没有@Configuration类的情况下创建bean - How does a Spring Boot Application create beans without @Configuration class 如何为来自不同属性文件的 Bean 列表配置 Spring 配置? - How to configure Spring configuration for List of Beans from different property files? 如何将类设置为 bean 中的值? - How do i set a class as a value in beans? 如何找到给定的Spring Boot类/包的bean列表? - How do I find a list of beans for a given Spring Boot class/package? 如何将 applicationContext.xml 转换为 spring @Configuration class? - How do I convert an applicationContext.xml into a spring @Configuration class? 如何在另一个类中更改变量的值,并且仍然能够在另一个类中更改它的值? - How do I change the value of a variable from another class and still be able to change it within the other class? 如果我在配置 class 上使用 @ActiveProfiles 注释而不是在定义我的 bean 的 class 上使用它,Spring 会发生什么? - What happens in Spring if I use the @ActiveProfiles annotation on a configuration class instead use it on the class that defines my beans? 从XML Bean配置生成Spring Model Bean - Generate Spring Model Beans from XML beans configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM