Spring Frameword - 10-纯注解开发

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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容