Bean的申明方式
在A中实现
FactoryBean
接口来完成B的初始化,达到Bean的声明效果1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class TestA implements InitializingBean, FactoryBean<TestB> {
private int a;
private TestB testB;
public void afterPropertiesSet() throws Exception {
this.testB = new TestB();
}
public TestB getObject() throws Exception {
return this.testB;
}
public Class<?> getObjectType() {
return (this.testB != null ? this.testB.getClass() : TestB.class);
}
public boolean isSingleton() {
return true;
}
}
public class TestB {
private int b;
}
public class BeanConfig {
public TestA testA() {
return new TestA();//此时B已经声明好了
}
}
如何使用
@Value
初始化类静态属性1
2
3
4
5
6
7
8
9
10
11
12
13
public class TestC {
public static String s;
"${test.s}") (
public void setS(String sVal) {//使用set方法注入,方法需要是非静态的
s = sVal;
}
public static String getS() {
return s;
}
}properties乱码解决