Spring @Component@Scope(“singleton”)公共类引导程序实现ApplicationListener<;ContextStartedEvent>;{
我正在尝试一次性初始化我的webapp。我需要ApplicationListener类的singleton,因此我将作用域设置为singleton,但它正在创建多个实例。此引导程序未在任何其他xml配置文件中定义。 我知道默认范围是singleton,但必须添加@scope(“singleton”),因为它不是singleton。即使使用此注释,它仍然会创建多个实例。 这是我的应用程序列表Spring @Component@Scope(“singleton”)公共类引导程序实现ApplicationListener<;ContextStartedEvent>;{,spring,web-applications,Spring,Web Applications,我正在尝试一次性初始化我的webapp。我需要ApplicationListener类的singleton,因此我将作用域设置为singleton,但它正在创建多个实例。此引导程序未在任何其他xml配置文件中定义。 我知道默认范围是singleton,但必须添加@scope(“singleton”),因为它不是singleton。即使使用此注释,它仍然会创建多个实例。 这是我的应用程序列表 @Component @Scope("singleton") public class BootStrap
@Component
@Scope("singleton")
public class BootStrapper implements ApplicationListener<ContextRefreshedEvent> {
@组件
@范围(“单例”)
公共类引导程序实现ApplicationListener{
我遗漏了什么吗?要在bean初始化后调用回调,请使用 试着这样做:
@Configuration
public class TestService
{
private Properties properties;
@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public Properties getAppProperties()
{
try
{
if (properties == null)
{
properties = ServiceUtils.loadProperties();
}
return properties;
}
catch (Exception e)
{
LOGGER.logCaughtException("Exception Occured while loading App Properties.", e);
}
}
}
默认范围是Singleton您的问题是什么?“但它不工作”不是任何(未命名)问题的有效描述!谢谢,更新了问题。您如何检查它是否创建bean的服务器实例?您将断点放在构造函数中的何处?您可能指的是javax.annotation.PostConstruct(不是后创建)。此外,还应将返回类型添加到“doSomething”方法中。
@Configuration
public class TestService
{
private Properties properties;
@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public Properties getAppProperties()
{
try
{
if (properties == null)
{
properties = ServiceUtils.loadProperties();
}
return properties;
}
catch (Exception e)
{
LOGGER.logCaughtException("Exception Occured while loading App Properties.", e);
}
}
}