我有以下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);
我需要通过注释或任何其他方式将 deleteButton 和 addNewButton 从 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 文件中取出 deleteButton 和 addNewButton 的定义进行编码?
最佳答案
将 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/