标签: spring-bean

Spring:如何在主bean创建后初始化相关的懒豆

我有懒惰初始化bean的代码:

@Component @Lazy
class Resource {...}

@Component @Lazy @CustomProcessor
class ResourceProcessorFoo{
    @Autowired
    public ResourceProcessor(Resource resource) {...}
}
@Component @Lazy @CustomProcessor
class ResourceProcessorBar{
    @Autowired
    public ResourceProcessor(Resource resource) {...}
}
Run Code Online (Sandbox Code Playgroud)

初始化应用程序上下文后,没有此bean的实例.当bean资源由应用程序上下文创建时(例如,applicationContext.getBean(Resource.class)),没有@CustomProcessor实例标记bean.

在创建Resource bean时,需要使用@CustomProcessor创建bean.怎么做?

更新:找到一个丑陋的解决方案 - 使用空的自动装配的setter:

@Autowired
public void setProcessors(List<ResourceProcessor> processor){}
Run Code Online (Sandbox Code Playgroud)

Bean BeanPostProcessor的另一个丑陋的解决方案(太神奇了!)

@Component
class CustomProcessor implements BeanPostProcessor{
    public postProcessBeforeInitialization(Object bean, String beanName) {
        if(bean instanceof Resource){
            applicationContext.getBeansWithAnnotation(CustomProcessor.class);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

也许有更优雅的方式?

java spring spring-bean

5
推荐指数
1
解决办法
2868
查看次数

相当于非 Boot Spring 项目的 org.springframework.boot.context.embedded.FilterRegistrationBean?

我正在尝试按照本教程在 Spring 中实现外部会话处理。不过,我在添加正确的过滤器时遇到了一些麻烦。Spring Boot 似乎定义了正确的 bean/过滤器,但我的项目不是 Spring Boot,所以它找不到FilterRegistrationBean. 在 Spring 的非 Boot 版本中是否有某种等价于这个类?我也试过org.springframework.web.context.embedded.FilterRegistrationBean,但无法正确导入(看起来这个文档指的是 SNAPSHOT 版本,所以也许这个包从来都不是正确版本的一部分)。

spring spring-mvc servlet-filters spring-bean spring-session

5
推荐指数
1
解决办法
1914
查看次数

spring 是否并行初始化 bean?

我的项目中有很多独立的spring bean(用@Component-- NOT 延迟初始化注释的类)。我想知道豆子是串行还是并行初始化的天气?我问这个是因为 bean 在初始化时修改了一个公共数据结构,我想知道数据结构应该是线程安全的。

使用的 Spring 版本:- 4.2

我在 spring jira 网站上阅读未解决的问题:-

根据https://jira.spring.io/browse/SPR-9887 它似乎是并行初始化的

但根据https://jira.spring.io/browse/SPR-8767它似乎是串行初始化的。

spring spring-bean

5
推荐指数
1
解决办法
3333
查看次数

Spring boot 1.4.2.RELEASE 启动时出错 java.lang.ClassNotFoundException: org.springframework.beans.factory.ObjectProvider

我正在尝试将 spring-boot 从 1.3 升级到1.4.2.RELEASE. 运行mvn spring-boot:run会引发以下错误。

java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.jpaVendorAdapter
    at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:64) ~[spring-boot-autoconfigure-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:102) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:178) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:140) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:333) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at com.cisco.ple.AdminApplication.main(AdminApplication.java:20) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_74]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_74]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) …
Run Code Online (Sandbox Code Playgroud)

spring hibernate spring-bean spring-boot

5
推荐指数
1
解决办法
2万
查看次数

异步 Bean 初始化

特定 bean 的初始化需要执行网络连接。这可能需要一些时间,并且不必要地将已经很长时间的启动阻塞几秒钟。

有什么方法可以让我的 bean 发出信号,即使它退出构造函数后它仍未初始化,然后在它准备就绪后,向上下文发出信号,表明它现在已准备就绪,并且它的状态应移至“已初始化” .

spring spring-bean spring-async

5
推荐指数
1
解决办法
2047
查看次数

忽略 spring BeanUtils 嵌套字段的属性不起作用

如何将嵌套属性的忽略属性定义为 spring BeanUtils?

BeanUtils.copyProperties(source, target, ignorePropertiesName);
Run Code Online (Sandbox Code Playgroud)

我有嵌套类名称的类 Contact 及其两个属性“firstName”和“lastname”。

我尝试了三种不同的模式来传递嵌套的文件名以忽略列表,但它们都不起作用。

"contact.name.lastName"
"name.lastName"
"lastName"
Run Code Online (Sandbox Code Playgroud)

下面是带有类定义的完整单元测试,我使用的是 java 8 的 spring-beans-4.3.9.RELEASE 版本。

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.BeanUtils;

public class NestedCopyPropertiesTest {

// @Test
public void CopyProperties() {
    String firstName = "Andy";
    String lastName = "Murray";
    Contact source = new Contact(new Name(firstName, lastName));
    Contact target = new Contact(new Name(null, null));

    BeanUtils.copyProperties(source, target);

    String targetFirstName = target.getName().getFirstName();
    String targetLastName = target.getName().getLastName();
    log("targetFirstName: " + targetFirstName);
    log("targetLastName: " + targetLastName);

    Assert.assertTrue("Failed to copy nested properties.", …
Run Code Online (Sandbox Code Playgroud)

spring-bean

5
推荐指数
0
解决办法
890
查看次数

Junit 5 测试,获取 java.lang.IllegalStateException:测试类不能包含 @Bean 方法

@ContextConfiguration(classes = ConfigureCustomConfigurationModelProviderTest.class)
    public class ConfigureCustomConfigurationModelProviderTest extends AbstractContextTest {
        @Bean(name = "smth")
        public static ConfigurationModelProvider get() {
            return AnnotationConfigurationModelProvider.getInstance();
        }
        /*...*/
    }
Run Code Online (Sandbox Code Playgroud)

自从从 junit4 迁移到 junit5 后,我收到此错误。为什么?

java spring spring-bean junit5

5
推荐指数
1
解决办法
4882
查看次数

在 Spring Boot 中使用 Java 配置在 bean 定义之外别名 bean

如何在 Spring Boot 中使用 Java 配置为 bean 定义之外的 bean 取别名?

java spring spring-bean spring-boot

5
推荐指数
2
解决办法
319
查看次数

具有该名称的 bean 已在类路径资源 [path] 中定义并且覆盖被禁用

我有 Spring Data Elaticsearch(使用传输客户端)和 ESTemplate 的 java 配置。这里有一些除外:

@Configuration
@EnableElasticsearchRepositories(basePackages = "subpackage-in-this-project")
@PropertySource("file:path-to-file")
public class ESConfig {

    @Bean
    ElasticsearchTemplate elasticsearchTemplate(Client client) {
        return new ElasticsearchTemplate(client);
    }

    @Bean
    Client client() { 
// configuration of the ES client
   }

}
Run Code Online (Sandbox Code Playgroud)

我有一个配置,可以在不同的项目中扩展上面的配置。

@Configuration
@ComponentScan("package-prefix-that-matches-packages-in-both-projects")
@EnableElasticsearchRepositories(basePackages = "subpackage-in-this-project")
@PropertySource("file:same-path-to-file-as-in-the-config-above")
public class ExtendedESConfig extends ESConfig {

    @Value("index-name")
    private String indexName;

    @Bean
    public String indexName() {
        return indexName;
    }
}
Run Code Online (Sandbox Code Playgroud)

在执行第三个 Spring Boot 应用程序时,它使用了对项目的依赖ExtendedESConfig,我得到了这个,我不太明白为什么会发生,从 2.0.5.RELEASE Spring 升级到 2.2.9.RELEASE 后开始发生引导版。


***************************
APPLICATION FAILED TO START …
Run Code Online (Sandbox Code Playgroud)

initialization exception spring-bean spring-boot spring-data-elasticsearch

5
推荐指数
2
解决办法
1万
查看次数

将 Spring Bean 暴露给 Micronaut 应用程序

是否可以将 Spring Bean 暴露给 Micronaut 应用程序,并将它们与 @Autowired 一起使用,或 @Inject 它们到 Micronaut 应用程序中,并通过 Maven pom.xml 将 Spring Beans 项目作为 Maven 依赖项添加到 Micronaut 项目中?

该指南: https: //micronaut-projects.github.io/micronaut-spring/latest/guide/ 讨论“将 Micronaut Bean 暴露给 Spring 应用程序”,但反之则不然。

我想在 Micronaut 应用程序中使用的 Spring Bean 是使用 @Repository、@Component、@Service、@Configuration 公开的。

这是否可能,是否是在 Micronaut 应用程序本身中定义 Spring Bean 的先决条件,以及是否支持使用库(定义了 Spring Beans 的 JAR 文件)作为 Micronaut 应用程序的依赖项?

每当我尝试 @Autowire 或 @Inject 在与 Micronaut 应用程序不同的应用程序中定义的 Spring Bean(前者已作为 pom.xml 中的依赖项添加到其中)时,我会收到以下异常:

io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for field [fieldName] of class: MicronautClassName_From_Where_I_have_Referrenced_the_Spring_Bean

Path Taken: Class.fieldName
    at io.micronaut.context.AbstractBeanDefinition.getBeanForField(AbstractBeanDefinition.java:1462)
    at …
Run Code Online (Sandbox Code Playgroud)

java autowired spring-bean spring-boot micronaut

5
推荐指数
0
解决办法
309
查看次数