传统方式:myconfig.xml
<bean class="...A">
<property name="f1" value="val1"/>
<property name="f2" value="val2"/>
</bean>
基于java的配置方式-新方式:MyConfig.java
package com.pp.config;
public class A {
private String f1;
private String f2;
private B b;
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
System.out.println("A-setB");
}
public String getF1() {
return f1;
}
public void setF1(String f1) {
System.out.println("A-setF1");
this.f1 = f1;
}
public String getF2() {
return f2;
}
public void setF2(String f2) {
this.f2 = f2;
System.out.println("A-setF2");
}
}
package com.pp.config;
public class B {
}
package com.pp.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
@Bean
public B b() {
return new B();
}
@Bean
public A a(B b) {
A a = new A();
a.setF1("val1");
a.setF2("val2");
//组装setB(b)
a.setB(b);
return a;
}
}
打上Configuration就是配置类了
@Bean就是配置文件里的Bean标签