4.5.1 XML方式配置总结
- 所有内容写到xml格式配置文件中
- 声明bean通过<bean标签
- <bean标签包含基本信息(id,class)和属性信息 <property name value / ref
- 引入外部的properties文件可以通过<context:property-placeholder
- IoC具体容器实现选择ClassPathXmlApplicationContext对象
4.5.2 XML+注解方式配置总结
- 注解负责标记IoC的类和进行属性装配
- xml文件依然需要,需要通过<context:component-scan标签指定注解范围
- 标记IoC注解:@Component,@Service,@Controller,@Repository
- 标记DI注解:@Autowired @Qualifier @Resource @Value
- IoC具体容器实现选择ClassPathXmlApplicationContext对象
4.5.3 完全注解方式配置总结
- 完全注解方式指的是去掉xml文件,使用配置类 + 注解实现
- xml文件替换成使用@Configuration注解标记的类
- 标记IoC注解:@Component,@Service,@Controller,@Repository
- 标记DI注解:@Autowired @Qualifier @Resource @Value
- <context:component-scan标签指定注解范围使用@ComponentScan(basePackages = {"com.atguigu.components"})替代
- <context:property-placeholder引入外部配置文件使用@PropertySource({"classpath:application.properties","classpath:jdbc.properties"})替代
- <bean 标签使用@Bean注解和方法实现
- IoC具体容器实现选择AnnotationConfigApplicationContext对象
搭建测试环境
nfig(locations = {"classpath:spring-context.xml"}) //指定配置文件xml
@SpringJUnitConfig(value = {BeanConfig.class}) //指定配置类
public class Junit5IntegrationTest {
@Autowired
private User user;
@Test
public void testJunit5() {
System.out.println(user);
}
}
//@SpringJUnitConfig(locations = {"classpath:spring-context.xml"}) //指定配置文件xml
@SpringJUnitConfig(value = JavaConfig.class) //指定配置类
public class AnnotationTest {
@Autowired
private StudentController studentController;
@Test
public void test(){
System.out.println("studentController" + studentController);
}
}
网友评论