草庐IT

Bean-IOC

全部标签

java - 实现 Spring IOC 时如何避免使用 ApplicationContext.getBean()

我刚刚开始了解SpringIOC概念。我经常看到网上找到的大部分例子都是用代码来获取对象的。ApplicationContextappContext=newClassPathXmlApplicationContext("applicationContext.xml");Hellohello=(Hello)appContext.getBean("hello");作为这些问题的引用1和2在堆栈溢出中。我推断,没有必要在代码中使用appContext.getBean("hello"),这被认为是不好的做法。另外,不推荐了。在这里纠正我,如果我的推断是错误的。考虑到这一点,我对我的项目进行了相

主方法类中的 Spring bean 注入(inject)

我有一个带有spring3.0的Web应用程序。我需要从一个使用appcontextxml中定义的bean的cron运行一个带有main方法的类(使用组件扫描通知)。我在同一个src目录中有我的主类。如何将Web上下文中的bean注入(inject)main方法。我尝试使用ApplicationContextcontext=newClassPathXmlApplicationContext("appservlet.xml");我尝试使用AutoWired,它返回一个空bean。所以我使用了Applicationctx,这在我运行main方法时创建了一个新的上下文(如预期的那样)。但是是

spring - hibernate-envers RevisionListener spring 集成为 spring bean

我需要在hibernate-envers的修订监听器中做一些数据库处理。为此,我需要SpringFramework的注入(inject)功能。如何实现?这是表示需要的代码,但CustomRevisionListener由Envers代码中的构造函数实例化。Spring只有SecurityContextHolder作为静态服务定位器。如何注入(inject)其他bean?@ServicepublicclassCustomRevisionListenerimplementsEntityTrackingRevisionListener{@ResourceprivatePersistenceM

java - @Bean 和 @Component 注释是否相同但针对 Spring Framework 中的不同目标?

这个问题在这里已经有了答案:Spring:@Componentversus@Bean(15个回答)关闭7年前。我认为@Bean和@Component注释是一样的,但第一个是用于类,另一个是用于方法?在这两种情况下,容器只会创建适当的bean,对吗? 最佳答案 不完全是。它们属于不同的概念。@Component是一个原型(prototype)注解。使用此注释注释的类将在类路径扫描期间自动检测到。另请参阅Spring引用文档第5.10章。除此之外,@Bean注释属于Java配置特性。在配置类中,此注解用于标记定义bean的方法。

java - Spring原型(prototype)bean是否需要手动销毁?

我注意到我的原型(prototype)作用域Springbean的@PreDestroy钩子(Hook)没有被执行。我已经阅读了here这实际上是设计使然。Spring容器将销毁单例bean,但不会销毁原型(prototype)bean。我不清楚为什么。如果Spring容器将创建我的原型(prototype)bean并执行它的@PostConstruct钩子(Hook),为什么当容器关闭时它不会破坏我的bean?一旦我的Spring容器关闭,继续使用它的任何bean是否有意义?我看不到您想要在完成其bean之前关闭容器的场景。在容器关闭后是否可以继续使用原型(prototype)Spr

java - 由于缺少 EmbeddedServletContainerFactory bean,Spring 应用程序无法启动

我正在尝试在heroku上部署一个应用程序,但遇到了几个问题。该应用程序在我的IDE(Intellij)中运行,但是当我尝试使用“herokulocal-fProcfile.windows”运行它时,我收到此错误:11:30:03PMweb.1|2016-05-2423:30:03.491WARN10368---[main]ationConfigEmbeddedWebApplicationContext:Exceptionencounteredduringcontextinitialization-cancellingrefreshattempt:org.springframework

java - 如何禁用某个 bean 的 Spring Autowiring ?

jar(外部库)中有一些类在内部使用Spring。所以库类的结构如下:@ComponentpublicclassTestBean{@AutowiredprivateTestDependencydependency;...}并且库提供了构造对象的API:publicclassLibrary{publicstaticTestBeancreateBean(){ApplicationContextcontext=newAnnotationConfigApplicationContext(springConfigs);returncontext.getBean(TestBean);}}在我的应用

spring - 注入(inject)bean列表时,列表中的顺序是否与bean定义的顺序相同

@Service@Order(1)publicclassFooServiceimplementsIService{..}@Service@Order(2)publicclassBarServiceimplementsIService{..}是否保证下面列表中的顺序永远是{FooService,BarService}:@InjectprivateListservices;(同样的问题也适用于xml配置) 最佳答案 我猜不是,因为@Order不是通用注释。来自javadoc:NOTE:Annotation-basedorderingis

java - 如何在spring boot中使用@Bean为抽象类创建bean

我需要将旧式spring项目迁移到Springboot。假设下面的代码片段我必须迁移到Spring引导样式。我问,如何将下面的抽象bean转换为@Bean? 最佳答案 用纯Java编写你的抽象基类(没有任何Spring耦合):publicabstractclassAbstractClass{privateSample1sample1;privateSample2sample2;publicAbstractClass(Sample1sample1,Sample1sample2){this.sample1=sample1;this.sa

java - Spring 无法 Autowiring ,有多个 `` 类型的 bean

这是我的问题:我有一个基接口(interface)和两个实现类。并且一个Service类对基接口(interface)有依赖,代码是这样的:@ComponentpublicinterfaceBaseInterface{}@ComponentpublicclassClazzImplAimplementsBaseInterface{}@ComponentpublicclassClazzImplBimplementsBaseInterface{}而且配置是这样的:@ConfigurationpublicclassSpringConfig{@BeanpublicBaseInterfacecla