草庐IT

java - Spring Java 配置 : how do you create a prototype-scoped @Bean with runtime arguments?

使用Spring的JavaConfig,我需要使用只能在运行时获得的构造函数参数来获取/实例化一个原型(prototype)范围的bean。考虑以下代码示例(为简洁起见):@AutowiredprivateApplicationContextappCtx;publicvoidonRequest(Requestrequest){//requestisalreadyvalidatedStringname=request.getParameter("name");Thingthing=appCtx.getBean(Thing.class,name);//System.out.println(

java - Autowiring 两个实现相同接口(interface)的 bean - 如何将默认 bean 设置为 Autowiring ?

背景:我有一个Spring2.5/Java/Tomcat应用程序。有下面这个bean,在整个应用中很多地方都用到了publicclassHibernateDeviceDaoimplementsDeviceDao以及以下新的bean:publicclassJdbcDeviceDaoimplementsDeviceDao第一个bean是这样配置的(包中的所有bean都包括在内)第二个(新)bean单独配置这会导致(当然)在启动服务器时出现异常:nestedexceptionisorg.springframework.beans.factory.NoSuchBeanDefinitionExc

java - 可以将 Gson 实例用作模型 bean 中的静态字段(重用)吗?

这是我实现的模型:publicclassLoginSession{privatestaticfinalGsongson=newGson();privateStringid;privateStringname;privatelongtimestamp;publicLoginSession(Stringid,Stringname){this.id=id;this.name=name;this.timestamp=System.currentTimeMillis();}publicStringtoJson(){returngson.toJson(this);}publicstaticLogi

java - “字段需要一个无法找到的 bean。”使用mongodb的错误spring restful API

所以我这几周一直在学习Spring,一直在学习本教程BuildingaRESTfulWebService在我尝试将它集成到mongodb之前一切都很好。所以我按照本教程进行操作。AccessingDatawithMongoDB但我的做法部分仍在使用第一个。所以我的项目目录结构是这样的。src/├──main/│└──java/|├──model/||└──User.java|├──rest/||├──Application.java||├──IndexController.java||└──UsersController.java|└──service/|└──UserService.

java - Spring:@Component 与 @Bean

我了解@Component注释是在Spring2.5中引入的,目的是通过使用类路径扫描来摆脱xmlbean定义。@Bean是在spring3.0中引入的,可以与@Configuration一起使用,以便完全摆脱xml文件并改用javaconfig。是否可以重复使用@Component注释而不是引入@Bean注释?我的理解是最终目标是在这两种情况下创建bean。 最佳答案 @组件首选用于元件扫描和自动布线。什么时候应该使用@Bean?有时自动配置不是一个选项。什么时候?假设您想从3rd-party库中连接组件(您没有源代码,因此无法使

java - Spring Bean 到底是什么?

我还没有找到我能理解的Springbean的高级定义。我在Grails文档和书籍中经常看到它们被引用,但我认为了解它们是什么会很有帮助。那么什么是Springbean?如何使用它们?他们和依赖注入(inject)有关系吗? 最佳答案 Spring核心技术referencedocumentation描述bean是什么。根据IntroductiontotheSpringIoCContainerandBeans部分(其中“IoC”表示“inversionofcontrol”):InSpring,theobjectsthatformtheb

一文搞懂什么是@Component和@Bean注解以及如何使用

一、前言@Component和@Bean的作用都是注册一个bean到IOC容器中。二、@Component和@Bean两者对比1、@Component作用于类,而@Bean作用于方法(见源码)2、@Component是通过类路径扫描的方式自动装配bean到IOC容器中的,而@Bean是将方法返回值作为bean自动装配到IOC容器中的3、@Bean的功能比@Component的功能更强大,当我们需要引入外部类(即第三方库中的类),并将它注入到IOC容器中时,@Component注解是做不到的,但@Bean可以做到。这也是我认为的两者最大的区别!(当然在引入第三方库中的类时,@Import注解也可

解决报错Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPoint

解决报错Failedtostartbean‘documentationPluginsBootstrapper’;nestedexceptionisjava.lang.NullPointerException目录解决报错Failedtostartbean'documentationPluginsBootstrapper';nestedexceptionisjava.lang.NullPointerException报错信息报错原因解决办法方法一:springboot版本回退到2.5.X;方法二:application.yml配置文件配置路径如下报错信息org.springframework.co

解决报错Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPoint

解决报错Failedtostartbean‘documentationPluginsBootstrapper’;nestedexceptionisjava.lang.NullPointerException目录解决报错Failedtostartbean'documentationPluginsBootstrapper';nestedexceptionisjava.lang.NullPointerException报错信息报错原因解决办法方法一:springboot版本回退到2.5.X;方法二:application.yml配置文件配置路径如下报错信息org.springframework.co

Spring Boot 2.7.3整合Swagger启动失败Failed to start bean ‘documentationPluginsBootstrapper‘

问题SpringBoot2.7.3版本引入依赖springfox-boot-starter(Swagger3.0)后,启动容器会报错:org.springframework.context.ApplicationContextException:Failedtostartbean'documentationPluginsBootstrapper';nestedexceptionisjava.lang.NullPointerException原因Springfox假设SpringMVC的路径匹配策略是ant-path-matcher,而SpringBoot2.6以上版本的默认匹配策略是path-