简体   繁体   English

基于Java的配置,带有参数的autowire bean

[英]Java based configuration, autowire bean with arguments

This is the first time i'm trying to write a swing app with Spring(java based configuration only, i don't want XML). 这是我第一次尝试使用Spring(仅基于Java的配置,我不需要XML)编写一个swing应用。 The problem is that i do not know how to autowire a bean that requires an argument, in my application i will be creating large amount of JPanel(and for them LineBorder etc.) and i'm struggling to find the correct way of how to do this. 问题是我不知道如何自动装配一个需要参数的bean,在我的应用程序中,我将创建大量的JPanel(以及它们的LineBorder等),而我正在努力寻找正确的方法来做这个。 Should i just create a bean definition that will return a simple JButton, set scope to "prototype", autowire it and do all the initialization after in my GUI class? 我是否应该只创建一个将返回简单JButton的bean定义,将作用域设置为“ prototype”,对其进行自动装配,然后在GUI类中进行所有初始化?

Also in swing app, should a create everything using spring?(not a single "new" keyword outside of my configuration class), like when i have 同样在swing应用中,是否应该使用spring创建所有内容?(不是我的配置类之外的单个“ new”关键字),就像我有

linebuttonPanel.setPreferredSize(new Dimension(0, 70));

should i instead write a method returning a new Dimension bean like this? 我应该改写一个返回像这样的新Dimension bean的方法吗?

Dimension d = context.getBean("dimension");
d.setSize(0, 70);
linebuttonPanel.setPreferredSize(d);

Or should i move the dimension initialization into the configuration class and write this 或者我应该将维度初始化移到配置类中并编写此代码

linebuttonPanel.setPreferredSize(context.getBean("dimension", 0, 70));

I was trying to use @Autowire wherever possible and this is the problem i encountered, here is first option where i need to somehow provide the argument while autowiring 我试图在任何可能的地方使用@Autowire,这是我遇到的问题,这是我在自动装配时需要以某种方式提供参数的第一个选择

在此处输入图片说明

Second option is to implement ApplicationContextAware and use getBean method but is this the right way of how to do this for every bean? 第二个选择是实现ApplicationContextAware并使用getBean方法,但这是对每个bean正确的做法吗?

在此处输入图片说明

I would probably use a factory method: 我可能会使用工厂方法:

public JButton createButton(String name) {
    (JButton)button = ctx.getBean(JButton.class);
    button.setName(name);
    return name;
}

So you can avoid "new" keyword and let spring everything create for you. 因此,您可以避免使用“ new”关键字,而让spring为您创建一切。

As an additional hint, you do not have to implement ApplicationContextAware, you can just autowire ApplicationContext 另外提示,您不必实现ApplicationContextAware,只需将ApplicationContext自动关联即可

You need an access to application context when you have to create your button. 必须创建按钮时,您需要访问应用程序上下文。

You can autowire ApplicationContext into your bean and lookup for Button bean from there, BUT it's extremely BAD idea. 您可以将ApplicationContext自动连接到您的bean中,然后从那里查找Button bean,但这是一个非常糟糕的主意。 You shouldn't inject entire application context into your particular bean, it breaks loose coupling idea the spring was designed for. 您不应该将整个应用程序上下文注入到特定的Bean中,它会破坏弹簧为之设计的松散耦合思想。

So you'd better remove ApplicationContextAware interface from your GUI class definition and delete ApplicationContext applicationContext class field as well. 因此,最好从GUI类定义中删除ApplicationContextAware接口,并删除ApplicationContext applicationContext类字段。

In this case it's better to make bean creation operations abstract and implement them in java based app config. 在这种情况下,最好使bean创建操作abstract并在基于Java的应用程序配置中实现。

I mean you can define abstract method 我的意思是您可以定义抽象方法

protected abstract JButton getButton(String name)

in your GUI class, so the class would become abstract as well. 在您的GUI类中,因此该类也将变为抽象。

Then you can make a bean definition in your current config as follows: 然后,您可以在当前配置中按如下所示定义Bean:

@Bean
public GUI gui() {
    return new GUI() {
        @Override
        protected JButton getButton(String name) {
            // note, here you're referencimg your Bean jButton of scope "prototype"
            return jButton(name);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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