02.Spring基础

Spring简史:

  • Spring1.x时代:xml配置
  • Spring2.x时代:注解配置(Jdk1.5引入注解),人们的选择是基本配置用xml,业务配置用注解
  • Spring3.x时代:Java配置
  • Spring4.x和Spring Boot都推荐使用Java配置

Spring框架本身有四大原则:

  • 使用POJO进行轻量级和最小侵入式开发。
  • 通过依赖注入和接口编程实现松耦合。
  • 通过AOP和默认习惯进行申明式编程。
  • 使用AOP和模板(template)减少模式化代码。

开始写代码吧

  • 此处未使用书中的4.1.6,而是用公司工程中的4.3.7,要跟上时代的节奏啊~
<properties>
        <spring-framework.version>4.3.7.RELEASE</spring-framework.version>
</properties>
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-framework.version}</version>
 </dependency>
  • 引用这一个jar,项目会出现如下:
引用的依赖
  • 一共7个包,这7个包的代码后面再研究。

  • Spring IoC容器(ApplicationContext)负责创建Bean,并通过容器将功能类Bean注入到你需要的Bean中。

声明Bean的注解
  1. @Component:没有明确的角色
  2. @Controller : Spring MVC使用
  3. @Service : service层使用
  4. @Repository :dao层使用
  • 都是声明当前类是Spring管理的一个Bean,4个注解等效。
注入Bean的注解
  1. @Autowired:spring提供的注解
  2. @Inject:JSR-330提供的注解
  3. @Resource:JSR-250提供的注解

依赖注入

  • github代码:
    Spring基础01依赖注入
  • 说明:使用@ComponentScan,自动扫描包名下所有使用@Component、@Controller 、@Service、@Repository的类,并注册为Bean。

Java配置

  • github代码:
    Spring基础02Java配置Bean
  • 说明:此处代码没有使用@Service声明Bean,也没用使用@Autowired注入Bean。
  • 使用@Bean注解声明当前方法FunctionService的返回值是一个Bean, Bean的名称是方法名。
  • 在Spring容器中,只要容器中存在某个Bean,就可以在另外一个Bean的声明方法的参数中注入。

AOP

  • aop:面向切面编程,相对于OOP(面向对象编程)
  • spring支持AspectJ的注解式切面编程
    1. 使用@Aspect声明是一个切面
    2. 使用@After、@before、@Around定义建言(advice),可直接将拦截规则(切点)PointCut作为参数,使用@PointCut定义拦截规则。
    3. 其中符合条件的每一个被拦截处为连接点(JoinPoint)
  • 基于注解拦截能够很好的控制要拦截的粒度和获得更丰富的信息。
<!-- spring aop支持 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <!-- aspectj支持 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.6</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.5</version>
        </dependency>
  • github代码:
    Spring基础03AOP
  • 代码解释:
    • 注解本身是没有功能的,就和xml一样。注解和xml都是一种元数据,元数据即解释数据的数据,这就是所谓配置。注解的功能来自用这个注解的地方。
    • 通过@Aspect注解声明一个切面。
    • 通过@Component让此切面成为Spring容器管理的Bean。
    • 通过@PointCut注解声明切点。
    • 通过@After注解声明一个建言,并使用切点。
    • 通过反射可获得注解上的属性,然后做日志记录相关的操作。
    • 通过@Before注解声明一个建言,此建言直接使用拦截规则作为参数。
    • @EnableAspectJAutoProxy开启Spring对AspectJ的支持。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,324评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,052评论 6 342
  • 什么是Spring Spring是一个开源的Java EE开发框架。Spring框架的核心功能可以应用在任何Jav...
    jemmm阅读 16,635评论 1 133
  • 做这个东西的话,其实主要是因为一件事,我会比较经常食用手机迅雷下载电影,每次开启迅雷,都会默认来一段广告,这种强制...
    HaloMartin阅读 4,483评论 0 2
  • We like our wine to be dry, don’t we? Unless, of course, ...
    c2dfe2ef89b9阅读 1,079评论 0 0