九点钟☆方向

记录成长点滴

欢迎来到我的个人博客~


Spring笔记(三) Spring 基于注解的IOC配置

目录

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

准备工作

  1. 引入依赖

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    

    使用注解之前首先要确保依赖的jar中存在spring-aop这个jar包

    2

  2. 创建 spring 的 xml 配置文件并开启对注解的支持

<?xml version="1.0" encoding="UTF-8"?>
<!--导入约束时需要多导入一个 context 名称空间下的约束-->
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:context="http://www.springframework.org/schema/context"
  	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			 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 创建容器时要扫描的包 -->
    <context:component-scan base-package="com.mg"></context:component-scan>
</beans>

注解

曾经的xml配置

<bean id="XXX" class="XXX" scope="XXX" init-method="XXX" destroy-method="XXX">
	<property name="XX" value="XX"></property>
</bean>

用于创建对象的

相当于:

< bean id=""   class="">
@Component

作用:把资源让 spring 来管理。相当于在 xml 中配置一个 bean。 属性: value:指定 bean 的 id。如果不指定 value 属性,默认 bean 的 id 是当前类的类名且首字母小写。

@Controller @Service @Repository

他们三个注解都是针对@Component 的衍生注解,他们的作用及属性都是一模一样的。只不过是Spring提供了更加明确的语义化,明确三层使用注解,使三层对象更清晰 @Controller: 一般用于表现层的注解。 @Service: 一般用于业务层的注解。 @Repository: 一般用于持久层的注解。

//细节:如果注解中有且只有一个属性要赋值时,且名称是 value, value 在赋值是可以不写。
//@Service(value = "accountService")
@Service("accountService")
public class AccountServiceImpl implements IAccountService {    }

用于注入数据的

相当于:

<property name="" ref="">
    或者
<property name="" value=""> 
@Autowired

作用:自动按照类型注入。当使用注解注入属性时, set 方法可以省略。它只能注入其他 bean 类型。当有多个 类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到 就报错。 出现位置可以在变量上也可以在方法上

@Qualifier

作用:在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。它在给类成员注入时不能独立使用,必须和 @Autowire 一起使用;但是给方法参数注入时,可以独立使用。 属性: value:指定 bean 的 id。

@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    @Autowired
    @Qualifier("accountDao1")
    private IAccountDao accountDao;
}

比如配置文件中配置了两个dao
    <!-- 配置 dao -->
    <bean id="accountDao1" class="com.mg.dao.impl.AccountDaoImpl"></bean>
    <bean id="accountDao2" class="com.mg.dao.impl.AccountDaoImpl"></bean>
@Resource

作用:直接按照 Bean 的 id 注入。它也只能注入其他 bean 类型。 属性: name:指定 bean 的 id。

@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    @Resource(name = "account2")
    private IAccountDao accountDao;
}
@Value

作用:注入基本数据类型和 String 类型数据的 属性: value:用于指定值

他可以使用spring的EL表达式SPEL,${表达式} 取值

用于改变作用范围的:

相当于:

<bean id="" class="" scope=""></bean>
@Scope

作用:指定 bean 的作用范围,不写默认为singleton 属性: value:指定范围的值。 取值: singleton prototype request session globalsession

@Service("accountService")
@Scope("prototype")
public class AccountServiceImpl implements IAccountService {    }

和生命周期相关的

相当于:

 <bean id="" class="" init-method="" destroy-method="" />
@PostConstruct

作用:用于指定初始化方法

@PreDestroy

作用:用于指定销毁方法

@Service("accountService")
@Scope("prototype")
public class AccountServiceImpl implements IAccountService {
    @PostConstruct
    public void init() {
        System.out.println("初始化方法");
    }
    @PreDestroy
    public void destory() {
        System.out.println("销毁方法");
    }
}

关于 Spring 注解和 XML 的选择问题

注解的优势:配置简单,维护方便(我们找到类,就相当于找到了对应的配置)。 XML 的优势:修改时,不用改源码。不涉及重新编译和部署

Spring 管理 Bean 方式的比较 :

3

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦