如何在Spring中向自示例化对象注入依赖项?

q3qa4bjr  于 2023-02-03  发布在  Spring
关注(0)|答案(7)|浏览(110)

假设我们有一个类:

public class MyClass {
    @Autowired private AnotherBean anotherBean;
}

然后我们创建了这个类的一个对象(或者其他框架已经创建了这个类的示例)。

MyClass obj = new MyClass();

是否仍然可以注入依赖项?类似于:

applicationContext.injectDependencies(obj);

(我想谷歌Guice有这样的东西)

c2e8gylq

c2e8gylq1#

你可以使用AutowireCapableBeanFactoryautowireBean()方法来实现,你传递给它一个任意的对象,Spring会把它当作它自己创建的东西来对待,并且会应用各种各样的自动装配细节。
要获得AutowireCapableBeanFactory,只需自动连线:

private @Autowired AutowireCapableBeanFactory beanFactory;

public void doStuff() {
   MyBean obj = new MyBean();
   beanFactory.autowireBean(obj);
   // obj will now have its dependencies autowired.
}
tcbh2hod

tcbh2hod2#

您还可以使用@Configurable注解标记MyClass:

@Configurable
public class MyClass {
   @Autowired private AnotherClass instance
}

然后在创建时它会自动注入它的依赖项。你也应该在你的应用上下文xml中有<context:spring-configured/>

mpgws1up

mpgws1up3#

只是得到了同样的需求,在我的情况下,它已经是非Spring可管理的java类内部的逻辑,可以访问ApplicationContext。灵感来自scaffman。

AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
factory.autowireBean(manuallyCreatedInstance);
j9per5c4

j9per5c44#

我使用了一种不同的方法,我用Spring加载了bean,我想从第三方库的扩展类中调用这些bean,该库创建了自己的线程。
我使用的方法是https://confluence.jaytaala.com/display/TKB/Super+simple+approach+to+accessing+Spring+beans+from+non-Spring+managed+classes+and+POJOs
在非托管类中:

{
    [...]
    SomeBean bc = (SomeBean) SpringContext.getBean(SomeBean.class);

    [...]
    bc.someMethod(...)
}

然后作为主应用程序中的帮助器类:

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContext implements ApplicationContextAware
{
    private static ApplicationContext context;

    public static <T extends Object> T getBean(Class<T> beanClass)
    {   
        return context.getBean(beanClass);
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException
    {   
        SpringContext.context = context;
    }
}
zqry0prt

zqry0prt5#

我想分享我的解决方案,它遵循@glaz666 answer中提到的briefly方法,因为

  • skaffman开发的answer已经有将近10年的历史了,但这并不意味着它不够好或者不起作用
  • @glaz666的回答很简短,并没有真正帮助我解决问题,但却为我指明了正确的方向

我的设置

1.带有Spring Neo4j & Aop starts的Spring Boot 2.0.3(无论如何都是无关紧要的)
1.当Spring Boot准备就绪时,使用@Configurable方法示例化bean(使用ApplicationRunner

  1. Gradle和Eclipse
    步骤
    我需要按照以下步骤操作才能使其正常工作
  2. @Configurable(preConstruction = true, autowire = Autowire.BY_TYPE, dependencyCheck = false)将被放置在要手动示例化的Bean之上。在我的例子中,要手动示例化的Bean具有@Autowired服务,因此,支持上述注解。
    1.使用@EnableSpringConfigured@EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.ENABLED)注解Spring Boot的主XXXApplicaiton.java(或使用@SpringBootApplication注解的文件
    1.在您的构建文件中添加依赖项(即build. gradle或pom.xml,具体取决于您使用的文件)compile('org.springframework.boot:spring-boot-starter-aop')compile('org.springframework:spring-aspects:5.0.7.RELEASE')
    1.新的+你的Bean,它是用@Configurable注解的,它的依赖关系应该是自动连接的。
  • 关于上面的第3点,我知道org.springframework.boot:spring-boot-starter-aop会传递性地拉取spring-aop(如这里所示mavencentral)但是,在我的例子中,Eclipse未能解析@EnableSpringConfigured注解,因此,除了启动器之外,我还显式添加了spring-aop依赖项。只需声明依赖关系或冒险找出
  • 是否存在版本冲突
  • 为什么org.springframework.context.annotation.aspect.*不可用
  • 您的IDE设置是否正确
  • 等等等等
fnx2tebb

fnx2tebb6#

这对我很有效:

@Configuration
public class AppConfig {
    @Bean
    public TransferService transferService() {
        return new TransferServiceImpl();
    }
}

查看更多信息:https://docs.spring.io/spring-javaconfig/docs/1.0.0.m3/reference/html/creating-bean-definitions.html

w8rqjzmb

w8rqjzmb7#

发现以下方法对我的用例很有用。在这里分享以供参考,功劳完全归blogger所有。这创建了一个静态字段并从Spring填充该字段,然后提供了一个公共静态方法,该方法返回上面填充的字段。
https://sultanov.dev/blog/access-spring-beans-from-unmanaged-objects/

相关问题