ins*_*ect 2 java spring components autowired
请处理这个基本问题。
我曾经使用 @Autowired 注释,其中可以使用该类的键/值格式为属性(类变量)赋予一个值。
<bean id="class" class="a.b.c.Class" lazy-init="true">
<property name="var1" value="${var1}" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我将 Class 更改为带有不需要自动装配的组件扫描选项的 @Component 。我现在如何添加属性变量?
另外,我不想在编写 Junit 时遇到麻烦。
谢谢,
如果你是从一个属性文件思维特性,检查@PropertySource和@Value注释。
@Component
@PropertySource("classpath:myProps.properties")
public class MyComponent {
@Value("${some.property}")
private String valueFromProperty;
// You can also use environment
@Autowired
private Environment env;
public void someMethod() {
String prop = env.getProperty("some.property");
MyBean bean = new MyBean();
bean.setProp(prop);
return bean;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2648 次 |
| 最近记录: |