【Spring5】第三篇:IOC之Annotation方式

Bertha 。 2023-02-28 04:14 121阅读 0赞

一、什么是注解

  • 注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…)
  • 使用注解,注解作用在类上面,方法上面,属性上面
  • 使用注解目的:简化 xml 配置

二、Spring 注解创建对象

  • @Component
  • @Service
  • @Controller
  • @Repository

上面四个注解功能是一样的,都可以用来创建 bean 实例,名称不一样是为了按照约定的习惯规范代码;主要针对自己写的类;

  • @Bean:针对外部引入的jar(用在创建方法返回对象上)

    • @Lazy:是否为懒加载
    • @Scope:指定单实例还是多实例
  • @Import:作用在类,可以直接引入类名创建对象实例

    • @ImportSelector:可以通过包全类名创建对象实例
    • @ImportBeanDefinitionRegistrar:通过在重写方法中手动创建Bean
  • @Conditional:根据条件创建对象,条件类需要实现 Condition 接口;可以作用在方法或者类上
  • FactoryBean:创建对象实例,定义 bean 类型可以和返回类型不一样;

2.1、注解创建对象

默认的实例名为: 类名首字母小写;也可以自己指定 @Service(value=””)
pom.xml

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-aop</artifactId>
  4. <version>5.2.7.RELEASE</version>
  5. </dependency>
  6. package com.wells.demo.ioc.annotation;
  7. import org.springframework.stereotype.Service;
  8. /** * Description 通过四种注解方式创建对象实例: @Component、@Service、@Controller、@Respository * Created by wells on 2020-07-19 20:44:31 */
  9. @Service
  10. public class UserService {
  11. public void delUser() {
  12. System.out.println("del user");
  13. }
  14. }

配置spring-bean.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <!-- 开启包扫描 -->
  4. <context:component-scan base-package="com.wells.demo.ioc.annotation"/>
  5. </beans>

2.2、开启组件扫描细节配置

2.2.1、include-filter

use-default-filters=“false” 表示现在不使用默认 filter,自己配置 filter:【context:include-filter ,设置扫描哪些内容】

  1. <context:component-scan base-package="com.wells.demo" use-default- filters="false">
  2. <context:include-filter type="annotation" expression="org.wells.demo.ioc.annotation.service"/>
  3. </context:component-scan>

2.2.2、exclude-filter

下面配置扫描包所有内容 context:exclude-filter: 设置哪些内容不进行扫描

  1. <context:component-scan base-package="com.wells.demo">
  2. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  3. </context:component-scan>

三、Spring 注解注入属性

  • 注入自定义类型属性:

    • @Autowired:根据属性类型进行注入引用类型
    • @Qualifier:根据名称进行注入引用类型,需要与 @Autowired 一起使用
    • @Resource:可以根据类型注入引用类型,可以根据名称注入引用类型
  • 注入基本类型属性:

    • @Value:注入普通类型类型属性

    package com.wells.demo.ioc.annotation;

    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Service;

    import javax.annotation.Resource;

    /* Description 通过四种注解方式创建对象实例: @Component、@Service、@Controller、@Respository 默认的实例名为: 类名首字母小写;也可以自己指定,例如:@Service(value=””) Created by wells on 2020-07-19 20:44:31 */

    @Service
    public class UserService {
    // @Autowired // 根据类型进行注入引用类型
    // private UserDao userDao;

    // @Autowired
    // @Qualifier(value = “userDao”) // 根据名称进行注入引用类型,但是必须和Autowired一起使用
    // private UserDao userDao;

    1. // @Resource // 根据类型注入引用类型
    2. @Resource(name = "userDao") // 根据名称进行注入引用类型
    3. private UserDao userDao;
    4. @Value(value = "wells") // 注入基础类型
    5. private String name;
    6. public void delUser() {
    7. System.out.println("del user in service");
    8. userDao.delUser();
    9. }
    10. @Test
    11. public void testAnnotation() {
    12. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("component-scan.xml");
    13. UserService userService = applicationContext.getBean("userService", UserService.class);
    14. userService.delUser();
    15. System.out.println(userService.name);
    16. }

    }

四、spring配置文件注解

@Configuration // 作为配置类,替代xml配置文件

配置文件替代类,通过 @Configuration 注解代替xml配置文件

  1. package com.wells.demo.ioc.annotation;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. /** * Description * Created by wells on 2020-07-19 22:40:51 */
  5. @Configuration
  6. @ComponentScan(basePackages = "com.wells.demo.ioc.annotation")
  7. public class SpringConfig {
  8. }

测试类

  1. @Test
  2. public void testAnnotationByConfiguration(){
  3. ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
  4. UserService userService = applicationContext.getBean("userService", UserService.class);
  5. userService.delUser();
  6. System.out.println(userService.name);
  7. }

完整代码

Spring Annotation

发表评论

表情:
评论列表 (有 0 条评论,121人围观)

还没有评论,来说两句吧...

相关阅读