spring 一般有两种配置文件模式 一种是通过XML来配置文件,另一种是通过Java代码来配置文件,如下
Java文件配置方式
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import implement.HelloWordImplement;import implement.HelloWordImplement2;
import minterface.HelloWord;
@Configuration
public class AppConfig {
@Bean(name = "helloBean")
public HelloWord helloWord() {
return new HelloWordImplement();
}
@Bean(name = "helloBean2")
public HelloWord helloWord2() {
return new HelloWordImplement2();
}
}
Java文件中用 @Configuration的方式告诉spring 这个类是一个配置文件,其中@Bean即时告诉spring下面的的方法需要通过setter注入的方式注入bean
其中上述配置文件等效于XML的
<bean id="helloWord" class="implement.HelloWordImplement">
</bean>
<bean id="helloWord2" class="implement.HelloWordImplement2">
</bean>