案例04 基于注解的依赖注入案例

简介: 基于注解的依赖注入方式实现学生信息新增。

 基于注解的依赖注入方式实现学生信息新增。

1. 创建项目

Idea创建Java项目,项目名称为:case04-spring-student03。

2. 导入spring相关jar包

case04-spring-student03项目下创建lib目录,在lib目录下导入Jar包:

    • 核心包

    spring-core-5.3.25.jar

    spring-beans-5.3.25.jar

    spring-context-5.3.25.jar

    spring-expression-5.3.25.jar

      • AOP包

      spring-aop-5.3.25.jar

        • 测试包

        junit-4.6.jar

          • 依赖包

          commons-logging-1.2.jar

          3. 创建Spring配置文件

          src目录下创建applicationContext.xml配置文件。

          <?xmlversion="1.0" encoding="UTF-8"?><beansxmlns="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 https://www.springframework.org/schema/context/spring-context.xsd"><!--开启组件扫描--><context:component-scanbase-package="com.wfit"/></beans>


          4. 创建StudentService接口

          src目录下创建com.wfit.service包,此包目录下创建StudentService接口,声明addStudent方法。

          publicinterfaceStudentService {
          //新增学生信息publicvoidaddStudent();
          }


          5.创建StudentServiceImpl实现类

          src目录下创建com.wfit.service.impl包,此包目录下创建StudentServiceImpl实现类,实现addStudent方法。

          @Service//标注业务逻辑组件publicclassStudentServiceImplimplementsStudentService {
          @Autowired//@Autowired注解 完成自动配置privateStudentDaostudentDao;
          @OverridepublicvoidaddStudent() {
          //调用StudentDao中的saveStudent方法studentDao.saveStudent();
              }
          }


          6. 创建StudentDao类

          com.wfit.dao目录下创建StudentDao.java类

          @Repository//标注数据访问层publicclassStudentDao {
          //保存学生信息publicvoidsaveStudent(){
          System.out.println("保存学生信息成功!");
              }
          }


          7. 创建测试类

          src目录下创建com.wfit.test包,此包目录下创建TestStudent测试类。

          publicclassTestStudent {
          @Testpublicvoidtest(){
          //初始化Spring容器ApplicationContext,加载配置文件ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml");
          //通过容器获取StudentServiceImpl实例StudentServicestudentService=applicationContext.getBean("studentServiceImpl",StudentService.class);
          studentService.addStudent();
              }
          }


          8. 执行结果



          目录
          相关文章
          |
          XML Java 数据格式
          Spring框架入门以及 ioc的三种注入方式
          Spring框架入门以及 ioc的三种注入方式
          403 0
          |
          2月前
          |
          设计模式 XML Java
          【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
          本文详细介绍了Spring框架的核心功能,并通过手写自定义Spring框架的方式,深入理解了Spring的IOC(控制反转)和DI(依赖注入)功能,并且学会实际运用设计模式到真实开发中。
          【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
          |
          9月前
          |
          XML Java 程序员
          Spring6框架中依赖注入的多种方式(推荐构造器注入)
          依赖注入(DI)是一种过程,对象通过构造函数参数、工厂方法的参数或在对象实例构建后设置的属性来定义它们的依赖关系(即与其一起工作的其他对象)。
          167 3
          |
          6月前
          |
          缓存 Java 开发者
          Spring高手之路22——AOP切面类的封装与解析
          本篇文章深入解析了Spring AOP的工作机制,包括Advisor和TargetSource的构建与作用。通过详尽的源码分析和实际案例,帮助开发者全面理解AOP的核心技术,提升在实际项目中的应用能力。
          93 0
          Spring高手之路22——AOP切面类的封装与解析
          |
          8月前
          |
          XML Java 数据格式
          Spring框架第三章(基于注解管理bean)
          Spring框架第三章(基于注解管理bean)
          |
          9月前
          |
          Java Spring
          代码优雅的转变:基于注解的AOP编程在Spring中的实践
          代码优雅的转变:基于注解的AOP编程在Spring中的实践
          42 0
          |
          9月前
          |
          Java 数据库连接 API
          Java反射详解,学以致用,实战案例(AOP修改参数、Mybatis拦截器实现自动填充)2
          Java反射详解,学以致用,实战案例(AOP修改参数、Mybatis拦截器实现自动填充)
          113 0
          |
          9月前
          |
          安全 前端开发 Java
          Java反射详解,学以致用,实战案例(AOP修改参数、Mybatis拦截器实现自动填充)3
          Java反射详解,学以致用,实战案例(AOP修改参数、Mybatis拦截器实现自动填充)
          146 0
          |
          9月前
          |
          存储 Java 数据库连接
          Java反射详解,学以致用,实战案例(AOP修改参数、Mybatis拦截器实现自动填充)1
          Java反射详解,学以致用,实战案例(AOP修改参数、Mybatis拦截器实现自动填充)
          106 0
          |
          XML Java 数据格式
          spring框架入门介绍以及IOC的三种注入方式
          spring框架入门介绍以及IOC的三种注入方式
          268 0