首页 > 其他分享 >Spring_2023_11_20_2 -DI 依赖注入=》构造方式的形式

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

时间:2023-11-20 20:55:17浏览次数:24  
标签:11 20 DI Component bboy 注解 import studentService public

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>

标签:11,20,DI,Component,bboy,注解,import,studentService,public
From: https://www.cnblogs.com/Qinyyds/p/17844838.html

相关文章

  • HUAWEI SECURITY 2023 山东大学专场 WP
    CryptobySmera1d01.ezrsa题干如下:fromCrypto.Util.numberimportgetPrimefromsecretimportflagp=getPrime(512)print(p,pow(flag,2,p))给出了\(p\)和\({flag}^2modp\)即我们需要解一个已知\(n\)和\(p\),求解\(x^2=n(modp)\)中\(x\)的值上网查阅发现\(Tonelli......
  • Redis入门篇(一)
    初学Redis简介以及入门一.Redis的介绍1.什么是redis?Redis(RemoteDictionaryServer)是一个开源的内存数据结构存储系统。它支持多种数据结构,包括字符串、哈希表、列表、集合、有序集合等。与传统的关系型数据库相比,Redis是一种更加快速高效的数据存储方式。Redis在内存中存......
  • Redis:Key-Value的NoSQL数据库
    Redis:Key-Value的NoSQL数据库(基础)主要内容:数据库分类Redis入门客户端连接RedisRedis数据类型及操作Springboot整合RedisSpringCache学习目标:知识点要求数据库分类熟悉Redis使用场景熟悉Redis下载与安装了解Redis启动关闭掌握客户端连接Redis......
  • 11.20每日总结
    B/S结构用户界面设计    【实验编号】10003809548jWeb界面设计【实验学时】8学时【实验环境】l 所需硬件环境为微机;l 所需软件环境为dreamweaver【实验内容】这次实验要设计一个B/S结构的用户界面,题目自拟,我刚开始的选题是潮鞋售卖,所以我要做......
  • 20231120
    运行flash文件真是一件难事,不如直接转化为mp4通过本次的实验也是学习到了html界面中如何运行swf文件,也是了解到了flash的流氓性。更加深刻的了解到了人机交互技术的重要性。     ......
  • NOIP2023游记
    写下这篇游记的时候,我的内心是怎样的五味杂陈啊。随一首歌,随到了《如愿》。世间所有的路都将与你相逢。考前一天便感觉不太对劲,嗓子有点火辣辣地疼,鼻腔内也充斥着少量鼻涕。但这显然是心理作用的吧!于是第二天一上场头就开始变得有些蒙。偏偏系统炸了,大家都下不到题面。等......
  • sodick 成型机 沙迪克 数据采集 制程参数
    1、工业电脑主机设置2个共享文件夹比如在D盘:EXPORT和DATA  ,名字不能改。新建Window登录账号sodick和plustech,密码111 手册中有账号密码  2、sodick机台设置成型机的ip和工业电脑主机的ip,保证在同一个网段。 sodick自己会放共享里面写文件。账号密码如上面,不能改。......
  • Vue公共loading升级版(处理并发异步差时响应)
    公共loading是项目系统中很常见的场景,处理方式也不外乎三个步骤:1.通过全局状态管理定义状态值(vuex、pinia等)。2.在程序主入口监听状态值变化,从而展示/隐藏laoding动画。3.在请求和相应拦截器中变更状态值。第一二步骤处理大同小异,但在第三步中,网上很多博文分享的方法是:在请求......
  • 20231119 mac 使用dd 命令 烧写 linux img到sd卡
    https://docs.radxa.com/rock5/official-images?model=ROCK+5B下载rock5b官方操作系统文件是一个.img.xz文件打开一个mac终端,ls/dev关注/dev/disk相关的,插入SD卡读卡器到macmini,再次ls/dev 把sd卡格式化sudoddif=/dev/zeroof=/dev/disk4bs=64Mcoun......
  • MIT18.06Linear Algebra 第11讲 矩阵空间、秩 1 矩阵和小世界图
    转载于:超详细MIT线性代数公开课笔记......