我希望在spring应用程序上下文中动态定义一个MBeanServerConnection,因此我将通过prepareBeanFactory()注册它的工厂。我可以看到bean存在于上下文中,但是当我执行getBean()时,它会返回null!
有什么建议吗?
public static void main(String[] args) throws IOException, Exception, IntrospectionException, MalformedObjectNameException, ReflectionException {
final AbstractApplicationContext context = new ClassPathXmlApplicationContext() {
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
super.prepareBeanFactory(beanFactory);
final MBeanServerConnectionFactoryBean clientConnection = new MBeanServerConnectionFactoryBean();
try {
clientConnection.setServiceUrl("service:jmx:jmxmp://" + "localhost:7777");
beanFactory.registerSingleton("clientConn", clientConnection);
} catch (MalformedURLException e) {
}
}
};
context.refresh();
for (String name : context.getBeanNamesForType(Object.class)) {
System.out.println(name);
}
MBeanServerConnection mb = context.getBean("clientConn", MBeanServerConnection.class);
for (String s : mb.getDomains()) {
System.out.println(s);
}
}发布于 2013-09-16 13:16:58
假设您自己正在实例化工厂bean,那么您将负责通过调用afterPropertiesSet()来初始化它,这就是连接的位置。
如果您注册了一个BeanDefinition,上下文将负责为您初始化它。
https://stackoverflow.com/questions/18822343
复制相似问题