首页 > 其他分享 >Spring_2023_11_20_1

Spring_2023_11_20_1

时间:2023-11-20 11:35:03浏览次数:26  
标签:11 20 service Spring bboy com StudentDao import StudentService

Spring基础依赖

pom依赖

<!-- Spring 基础包括 :
            Spring-core/Spring-beans/Spring-app/Spring-expression
 -->
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.20</version>
        </dependency>

核心配置文件(beans.xml/application.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
    UserService 的注册,通过bean标签进行注册
    1.<bean>标签
       1.1 id: 当前bean标签的标识符
       1.2 class 指定注册当前的类(不是接口)
-->
    <bean id="studentService"    class="com.bboy.service.impl.StudentServiceImpl">
        <!-- 属性注入
        1. name 表示在当前类中的属性名
            例如: studentDao 是在StudentServiceImpl 类中的属性名
        2. ref  表示当前属性在spring 核心配置文件中的bean = 》id为 studentDao
        -->
    <property name="studentDao" ref="studentDao"/>
    </bean>
    <bean id="teacherService"    class="com.bboy.service.impl.TeacherServiceImpl">
        <property name="teacherDao" ref="teacherDao"/>
    </bean>

    <bean id="studentDao"  class="com.bboy.dao.impl.StudentDapImpl"/>
    <bean id="teacherDao"  class="com.bboy.dao.impl.TeacherDaoImpl"/>
</beans>

测试 调用核心配置文件里的bean对象

package com.bboy.demo;

import com.bboy.service.StudentService;
import com.bboy.service.TeacherService;
import com.bboy.service.impl.StudentServiceImpl;
import com.bboy.service.impl.TeacherServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/20 0020 10:30:48
 */
public class Demo {
    public static void main(String[] args) {
    //- : 使用StudentService
    /*    StudentService service = new StudentServiceImpl();
        service.listStudents();*/
    //-1. 获取Spring容器(通过配置文件获取Spring容器)
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
    //-2. 从容器中获取去bean对象
        StudentService studentService =(StudentService) applicationContext.getBean("studentService");
        studentService.listStudents();
        TeacherService teacherService =(TeacherService) applicationContext.getBean("teacherService");
        teacherService.listTeachers();
    }
}

依赖注入

  • 通过service调用dao
  • 属性注入: 将StudentDao注入到StudentService

i.在StudentService实现类中设置StudentDao的属性并生成其get与set方法

package com.bboy.service.impl;
import com.bboy.dao.StudentDao;
import com.bboy.pojo.Student;
import com.bboy.service.StudentService;
import java.util.List;
/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/20 0020 10:27:51
 */
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao;
    public void setStudentDao(StudentDao studentDao){
        this.studentDao=studentDao;
    }
    public StudentDao getStudentDao() {
        return studentDao;
    }
    @Override
    public void listStudents() {
        System.out.println("==============>>StudentServiceImpl");
    }
}

ii. 在spring核心的配置文件中将StudentDao注入到StudentService中
image

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">
    <!--
    UserService 的注册,通过bean标签进行注册
    1.<bean>标签
       1.1 id: 当前bean标签的标识符
       1.2 class 指定注册当前的类(不是接口)
-->
    <bean id="studentService"    class="com.bboy.service.impl.StudentServiceImpl">
        <!-- 属性注入
        1. name 表示在当前类中的属性名
            例如: studentDao 是在StudentServiceImpl 类中的属性名
        2. ref  表示当前属性在spring 核心配置文件中的bean = 》id为 studentDao
        -->
    <property name="studentDao" ref="studentDao"/>
    </bean>

    <bean id="teacherService"    class="com.bboy.service.impl.TeacherServiceImpl">
        <property name="teacherDao" ref="teacherDao"/>
    </bean>

    <bean id="studentDao"  class="com.bboy.dao.impl.StudentDapImpl"/>
    <bean id="teacherDao"  class="com.bboy.dao.impl.TeacherDaoImpl"/>
</beans>

标签:11,20,service,Spring,bboy,com,StudentDao,import,StudentService
From: https://www.cnblogs.com/Qinyyds/p/17843555.html

相关文章

  • 2023第四季北京/杭州/青岛/深圳DAMA-CDGA/CDGP认证备考
    DAMA认证为数据管理专业人士提供职业目标晋升规划,彰显了职业发展里程碑及发展阶梯定义,帮助数据管理从业人士获得企业数字化转型战略下的必备职业能力,促进开展工作实践应用及实际问题解决,形成企业所需的新数字经济下的核心职业竞争能力。DAMA是数据管理方面的认证,帮助数据从业者提升......
  • 2023第四季北京/杭州/青岛/深圳CDGP认证报名到这儿
    DAMA认证为数据管理专业人士提供职业目标晋升规划,彰显了职业发展里程碑及发展阶梯定义,帮助数据管理从业人士获得企业数字化转型战略下的必备职业能力,促进开展工作实践应用及实际问题解决,形成企业所需的新数字经济下的核心职业竞争能力。DAMA是数据管理方面的认证,帮助数据从业者提升......
  • 高效开发与设计:提效Spring应用的运行效率和生产力
    引言现状和背景Spring框架是广泛使用的Java开发框架之一,它提供了强大的功能和灵活性,但在大型应用中,由于Spring框架的复杂性和依赖关系,应用的启动时间和性能可能会受到影响。这可能导致开发过程中的迟缓和开发效率低下。优化Spring应用程序的启动速度和性能是一个重要的任务,通过......
  • springboot 控制序列化反序列化示例(接口返回数据处理/接口接收数据处理)
    1.返回Long转JSONpackagecom.mingx.drone.config;importcom.fasterxml.jackson.core.JsonGenerator;importcom.fasterxml.jackson.databind.JsonSerializer;importcom.fasterxml.jackson.databind.SerializerProvider;importjava.io.IOException;/***@Descript......
  • NOIP 2023 游记
    NOIP2023游记赛前HF周四下午就放了,回家好好休息休息。周五上午睡了个懒觉,玩了会游戏。下午被我妈拉出去骑车,骑到一半,涵说他们因为教师研讨会放假,在图书馆写作业。说有个挂件想给我,然后就把我妈丢下骑车过去。一共52km,晚上8点才回到家。后来考完我妈和HF说了骑车这件......
  • 新建springboot项目,访问前端界面
    直接在IDEA中下载依赖会比较慢,将常用依赖下载到本地,然后从本地加载依赖会比较快。(方法可以搜,很多) pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quo......
  • 2023 互测 R2T1 序列的线性做法
    把原题做法GF的系数进行OEIS,发现那个三角形就是Catalan数的GF复合上一个\(xy(1-x)\)的形式。更为奇妙的是,OEIS下面竟然给出了一个通项公式,\(T(n,k)=(-1)^{n-k}{k\choosen-k}C_k\),其中\(C\)是Catalan数列。代入原题的式子,发现答案竟然就是:\[\sum_{i=0}^n(-1)^{n......
  • 【2023-11-15】亲情最美
    20:00年轻的时候以为不读书不足以了解人生,直到后来才发现如果不了解人生,是读不懂书的。读书的意义大概就是用生活所感去读书,用读书所得去生活吧。                                        ......
  • 11-20
    某系统需要提供一个命令集合(注:可以使用链表,栈等集合对象实现),用于存储一系列命令对象,并通过该命令集合实现多次undo()和redo()操作,可以使用加法运算来模拟实现。  packageLab16;importjava.util.Stack; publicabstractclassAbstractCommand{    publicabstrac......
  • 上周热点回顾(11.13-11.19)
    热点随笔:· 30岁之前透支,30岁之后还债。 (程序员济癫)· .NET8正式发布 (张善友)· 阿里云崩了,总结我们从云上搬到线下经历了什么 (iNeuOS工业互联网系统)· 最后的一次努力:尝试解决百度收录与排名问题 (博客园团队)· .NET8.0AOT经验分享FreeSql/FreeRedis/FreeSch......