Spring3.0开启了纯注解开发模式,使用Java类替代配置文件,开启了Spring快速开发赛道
Java类代替了Spring的核心配置文件
@Configuration
@ComponentScan({"com.itheima"})
public class SpringConfig {
}
@Configruation代替了xml/文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
</beans>
@ComponentScan("com.itheima")代替了
<context:component-scan base-package="com.itheima" />
应用程序app中使用
AnnotationConfigApplicationContext(SpringConfig.class)
public class app {
public static void main(String[] args) {
ApplicationContext cxk = new AnnotationConfigApplicationContext(SpringConfig.class);
BookDao bookDao = (BookDao) cxk.getBean("bookDao");
BookService bookService = cxk.getBean(BookService.class);
System.out.println(bookDao);
System.out.println(bookService);
}
}
总结
@Configuration注解用于设定当前类为配置类
@ComponentScan注解用于定义扫描路径,此注解只能添加一次,多个数据用数组的形式
@ComponentScan({"com.itheima.dao","com.itheima.service"})
创建Ioc容器用
AnnotationConfigApplicationContext(SpringConfig.class)