java - 如何从xml文件中取出bean定义?

标签 java spring

我有以下app-context.xml文件:

<bean id="buttonPanel" class="todo.ui.BoxLayoutPanel" init-method="init">
    <property name="axis">
      <!--  "0" corresponds to BoxLayout.X_AXIS -->
      <value>0</value>
    </property>
    <property name="panelComponents">
      <list>
        <ref bean="deleteButton"/>
        <ref bean="addNewButton"/>
      </list>
    </property>
</bean>

<bean id="deleteButton" class="todo.ui.button.ActionListenerButton" 
        init-method="init">
    <property name="actionListener">
      <ref bean="deleteButtonActionListener"/>
    </property>
    <property name="text">
      <value>Delete</value>
    </property>
</bean> 

<bean id="addNewButton" class="todo.ui.button.ActionListenerButton" 
        init-method="init">
    <property name="actionListener">
      <ref bean="addNewButtonActionListener"/>
    </property>
    <property name="text">
      <value>Add New</value>
    </property>
</bean>

ActionListenerButton 看起来像这样:

public class ActionListenerButton extends JButton {
    private ActionListener actionListener;

    public void setActionListener(ActionListener actionListener) {
        this.actionListener = actionListener;
    }

    public ActionListener getActionListener() {
        return actionListener;
    }

    public void init() {
        this.addActionListener(actionListener);
    }
}

主类包含以下代码:

String[] contextPaths = new String[] {"pack/app-context.xml"};
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(contextPaths);

我需要通过注释或任何其他方式将 deleteButtonaddNewButton 从 xml 文件中取出来进行编码。

我尝试了以下方法:

String[] contextPaths = new String[] {"pack/app-context.xml"};
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(contextPaths);

AutowireCapableBeanFactory factory = ctx.getAutowireCapableBeanFactory();
GenericBeanDefinition definition = new GenericBeanDefinition();
definition.setBeanClass(ActionListenerButton.class);

MutablePropertyValues mpv = new MutablePropertyValues();
mpv.addPropertyValue("actionListener", ctx.getBean("deleteButtonActionListener"));
mpv.addPropertyValue("text", "Delete");
definition.setPropertyValues(mpv);

BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
registry.registerBeanDefinition("deleteButton", definition);
ctx.refresh();

但是在 app-context.xml 文件中注释 deleteButton bean 后:

<!-- <bean id="deleteButton" class="todo.ui.button.ActionListenerButton" 
        init-method="init">
    <property name="actionListener">
      <ref bean="deleteButtonActionListener"/>
    </property>
    <property name="text">
      <value>Delete</value>
    </property>
</bean>  -->

我遇到错误:

NoSuchBeanDefinitionException: No bean named 'deleteButton' is defined

如何通过注释或任何其他方式从 xml 文件中取出 deleteButtonaddNewButton 的定义进行编码?

最佳答案

将 XML 配置 bean 定义替换为 Java 配置 bean 定义 - 下面是 2 个 bean 之一的示例:

//ensure this class is discoverable via the component-scan of your app-context.xml
@Configuration
public class MyConfig {

    //..

    @Autowired
    @Bean(name = "deleteButton")
    public ActionListenerButton deleteButton(@Qualifier("deleteButtonActionListener") ???Type deleteButtonActionListener) {
        ActionListenerButton deleteButton = new ActionListenerButton();
        deleteButton.setActionListener(deleteButtonActionListener);
        deleteButton.setText("Delete");
        deleteButton.init();
        return deleteButton;
    }

    //..
}

???Type 是您的 deleteButtonActionListener

的类型

关于java - 如何从xml文件中取出bean定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49610375/

相关文章:

java - 如何使用 dao、hibernate 检查同名项目是否存在

java - 需要最佳的实体和查询设计来获取具有多个关联的对象

java - Spring Boot Controller 错误

spring - 带有 Java util.Date 的 Postgres 时间戳列

java - 示例需要 : manually override automatic SPI class registration (Java AudioSystem), 以实现 MP3+M4A 共存

java - 字符串正则表达式解析数据中的分号

Spring分页-请求参数

java - 如何从 Spring 中的 Soap 消息中提取附件

java - Spring Data Cassandra 连接问题

java - JTree 对象转换