Spring自动装配

Spring自动装配

  1. 手动装配实现属性注入

<bean id="studentDao" class="com.xz.dao.impl.StudentDaoImpl"></bean>

<bean id="studentService" class="com.xz.service.impl.StudentServiceImpl">

      <!--手动装配:设值注入,将studentDao对象注入给service对象的dao属性 -->

      <property name="studentDao" ref="studentDao"></property>

</bean>

  缺点:当维护bean组件或bean属性增加时,需要大量工作量完成配置。

  1. 自动装配实现属性注入
  1. 基于xml配置实现自动装配
  2. 基于注解+xml配置实现自动装配
  3. 基于注解+配置类实现自动装配

  1. 基于xml配置实现自动装配

   3.1 在bean标签上加上autowire自动装配属性

  <bean id="studentDao" class="com.xz.dao.impl.StudentDaoImpl"></bean>

  <bean id="studentService" class="com.xz.service.impl.StudentServiceImpl"

         autowire="byName">

  </bean>

   3.2  autowire属性值

属性值

描述

byName

根据名称自动装配。

要求:对象id标识名和对象属性的set方法名相同。

byType

根据类型自动装配

要求:对象的class类型和对象属性的类型相同。

注意:如果spring容器中有多个与对象属性相同的类型的bean,会报错。

constructor

类似于byType,区别在于走构造方法。

no

不自动装配。必须手动装配进行配置

default

装配方式和全局自动装配default-autowire的值一致。

如果autowire和default-autowrie都为default,那么就是不自动装配

  3.3 全局的自动装配

<beans default-autowire="byName"/> 适用于所有的bean

  1. 基于注解+XML配置管理 Bean实现方式
    1. Spring管理对象的注解,等价于<bean/>

注解

描述

@Component

创建对象注解,没有语义

默认id标识名:类名首字母小写

自定义id标识名:@Service("studentService")

@Service

放在service层类上注解

@Repository

放在dao层类上注解

@Controller

放在控制层Controller类上注解

    1. 自动注入注解,等价于autowire属性

注解

描述

@Resource

Jdk提供注解,不需要提供set方法。

先根据byName进行注入,如果没有name一样的,

再根据byType进行注入。

@Resource注解属于JDK扩展包,所以不在JDK当中,需要额外引入以下依赖:

<dependency>
    <groupId>jakarta.annotation</groupId>
    <artifactId>jakarta.annotation-api</artifactId>
    <version>2.1.1</version>
</dependency>

@Autowired

Spring提供注解,不需要提供set方法。

根据byType进行注入。

如果想根据名称装配,需要配合@Qualifier注解一起用。

@Qualifier(value = "名称") 根据指定的名称作为 bean 的id进行匹配注入

    1. 实现步骤
  1. Service

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    //@Resource
    StudentDao studentDao;

    @Override
    public List<Student> getStudents() {
        return studentDao.selectAll();
    }
}

  1. Dao

@Repository
public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Student> selectAll() {
        List<Student> list=new ArrayList<>();
        list.add(new Student("1001","小张",18));
        list.add(new Student("1002","小李",38));
        return list;
    }
}

  1. applicationContext.xml 配置文件

<!--开启注解扫描-->
<context:component-scan base-package="com.bdqn.service.impl,com.bdqn.dao.impl"/>

  1. 测试

/*基于注解+xml配置*/
@Test
public void test01(){
    ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    StudentService studentService=ac.getBean("studentServiceImpl",StudentService.class);
    List<Student> list=studentService.getStudents();
    System.out.println(list);
}

  1. 基于注解+配置类方式管理 Bean实现自动装配
  1. 相关注解

注解

描述

@Configuration

指定一个类为配置类,可以添加配置注解,替代配置xml文件

@ComponentScan(basePackages = {"包","包"})

开启注解扫描,替代<context:component-scan标签实现注解扫描

@PropertySource("classpath:配置文件地址")

加载读取配置文件,替代 <context:property-placeholder标签

  1. 实现步骤
  1. Service

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    //@Resource
    StudentDao studentDao;

    @Override
    public List<Student> getStudents() {
        return studentDao.selectAll();
    }
}

  1. Dao

@Repository
public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Student> selectAll() {
        List<Student> list=new ArrayList<>();
        list.add(new Student("1001","小张",18));
        list.add(new Student("1002","小李",38));
        return list;
    }
}

  1. 创建一个SpringConfig.java的java配置类,替换掉Spring的配置文件

      注意:删除applicationContext.xml配置文件

//标注当前类是配置类,替代applicationContext.xml
@Configuration
//使用@ComponentScan注解,可以配置扫描包,替代<context:component-scan标签
@ComponentScan(basePackages = {"com.bdqn.dao.impl","com.bdqn.service.impl"})
public class MyConfiguration {

}

  1. 测试

/*基于注解+配置*/
@Test
public void test01(){
    ApplicationContext ac=new AnnotationConfigApplicationContext(MyConfiguration.class);
    StudentService studentService=ac.getBean("studentServiceImpl",StudentService.class);
    List<Student> list=studentService.getStudents();
    System.out.println(list);
}

6. XML、注解+XML、完全注解 配置类 方式管理 Bean的区别

XMLIoC方式问题总结:

  1. 注入的属性必须添加setter方法、代码结构乱!

  2. 配置文件和Java代码分离、编写不是很方便!

  3. XML配置文件解析效率低

注解+XML IoC方式问题总结

    1. 自定义类可以使用注解方式,但是第三方依赖的类依然使用XML方式!

    2. XML格式解析效率低!

Spring 完全注解配置(Fully Annotation-based Configuration)

   是指通过 Java配置类 代码来配置 Spring 应用程序,

   使用注解来替代原本在 XML 配置文件中的配置。

   相对于 XML 配置,完全注解配置具有更强的类型安全性和更好的可读性。

7. Spring5+Test5搭建测试环境

   1. 整合测试环境作用

       好处1:不需要自己创建IOC容器对象了

       好处2:任何需要的bean都可以在测试类中直接享受自动装配

2. 导入相关依赖

<!--junit5测试-->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>

<!--Spring-test-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>6.0.6</version>
</dependency>

  1. 整合测试注解使用

/**
 * Spring5+Test5搭建测试环境
 */
//@SpringJUnitConfig(locations = {"applicationContext.xml"}) //指定配置文件
@SpringJUnitConfig(value = {MyConfiguration.class}) //指定配置类
public class TestDemo02 {
    @Autowired
    StudentService  studentService;
    
    @Test
    public void test01(){
        List<Student> list=studentService.getStudents();
        System.out.println(list);
    }
}

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值