Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 库中的Bean注入失败_Java_Spring_Dependency Injection - Fatal编程技术网

Java 库中的Bean注入失败

Java 库中的Bean注入失败,java,spring,dependency-injection,Java,Spring,Dependency Injection,我创建了一个库,在其中创建了一些bean。下面是我创建一些bean的文件: @Configuration public class StorageBindings { @Value("${storageAccountName}") private String storageAccountName; @Value("${storageAccountKey}") private String storageAccountKey; @Bean(name = "cloudB

我创建了一个库,在其中创建了一些bean。下面是我创建一些bean的文件:

@Configuration
public class StorageBindings {

  @Value("${storageAccountName}")
  private String storageAccountName;

  @Value("${storageAccountKey}")
  private String storageAccountKey;


  @Bean(name = "cloudBlobClient")
  public CloudBlobClient getCloudBlobClientUsingCredentials() throws URISyntaxException {
     return new CloudBlobClient();
  }


  @Bean(name = "storageCredentialsToken")

  public StorageCredentialsToken getStorageCredentialsToken() throws IOException {
     return new StorageCredentialsToken();
  }

  @Bean(name = "msiTokenGenerator")
  public MSITokenGenerator getMSITokenGenerator() {
    return new MSITokenGenerator();
  }
}
然后我创建了这个类,并将其用作进一步操作的入口点

public class StorageClient {

  @Autowired
  private CloudBlobClient cloudBlobClient;

  @Autowired
  private MSITokenGenerator msiTokenGenerator;

  @Value("${storageAccountName}")
  private String storageAccountName;

  @Value("${storageAccountKey}")
  private String storageAccountKey;
}
我用上述文件创建了jar,并将其包含在我们的主项目中,在主项目中,我创建了StorageClient的bean,如下所示:

@Bean(name = {"storageClient"})
    public StorageClient getStorageClient() {
        LOG.debug("I am inside storage class");
        StorageClient ac = null;
        try {
            ac = new StorageClient();
            return ac;
    }
但在执行之后,我发现StorageClient实例ac中没有以下变量的注入,甚至没有反映环境属性,并且所有变量都为空:

   //beans NOT Injecting 
   ac.cloudBlobClient=null;
   ac.msiTokenGenerator=null;

//env variables
   ac.storageAccountName=null;
   ac.storageAccountKey=null;

我是否遗漏了什么,因为我正在变空。bean的实例化顺序是确定的。我查过了。因此,首先要创建StorageBindings的Bean。

如果您在@Bean注释方法内部创建对象,autowiring不会在那里注入Bean,您只是自己创建它。 因此,您必须在配置类中的字段上@Autowire it ie,并使用setter/constructor进行设置。 即:


若您在@Bean注释的方法内部创建对象,那个么autowiring不会在那个里注入Bean——您只是自己创建它。 因此,您必须在配置类中的字段上@Autowire it ie,并使用setter/constructor进行设置。 即:

执行此操作时:

ac = new StorageClient();
您将失去spring的上下文,因为您正在该上下文之外创建一个新实例。CloudBlobClient、MSITokenerator和变量storageAccountName、storageAccountKey中的bean不会被注入

您可以使用@Component注释StorageClient

因此,由于您将其打包为jar,因此在主项目中,您必须确保@ComponentScan包含StorageClient所在的路径

然后你可以做:

@Autowired
private StorageClient storageClient;
在主项目中。

执行此操作时:

ac = new StorageClient();
您将失去spring的上下文,因为您正在该上下文之外创建一个新实例。CloudBlobClient、MSITokenerator和变量storageAccountName、storageAccountKey中的bean不会被注入

您可以使用@Component注释StorageClient

因此,由于您将其打包为jar,因此在主项目中,您必须确保@ComponentScan包含StorageClient所在的路径

然后你可以做:

@Autowired
private StorageClient storageClient;

在您的主项目中。

我尝试了[@]自动连线私有CloudBlobClient CloudBlobClient;[@]自动连线专用MSITokenerator MSITokenerator;在main类中,我也得到了cloudBlobClient=null和msiTokenGenerator=null。为什么这些都是空的你所说的主类是什么意思?它应该在配置类中为:@Beanname={storageClient}我尝试了[@]自动连线私有CloudBlobClient CloudBlobClient;[@]自动连线专用MSITokenerator MSITokenerator;在main类中,我也得到了cloudBlobClient=null和msiTokenGenerator=null。为什么这些都是空的你所说的主类是什么意思?它应该位于配置类中:@Beanname={storageClient}还有一个疑问,我有bean MSITokenGenerator,它是根据某些条件创建的。现在,当我在另一个类中自动连接MSITokenerator时,如果没有创建MSITokenerator bean,我会得到一些错误。因此,某种条件自动连接是可能的,即如果bean存在,只需自动连接。您可以执行@Autowiredrequired=false,如果bean msitonkGenerator不存在,则将为null,但不会有例外。还有一个疑问,我有bean msitonkGenerator,它是根据某些条件创建的。现在,当我在另一个类中自动连接MSITokenerator时,如果没有创建MSITokenerator bean,我会得到一些错误。因此,某种条件自动连接是可能的,即如果bean存在,只需自动连接。您可以执行@Autowiredrequired=false,如果bean MSITokenGenerator不存在,则将为null,但不会出现异常。