Spring 概述和依赖注入(DI)

Spring概述

  • Spring框架是一个轻量级的企业级开发的一站式解决方案
  • Spring框架主要提供Ioc容器、AOP、数据访问、WEB开发、消息、测试等相关技术的支持
  • 每一个被Spring管理的Java对象都称之为Bean

Spring的模块

核心容器(core container)

  • Spring-Core:核心工具类,Spring其他模块大量使用Spring-Core
  • Spring-Beans:Spring定义Bean的支持
  • Spring-Context:运行时Spring容器
  • Spring-Context-Support:Spring容器对第三方包的支持
  • Spring-Expression:使用表达式语言在运行时查询和操作对象

AOP

  • Spring-AOP:基于代理的AOP支持
  • Spring-Aspects:基于AspectJ的AOP支持

消息(Messaging)

  • Spring-Messaging:对消息架构和协议的支持

Web

  • Spring-Web:提供基础的Web集成功能,在Web项目中提供Spring的容器
  • Spring-Webmvc:提供基于Servlet的Spring MVC
  • Spring-WebSocket:提供WebSocket功能
  • Spring-Webmvc-Portlet:提供Portlet环境支持

数据访问/集成(Data Access/Integration)

  • Spring-JDBC:提供以JDBC访问数据库的支持
  • Spring-TX:提供编程式和声明式的事务支持
  • Spring-ORM:提供对对象/关系映射技术的支持
  • Spring-OXM:提供对对象/xml映射技术的支持
  • Spring-JMS:提供对JMS的支持

Spring 的生态

Spring Boot
| 使用默认开发配置来实现快速开发
Spring XD
| 用来简化大数据应用开发
Spring Cloud
| 为分布式系统开发提供工具集
Spring Data
| 对主流的关系型和NoSQL数据库的支持
Spring Integration
| 通过消息机制对企业集成模式(EIP)的支持
Spring Batch
| 简化及优化大量数据的批处理操作
Spring Security
| 通过认证和授权保护应用
Spring HATEOAS
| 基于HATEOAS原则简化REST服务开发
Spring Social
| 与社交网络API(如Facebook、新浪微博等)的集成
Spring AMQP
| 对基于AMQP的消息支持
Spring Mobile
| 提供对手机设备检测的功能,给不同的设备返回不同的页面的支持
Spring for Android
| 主要提供在Android上消费RESTful API的功能
Spring Web Flow
| 基于Spring MVC提供基于向导流程式的Web应用开发
Spring LDAP
| 简化使用LDAP开发
Spring Session
| 提供一个API及实现来管理用户会话信息

Spring 基础配置

Spring 框架四大原则

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

依赖注入

  • 依赖注入(DI)和控制反转(IoC)在Spring环境下是等同的概念。
  • 控制反转是通过依赖注入实现的。
  • 依赖注入指的是容器负责创建对象和维护对象间的依赖关系,而不是通过对象本身负责自己的创建和解决自己的依赖。
  • 依赖注入的目的是为了解耦,体现‘组合’的理念。(如果要一个类实现某个功能,可以继承具有此功能的父类,也可以组合另外一个有此功能的类。子类继承父类耦合度高,所以采用‘组合’其他类的方式来降低耦合)。
  • Spring IoC容器负责创建Bean、注入Bean。Spring提供xml、注解、Java配置、groovy配置的方式实现Bean的创建和注入。

声明Bean的注解

  • @Component mei有明确的角色

  • @Service 在业务逻辑层(Service层)使用

      package ch1.di;
      import org.springframework.stereotype.Service;
    
      @Service //使用@Service注解声明当前FunctionService类是Spring管理的一个bean
      public class FunctionService {
          public String sayHello(String word){
              return "Hello "+word+" !";
          }
      }
    
  • @Repository 在数据访问层(dao层)使用

  • @Controller 在展现层(MVC->Spring MVC)使用

这4个注解是等效的,可根据需要选用。

注入Bean的注解

  • @Autowired: Spring 提供的注解

      package ch1.di;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
    
      @Service
      public class UseFunctionService {
      	@Autowired // 使用@Autowired将FunctionService的实体Bean注入到UseFunctionService中,让UseFunctionService具备FunctionService的功能
      	FunctionService functionService;
    
          public String Sayhello(String word){
              return functionService.sayHello(word);
          }
      }
    
  • @Inject: JSR-330提供的注解

  • @Resource: JSR-250提供的注解

@Autowired、@Inject、@Resource可注解在set方法或者属性上。

配置类
package ch1.di;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration // 声明当前是一个配置类
@ComponentScan("ch1.di") // 使用@ComponentScan自动扫描包名下所有使用@Component、@Service、@Repository、@Controller的类,并注册为Bean
public class DiConfig {
}
运行
package ch1.di;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args){
        // 使用AnnotationConfigApplicationContext作为Spring容器,接受输入一个配置类作为参数
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfig.class);
        // 获得声明配置的UseFunctionService的Bean
        UseFunctionService useFunctionService = context.getBean(UseFunctionService.class);
        System.out.println(useFunctionService.Sayhello("helloword"));
        context.close();
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值