Spring-3-Bean之注解

本文详细介绍了Spring框架中注解的使用,包括如何让项目支持注解,Bean的实例化、作用域、属性注入,以及@Autowired、@Qualifier和@Resource的区别和使用场景。同时,还探讨了混合使用XML+注解的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、注意事项

  • 需要导入spring-aop的包。

二、让项目支持注解

  1. 在spring的配置文件中添加约束
  2. 在spring的配置文件中配置注解扫描
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置扫描的包下面的类、方法、属性的注解 -->
    <context:component-scan base-package="com.seven.bean"/>

    <!-- 配置扫描属性的注解,上面的那个配置了过后,这个就没有配置的意义了 -->
    <context:annotation-config/>
</beans>

在这里插入图片描述

三、Bean的实例化

在类上使用注解:
@Component 最基本的
@Controller 控制层
@Service 业务层
@Repository 持久层
这是个在实例化Bean中没有区别,只是习惯上是这样写,方便阅读。
例:
在这里插入图片描述
这四个都有一个参数value,相当于xml中bean的id。

四、Bean的作用域

在类上使用注解:
@Scope(value = “”)
例:
在这里插入图片描述

五、Bean的属性注入

Bean的属性注入有两个注解:

  1. @Autowired
    用于对 Bean 的属性变量、属性的 Set 方法及构造函数进行标注,配合对应的注解处理器完成 Bean 的自动配置工作。默认按照 Bean 的类型进行装配。
    在这里插入图片描述

    @Autowired有一个属性:required
    required的默认值未true,表示必须注入该属性。可设置未false,表示不必须注入该属性。
    在这里插入图片描述
    拓展:
    @Qualifier
    与 @Autowired 注解配合使用,会将默认的按 Bean 类型装配修改为按 Bean 的实例名称装配,Bean 的实例名称由 @Qualifier 注解的参数指定。
    在这里插入图片描述

  2. @Resource
    其作用与 Autowired 一样。其区别在于 @Autowired 默认按照 Bean 类型装配,而 @Resource 默认按照 Bean 实例名称进行装配。

    @Resource 中有两个重要属性:name 和 type。

    Spring 将 name 属性解析为 Bean 实例名称,type 属性解析为 Bean 实例类型。如果指定 name 属性,则按实例名称进行装配;如果指定 type 属性,则按 Bean 类型进行装配。

    如果都不指定,则先按 Bean 实例名称装配,如果不能匹配,则再按照 Bean 类型进行装配;如果都无法匹配,则抛出 NoSuchBeanDefinitionException 异常。
    在这里插入图片描述
    在这里插入图片描述

六、注解总结

  1. @Component
    可以使用此注解描述 Spring 中的 Bean,但它是一个泛化的概念,仅仅表示一个组件(Bean),并且可以作用在任何层次。使用时只需将该注解标注在相应类上即可。

  2. @Repository
    用于将数据访问层(DAO层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。

  3. @Service
    通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。

  4. @Controller
    通常作用在控制层(如 Struts2 的 Action),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。

  5. @Autowired
    用于对 Bean 的属性变量、属性的 Set 方法及构造函数进行标注,配合对应的注解处理器完成 Bean 的自动配置工作。默认按照 Bean 的类型进行装配。

    @Autowired有一个属性:required
    required的默认值未true,表示必须注入该属性。可设置未false,表示不必须注入该属性。

  6. @Resource
    其作用与 Autowired 一样。其区别在于 @Autowired 默认按照 Bean 类型装配,而 @Resource 默认按照 Bean 实例名称进行装配。

    @Resource 中有两个重要属性:name 和 type。

    Spring 将 name 属性解析为 Bean 实例名称,type 属性解析为 Bean 实例类型。如果指定 name 属性,则按实例名称进行装配;如果指定 type 属性,则按 Bean 类型进行装配。

    如果都不指定,则先按 Bean 实例名称装配,如果不能匹配,则再按照 Bean 类型进行装配;如果都无法匹配,则抛出 NoSuchBeanDefinitionException 异常。

  7. @Qualifier
    与 @Autowired 注解配合使用,会将默认的按 Bean 类型装配修改为按 Bean 的实例名称装配,Bean 的实例名称由 @Qualifier 注解的参数指定。

七、混合使用XML+注解

本例演示:
使用xml的方式创建Bean,使用注解的方式注入属性。

创建两个类:
Card类:

package com.seven.bean;

public class Card {

    public void doSomething() {
        System.out.println("Card...");
    }
}

Person类:

package com.seven.bean;

import javax.annotation.Resource;

public class Person {
	//使用注解的方式注入属性
	@Autowried
    private Card card;

    public void doSomething() {
        System.out.println("Person...");
        card.doSomething();
    }
}

配置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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.seven.bean"/>

	<!-- 使用xml的方式创建Bean -->
    <bean id="person" class="com.seven.bean.Person"/>
    <bean id="card" class="com.seven.bean.Card"/>

</beans>

测试类:

import com.seven.bean.Person;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class XaTest {
    private ApplicationContext context;

    @Before
    public void init() {
        context = new ClassPathXmlApplicationContext("spring.config.xml");
    }

    @Test
    public void test() {
        Person person = (Person) context.getBean("person");
        person.doSomething();
    }
}

运行结果:
在这里插入图片描述


更新时间:2020-1-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值