Spring_2023_11_20_2 -DI 依赖注入=》构造方式的形式

发布时间 2023-11-20 20:52:39作者: Kbaor

DI 依赖注入=》构造方式的形式

构造方法的注入,使用实体类对象进行注入 Student类

集合的注入(数组、List、Set、Map)

 <!--
        <bean/> 等同于  new Student()
        通过构造方法的形式进行依赖注入
        constructor-arg:构造方法参数的注入标签
        1. index:下表,构造方法参数列表的下标
        2. type:指定参数的类型
        3. value:给指定参数注入的数据
    -->
    <bean id="student" class="com.neuedu.pojo.Student">
        <constructor-arg index="0" type="java.lang.String" value="Tom"/>
        <constructor-arg index="1" type="java.lang.String" value="男"/>
        <constructor-arg index="2" type="int" value="20"/>
        <!--
            属性:集合注入:Array
            <array>:表示数组概念
            <value>:表示数组中具体的内容
        -->
        <property name="arrData">
            <array>
                <value>100</value>
                <value>200</value>
                <value>300</value>
            </array>
        </property>
        <!--
            属性:集合注入:List
            <list>:表示list集合
            <value>:表示集合中具体的内容
        -->
        <property name="listData">
            <list>
                <value>list002</value>
                <value>list003</value>
                <value>list001</value>
            </list>
        </property>
        <!--
            属性:集合注入:Set
            <set>:表示Set集合
            <value>:表示集合中具体的内容
        -->
        <property name="setData">
            <set>
                <value>set001</value>
                <value>set001</value>
            </set>
        </property>
        <!--
            属性:集合注入:Set
            <map>:表示Set集合
            <entry>:表示集合中键值对对象key-value
        -->
        <property name="mapData">
            <map>
                <entry key="k01" value="v01"/>
                <entry key="k02" value="v02"/>
            </map>
        </property>
    </bean>

注册Bean对象基于注解形式

a) 注解:就是一个类,使用的格式:@注解名称
b) @Component 取代 xml文件
c) @Component(“id内容”) 取代
d) Web开发中,通过@Component注解衍生出来的注解
i. @Repository: 主要标注的是dao层
ii. @Service: 主要标注的是service层
iii. @Controller 主要标注的是web层(servlet)
e) 依赖的注入=》注解
i. 普通值:@Value
ii. 引用值 (ref)

  1. 按类型进行注入
    a) @Autowired
    f) 使用注解时,需要在spring核心的配置文件中,配置注解类所在的包
  2. 注解案例:
    a) 依赖的引入、核心配置文件的创建
    b) 创建controller/service/dao类并进行注解的使用

controller ---@Controller

package com.bboy.controller;

import com.bboy.service.StudentService;
import com.bboy.service.impl.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/20 0020 17:52:13
 */
@Component("studentController")
public class StudentController {
    @Autowired
    private StudentService studentService;
    public void listStudents(){
        studentService.listStudents();
    }

    public StudentService getStudentService() {
        return studentService;
    }

    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
}

dao --- @Repository("studentDao")

package com.bboy.dao.impl;

import com.bboy.dao.StudentDao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/20 0020 17:46:10
 */
// @Component 等同于 <bean class="com.neuedu.dao.impl.StudentDaoImpl"/>
//@Component
// @Component("studentDao") 等同于 <bean id="studentDao" class="com.neuedu.dao.impl.StudentDaoImpl"/>
//@Component("studentDao")
@Repository("studentDao")
public class StudentDaoImpl implements StudentDao {
    @Override
    public void listStudents() {
        System.out.println("StudentDaoImpl===============>>listStudents");
    }
}

servicec---@Service("studentService")

package com.bboy.service.impl;

import com.bboy.dao.StudentDao;
import com.bboy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/20 0020 17:48:31
 */


// @Component("studentService") 等同于 <bean id="studentService" class="com.neuedu.service.impl.StudentServiceImpl"/>
//@Component("studentService")
@Service("studentService")
public class StudentServiceImpl implements StudentService {
    //-:@Autowired 属性注入,属性名要与注入bean的id内容统一
    @Autowired
    private StudentDao studentDao;
    @Override
    public void listStudents() {
        studentDao.listStudents();
    }
}

在核心的配置文件中加载标有注解的注解类

i. 开启context命名空间 ii. 进行注解类所在包的扫描

<?xml version="1.0" encoding="UTF-8"?>
<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">
		 <!--
            进行组件扫描,扫描含有注解的类
        -->
    <context:component-scan base-package="com.bboy.controller"/>
    <context:component-scan base-package="com.bboy.service.impl"/>
    <context:component-scan base-package="com.bboy.dao.impl"/>
</beans>