SSM之Spring系列(三)---- Spring 基于注解的 IoC 详解

注解配置和 xml 配置要实现的功能都是一样的,都是要降低程序间的耦合。只是配置的形式不一样。

Spring 基于注解的 IoC 详解

  • 我们先来看看之前的XML的配置:
    <bean id="accountService" class="com.cz.service.impl.AccountServiceImpl" scope="" init-method="" destroy-method="">
        <property name="" value="" ref=""></property>

    </bean>

上面的XML配置中有多个标签和属性,至于这些标签和属性的作用是啥,大家可以去看看上一篇文章。传送门

  • 接下来,Spring基于注解的IoC也分为四类:
  • 用于创建对象的:
    • 他们的作用就和在XML配置文件中编写一个<bean>标签的功能是一样的
  • 用于注入数据:
    • 他们的作用就和在XML配置文件中的<bean>标签中写一个<property>标签的作用是一样的。
  • 用于改变作用范围:
    • 他们的作用就和在<baen>标签中使用<scope>标签的作用是一样的。
  • 和生命周期相关
    • 他们的作用就和在<bean>标签中使用init-methoddestroy-method的作用是一样的。
  • 如果要使用注解的话,首先需要在配置文件中导入新的命名空间,并且指定要扫描的包。
<?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
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为context名称空间和约束中-->
    <context:component-scan base-package="com.cz"></context:component-scan>
</beans>

用于创建对象的注解

相当于:<bean id="" class="">

@Component 注解

  • 作用
    • 用于把当前类对象存入 Spring 容器中
  • 属性
    • value : 用于指定 Bean 的唯一标识 (Id)。如果没有写,默认值是当前类名,且首字母小写
  • 除了 @Component注解外,还有三个衍生注解也是用于创建对象,属性与 @Component 一样,见下表:
注解名称作用
@Controller表现层
@Service业务层
@Repository持久层

  他们三个的作用跟属性跟@Repository是一模一样的。这三个注解是 Spring 框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰。
  细节:如果注解中有且只有一个属性要赋值时,且名称是 value,value 在赋值是可以不写。

package com.cz.service.impl;

import com.cz.dao.AccountDao;
import com.cz.dao.impl.AccountDaoImpl;
import com.cz.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * 账户的业务层实现类
 */
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    public AccountServiceImpl(){
        System.out.println("对象创建了");
    }

    public void saveAccount(){
        accountDao.saveAccount();
        System.out.println("service中的saveAccount方法执行了");
    }
}

用于注入数据的注解

相当于:<property name=" " ref=" "> <property name="" value="">

@Autowired注解

  • 作用
    • 自动按照类型注入(By Type)
    • 容器首先用变量的类型进行查找,如果容器中有唯一一个 Bean 的类型匹配,那么就直接注入;如果容器中没有任何 Bean的类型与要注入的变量类型匹配,那么就报错;如果容器中有多个 Bean 的类型匹配,那么容器就会以变量的名称作为 Bean的id进行查找,找到匹配的则注入,未找到则报错。
  • 出现位置
    • 变量或者方法上
  • 细节
    • 当使用注解来注入属性时,set 方法可以省略
/**
 - 账户的业务层实现类
 */
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao = null;
    //......

对于上面的注解大家可以看看下图:
在这里插入图片描述
@Qualifier 注解

  • 作用

    • 按照类型注入的基础之上再按照名称注入。它在给类成员注入时不能单独使用,必须和 @Autowired``一起使用。但是在给方法参数注入时可以单独使用。
  • 属性
    -value : 用于指定注入 Bean 的唯一标识 (Id)

/**
 - 账户的业务层实现类
 */
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    @Qualifier("accountDao")
    private AccountDao accountDao = null;
	//.....

@Resource 注解

  • 作用

    • 自动按照名称注入(By Name),直接按照 Bean 的 Id 进行注入,它可以独立使用。
  • 属性

    • name : 指定注入 Bean 的 id
/**
 * 账户的业务层实现类
 */
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {

//    @Autowired
//    @Qualifier("accountDao")
    @Resource(name = "accountDao")
    private AccountDao accountDao = null;

以上三个注解都只能注入其他 Bean 类型的数据,而基本类型和 String 类型需要使用下面注解,另外,集合类型的注入只能通过XML来实现

@Value 注解

  • 作用
    • 注入基本类型和 String 类型数据
  • 属性
    • value: 用于指定数据的值。它可以使用 Spring 中的 SpEL( Spring 的 el 表达式,写法:${表达式})

用于改变作用范围的

相当于:<bean id="" class="" scope="">

@Scope 注解

  • 作用
    • 指定 Bean 的作用范围
  • 属性
    • value: 指定范围的取值。常用取值为singleton、prototype

和生命周期有关

相当于:<bean id="" class="" init-method="" destroy-method="" />

@PreDestroy 注解

  • 作用
    • 用于指定销毁方法

PostConstruct 注解

  • 作用
    • 用于指定初始化方法
@PostConstruct
public void init() {
    System.out.println("初始化方法执行了");
}

@PreDestroy
public void destroy() {
    System.out.println("销毁方法执行了");
}

XML 和 注解方式的比较

  • 注解的优势
    • 配置简单,维护方便
  • XML 的优势
    • 修改时无需修改源码,不涉及重新编译和部署

Spring 管理 Bean 方式的比较:

基于 XML 配置基于注解配置
Bean 定义<bean id = "..." class = "..." />@Component、@Controller、@Service、@Repository
Bean 名称通过id 或者 name属性指定@Component("user")
Bean 注入通过<property>或者 <constructor-arg>标签注入@Autowired、@Qualifier、@Resource
Bean 作用范围通过scope 属性指定@Scope
Bean 生命周期通过 init-method或者destroy-method 属性指定初始化和销毁方法@PostConstruct、@PreDestroy
Bean 适合场景Bean 来自于第三方,无法修改源码Bean 的实现类由自己开发

Spring基于注解的IoC到这就讲完了,接下来会同一个案例来看看XML方式实现和注解方式实现的差别。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值