学习 Spring 基于注解的IOC配置一篇文章就够了

注解配置和 xml 配置要实现的功能都是一样的,都是要降低程序间的耦合,只是配置的形式不一样
关于实际的开发中到底使用xml还是注解,每家公司有着不同的使用习惯。所以这两种配置方式我们都需要掌握。

适用注解时的bean.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
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--告知Spring在创建容器时,要扫描的包,配置所需要的标签不是在beans的约束中
    而是一个名为context的名称空间和约束中-->
    <context:component-scan base-package="mybatis"></context:component-scan>

</beans>

这种context配置是默认扫描该目录文件下的所有注解,也可以关闭默认扫描,指定哪些扫描内容注解:

<!--示例 1 use-default-filters="false" 表示现在不使用默认 filter,自己配置 filter context:include-filter ,设置扫描哪些内容-->
<context:component-scan base-package="mybatis" use-defaultfilters="false">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!--示例 2 下面配置扫描包所有内容context:exclude-filter: 设置哪些内容不进行扫描-->
<context:component-scan base-package="com.atguigu">
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

一、用于创建对象的注解

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

(1)@Component:把资源让 spring 来管理,相当于在 xml 中配置一个 bean

value:指定 bean 的 id。如果不指定 value 属性,默认 bean 的 id 是当前类的类名,首字母小写
@Component("accountDao")
public class AccountDaoImpl implements AccountDao {
}

(2)@Controller@Service@Repository
这三个注解都是针对一个的衍生注解,他们的作用及属性都是一模一样的,他们只不过是提供了更加明确的语义化。

@Controller: 一般用于表现层的注解。
@Service: 一般用于业务层的注解。
@Repository: 一般用于持久层的注解。
细节:如果注解中有且只有一个属性要赋值时,且名称是 value, value 在赋值是可以不写。
// @Repository()当不指定value时默认的id是当前的类名:accountDaoImpl,首字母小写
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
    @Override
    public void saveAccount() {
        System.out.println("保存了账户!!!");
    }
}

二、注入数据

作用就和在xml配置文件中的bean标签中写一个<property>标签的作用是一样的

这三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现。另外,集合类型的注入只能通过XML来实现。

(1)@Autowired
自动按照类型注入,当使用注解注入属性时, set 方法可以省略。它只能注入其他 bean 类型。当有多个类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到就报错。 出现位置,可以是变量上,也可以是方法上
细节:在使用注解注入时,set方法就不是必须的了
在这里插入图片描述
(2)@Qualifier
在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。它在给字段注入时不能独立使用,必须和@Autowire 一起使用;但是给方法参数注入时,可以独立使用。
属性:value:指定 bean 的 id。
在这里插入图片描述
(3)@Resource
直接按照 Bean 的 id 注入,它也只能注入其他 bean 类型。
属性:name:指定 bean 的 id
如果在引入该注释时,缺乏相关依赖,需要在pom.xml中单独引入相关配置:

<!--该依赖主要是针对 @Resource 使用-->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
</dependency>

在这里插入图片描述

三、用于改变作用范围

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

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

在这里插入图片描述

四、生命周期

他们的作用就和在bean标签中使用init-methoddestroy-methode的作用是一样的
PreDestroy作用:用于指定销毁方法
PostConstruct作用:用于指定初始化方法

@Service("accountService")
@Scope("prototype")//设置作用范围
public class AccountServiceImpl implements AccountService {
    @Resource(name = "accountDao")
    private AccountDao accountDao;
    @PostConstruct
    public void init(){
        System.out.println("初始化方法执行了");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("销毁方法执行了");
    }

    @Override
    public void saveAccount() {
        accountDao.saveAccount();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南淮北安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值