gpt4 book ai didi

java - Autowiring 定义为 in Spring 的集合

转载 作者:行者123 更新时间:2023-11-29 03:23:12 26 4
gpt4 key购买 nike

我想拥有 Set<LocationResolver>像这样在 Spring 上下文中定义:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="org.foo"/>

<!-- Artifact LocationResolvers -->
<bean id="fsLocationResolver" class="org.foo.storage.resolvers.FSLocationResolver" />
<bean id="memoryLocationResolver" class="org.foo.storage.resolvers.InMemoryLocationResolver" />

<!-- The Set<LocationResolver> -->
<util:set id="resolvers" value-type="org.foo.storage.resolvers.LocationResolver">
<ref bean="memoryLocationResolver"/>
<ref bean="fsLocationResolver"/>
</util:set>

<bean id="configurationManager"
class="org.foo.configuration.ConfigurationManager"
init-method="init">
</bean>

<bean id="artifactResolutionService" class="org.foo.resolvers.ArtifactResolutionService">
<property name="resolvers" ref="resolvers"/>
</bean>

</beans>

我的 ArtifactResolutionService :

package org.foo.storage.resolvers;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ArtifactResolutionService
{

@Resource(name = "resolvers")
private Set<LocationResolver> resolvers;


public Set<LocationResolver> getResolvers()
{
return resolvers;
}

public void setResolvers(Set<LocationResolver> resolvers)
{
this.resolvers = resolvers;
}

}

我的 ConfigurationManager类:

package org.foo.configuration;

import org.foo.resource.ConfigurationResourceResolver;
import org.foo.storage.Storage;
import org.foo.storage.resolvers.ArtifactResolutionService;
import org.foo.storage.resolvers.LocationResolver;
import org.foo.xml.parsers.ConfigurationParser;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

@Component
@Scope ("singleton")
public class ConfigurationManager
{

private static final Logger logger = LoggerFactory.getLogger(ConfigurationManager.class);

private String configurationPath;

private Configuration configuration;

@Autowired
private ConfigurationResourceResolver configurationResourceResolver;

@Autowired
private ArtifactResolutionService artifactResolutionService;


public ConfigurationManager()
{
}

public void init()
throws IOException
{
Resource resource = configurationResourceResolver.getConfigurationResource("etc/configuration.xml",
"repository.config.xml",
"etc/configuration.xml");

logger.info("Loading configuration from " + resource.toString() + "...");

ConfigurationParser parser = new ConfigurationParser();

configuration = parser.parse(resource.getInputStream());
configuration.setResource(resource);

dump();
}

public void dump()
{
System.out.println("Configuration version: " + configuration.getVersion());
System.out.println("Listening on port: " + configuration.getPort());

System.out.println("Loading storages...");
for (String storageKey : configuration.getStorages().keySet())
{
System.out.println(" -> Storage: " + storageKey);
Storage storage = configuration.getStorages().get(storageKey);
for (String repositoryKey : storage.getRepositories().keySet())
{
System.out.println(" -> Repository: " + repositoryKey);
}
}

//
// This throws a NullPointerException, as the resolvers are null.
//
System.out.println("Loading resolvers...");
for (LocationResolver resolver : artifactResolutionService.getResolvers())
{
System.out.println(" -> " + resolver.getClass());
}
}

public void storeConfiguration(Configuration configuration, String file)
throws IOException
{
ConfigurationParser parser = new ConfigurationParser();
parser.store(configuration, file);
}

public Configuration getConfiguration()
{
return configuration;
}

public void setConfiguration(Configuration configuration)
{
this.configuration = configuration;
}

public String getConfigurationPath()
{
return configurationPath;
}

public void setConfigurationPath(String configurationPath)
{
this.configurationPath = configurationPath;
}

public ArtifactResolutionService getArtifactResolutionService()
{
return artifactResolutionService;
}

public void setArtifactResolutionService(ArtifactResolutionService artifactResolutionService)
{
this.artifactResolutionService = artifactResolutionService;
}

}

resolvers , 然而,最终是 null我似乎无法弄清楚这一点。有什么明显的地方我做错了吗?

非常感谢!

最佳答案

有两件事值得警惕。

首先你有这个

<context:component-scan base-package="org.foo"/>

package org.foo.storage.resolvers;

@Component
public class ArtifactResolutionService

这意味着您正在隐式创建一个ArtifactResolutionService bean 。你还有

<bean id="artifactResolutionService" class="org.foo.resolvers.ArtifactResolutionService">
<property name="resolvers" ref="resolvers"/>
</bean>

显式创建第二个 ArtifactResolutionService bean(bean 定义可能会相互覆盖,因为它们具有相同的 id)。在这两种情况下,您都有办法注入(inject) Set解析器。一个是 @Autowired , 另一个是 <property> .

其次,你的Spring配置不可能注入(inject)null在托管 bean 中。

您可能在 bean 的生命周期中尝试在错误的时间使用该字段,此时该字段尚未被注入(inject)。

看来你有循环依赖。 Spring 尝试注入(inject) ConfigurationManager进入 LocationResolver 之一在它 Autowiring 它的字段之前。为此,它需要调用 init-method .


注意带注解配置,我信这个

@Autowired
private Set<LocationResolver> resolvers;

实际上会拾取所有 LocationResolver bean ,不是你的Set bean 在这里定义

<!-- The Set<LocationResolver> -->
<util:set id="resolvers" value-type="org.foo.storage.resolvers.LocationResolver">
<ref bean="memoryLocationResolver"/>
<ref bean="fsLocationResolver"/>
</util:set>

如果你想要 Set bean,我会使用 @Resource

@Resource(name = "resolvers")
private Set<LocationResolver> resolvers;

关于java - Autowiring 定义为 <util :set> in Spring 的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22463052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com