我们使用下面的代码为Spring bean注入属性文件中的属性.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:/my.properties"/>
</bean>
<bean id="blah" class="abc">
<property name="path" value="${the.path}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
有没有办法以编程方式访问属性?我正在尝试做一些没有依赖注入的代码.所以我想要一些像这样的代码:
PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer();
props.load("classpath:/my.properties");
props.get("path");
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Spring 3中配置一个基于Annotation配置的类,它将原始值作为构造函数参数:
@Component
class MyBean {
MyBean(String arg1, String arg2) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
和这样的应用程序上下文:
<beans [...]>
<context:component-scan base-package="com.example" />
<context:property-override location="/WEB-INF/example.properties" />
</beans>
Run Code Online (Sandbox Code Playgroud)
我正在尝试找到一些方法来指定构造函数参数应该从属性文件中获取.显然,这适用于采用常规bean(例如MyClass(Bean bean1, OtherBean bean2))但只是属性的构造函数?
我也尝试使用Spring 3的@Value注释和值的EL表达式来注释构造函数参数@Value("#{prop.Prop1}") arg1,但是,这似乎也不起作用.
我有一个 Spring MVC REST 控制器类,它有一个通过 @Value 注入的私有实例布尔字段,
@Value("${...property_name..}")
private boolean isFileIndex;
Run Code Online (Sandbox Code Playgroud)
现在要对这个控制器类进行单元测试,我需要注入这个布尔值。
我该如何做到这一点MockMvc?
我可以使用反射,但MockMvc实例没有给我传递给Field.setBoolean()方法的底层控制器实例。
测试类运行时无需模拟或注入此依赖项,其值始终为false。我需要将其设置true为覆盖所有路径。
设置如下所示。
@RunWith(SpringRunner.class)
@WebMvcTest(value=Controller.class,secure=false)
public class IndexControllerTest {
@Autowired
private MockMvc mockMvc;
....
}
Run Code Online (Sandbox Code Playgroud) 我在我的spring上下文xml中有以下xml配置,我使用基于注释的方法非常少,无法弄清楚如何使用注释来表示以下内容,需要帮助.
<bean id="myPolicyAdmin" class="org.springframework.security.oauth2.client.OAuth2RestTemplate">
<constructor-arg>
<bean class="org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails">
<property name="accessTokenUri" value="${accessTokenEndpointUrl}" />
<property name="clientId" value="${clientId}" />
<property name="clientSecret" value="${clientSecret}" />
<property name="username" value="${policyAdminUserName}" />
<property name="password" value="${policyAdminUserPassword}" />
</bean>
</constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)
在我的java类(策略管理器)中,它被称为如下,我实际上是在引用一个示例并尝试将其转换为所有注释.
@Autowired
@Qualifier("myPolicyAdmin")
private OAuth2RestTemplate myPolicyAdminTemplate;
Run Code Online (Sandbox Code Playgroud)
编辑:我尝试创建一个bean,org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails但不知道如何设置其属性以及如何作为构造函数args访问它myPolicyAdminTemplate