Spring Boot的三种配置方式
1,传统xml配置
2,基于java注解配置
3,基于类的java config
一,传统的xml配置
public interface BeanFactory {
public void beanTest();
}
public class BeanFactoryImpl implements BeanFatory {
@Override
public void beanTest(){
system.out.println("传统的xml配置")
}
}
在applicationContext.xml文件中添加
<bean id="beanFactoryImpl" class="com.*.*.BeanFactoryImpl " />
public class TestBean {
@Test
public void test(){
ApplicationContext cx = new ClassPathXmlApplicationContext(applicationContext.xml);
BeanFactory mm = (BeanFactory )cx.getBean("beanFactoryImpl");
mm.beanTest();
}
}
二,基于java注解配置
在applicationContext.xml中添加如下一行,作用是自动扫描base-package下的注解
<context:component-scan base-package="com.setgge">
向@Service、@Repository、@Controller等添加这些注解的类,就实现了bean的配置
三,基于类的java config
利用@Configuration 和@Bean的结束完成bean的配置
@Configuration
public calss BeanConfig {
@Bean
public BeanFactory beanFactory(){
return new BeanFactoryImpl();
}
}