在我的一个班级中有一个public static String成员,我需要在中设置这个值 applicationContext.xml!也就是说,我们可以为这个静态属性注入一个值吗?
我在Spring配置文件中定义了一个bean,
<bean id="accountConfigFile" class="java.lang.String">
<constructor-arg index="0" type="java.lang.String" value="/account.properties"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
然后我将此bean连接到AccountHelper类中的字段:
@Autowired
@Qualifier("accountConfigFile")
private static String configFilename;
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在构造函数中访问它时,我得到NullPointerException,因为它为null:
public Class AccountHelper {
private Properties properties;
@Autowired
@Qualifier("accountConfigFile")
private static String configFilename;
public AccountHelper() {
properties = new Properties();
InputStream is = null;
try
{
is = getClass().getResourceAsStream(configFilename);
properties.load(is);
is.close();
} catch (Exception e)
{
......
}
}
......
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我弄清楚为什么会这样吗?
非常感谢!
我正在尝试从 Spring Boot 的属性文件中获取价值。 application.properties文件位于资源文件夹下,及其内容;
TEST=someText
Run Code Online (Sandbox Code Playgroud)
代码是;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:application.properties")
public class Bb8Application {
@Value("${TEST}")
static String someString;
public static void main(String[] args) {
System.out.print(someString);
}
}
Run Code Online (Sandbox Code Playgroud)
结果我得到 NULL 而不是“someText”。有什么我想念的吗?
我有一个属性文件config.properties,它使用spring属性占位符配置.这是我在spring配置文件中配置的方式:
<context:property-placeholder location="classpath:properties/config.properties"/>
Run Code Online (Sandbox Code Playgroud)
现在我需要使用@Value注释将其值设置为静态字段.
@Value("${outputfilepath}")
private static String outputPath;
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我的xml看起来像这样:
<bean name="subscriberStore" class="java.util.HashSet" scope="singleton"/>
Run Code Online (Sandbox Code Playgroud)
我有以下代码:
@Value("#{subscriberStore}")
private static HashSet<Subscriber> subscriberStore;
Run Code Online (Sandbox Code Playgroud)
但是,在上面的类中,当我在subscriberStore上调用方法时,我得到一个空指针异常.我尝试过使用@Autowired而@Resource不是@Value ..上面但它没有任何区别.
任何人都有任何想法为什么订户商店没有初始化?
谢谢!
我试图通过 Spring @Value 注释将一些属性值注入到变量中,但我得到空值。我尝试了不同的配置和技巧,但它不起作用。想想今天之前一切都运转正常。我不知道我改变了什么才能把事情搞砸。
这是我的java类:
@Component
@ConditionalOnProperty(prefix = "studioghibli", name = "get")
public class StudioGhibliRestService {
@Value("${studioghibli.basepath}")
private static String BASE_PATH;
@Value("${studioghibli.path}")
private static String PATH;
@Value("${studioghibli.protocol:http}")
private static String PROTOCOL;
@Value("${studioghibli.host}")
private static String HOST;
private static String BASE_URI = PROTOCOL.concat("://").concat(HOST).concat(BASE_PATH).concat(PATH);
@Autowired
StudioGhibliRestConnector connector;
public List<StudioGhibliFilmDTO> findAllFilms() throws SipadContenziosoInternalException {
var response = connector.doGet(BASE_URI, null, null);
if (!response.getStatusCode().is2xxSuccessful() || !response.hasBody()) {
throw new SipadContenziosoInternalException(Errore.INTERNAL_REST_ERROR, "FindAll(), microservizio ".concat(BASE_URI), null);
}
return (List<StudioGhibliFilmDTO>) response.getBody();
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,该类用 @Component 进行注释,因为我需要将其用作 @Service 层,以便在我的业务逻辑中进行休息调用。该类还以属性条件进行注释... …