Spring-ioc-纯xml配置

3.21

1. 会创建 父子工程
父  --Project
子--- Module    -- 模块化开发

2. maven的目录结构

   src/main/java   存放java的源文件
   src/main/resources 存放配置文件       spring.xml
   src/main/webapp   存放 页面   css js html  jsp


3. spring 框架      https://spring.io/

    spring 可以简化开发, 开源的,
    spring 分为 aop(面向切面的编程)  和ioc (控制反转)


4. 添加依赖 -----pom.xml
spring-core.jar      核心库
spring-beans.jar     bean 豆, java豆, 操作java源文件
spring-expression.jar   表达式
spring-context.jar  上下文
commons-logging.jar  日志


5. 编写java 文件

 建包 建类

 类:要求:  体现封装
   a. 属性私有化
   b. 为每个属性 生成 get/set 方法
      getXXXX()  获得属性
      setXXX()  设置属性


b. 编写测试用例


junit ,  添加 依赖

在测试类中编写  方法, 方法上 增加 @Test  进行 单元测试用例

6. IOC
IOC  控制反转,  主要为了 降低程序的耦合度
   把 创建对象 及对象调用的过程 交给  spring处理

7. 为spring 添加 applicationContext.xml------- src/main/resources

  每一个类 都可以配置成一个 bean
  <bean id="值必须唯一" class="包名.类名"> </bean>

8. 测试类中 获得bean ,

   先读取  applicationContext.xml 通过调用 getBean("bean的id的值") 获取的对象



9. ioc的注入方法

如果创建一个类, 自己没有编写 构造方法, 则编译器自动增加 一个 无参数的构造
               如果 自己编写了, 则 编译器不会生成 无参 构造



今后
    类 必须 具有 属性, 属性的get/set方法
             无参及全参数的构造方法


  a. 构造方法注入

      <bean id="student" class="com.ly.Student" >
            <constructor-arg value="liuli" name="name"/>
             <constructor-arg name="sno" value="2024"/>

      </bean>


  b. 属性注入:  属性 必须具有 set/get  方法

       <bean id="student" class="com.ly.Student">
               <property name="name" value="wanglili"/>
               <property name="sno" value="1002"/>
       </bean>
  c. p命名空间





3.22     模拟 jdbc 连接数据库,并交给spring进行管理

     分层开发:  控制层 controller  ----servlet 充当
               业务层 service  ------ 主要调用 dao
               数据访问层 dao -------  主要连接db, 进行增删改查

创建 service 及 dao  进行模拟开发
    service 调用 dao,  dao 返回数据


实现步骤:
 添加依赖pom.xml
<!--  添加 spring 的相关依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.24</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.24</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.24</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.3.24</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>


    </dependencies>
    a. 编写 实体层 Student.java
           get/set  全参及无参构造
package com.ly;

public class Student {


    private int sno;//学号
    private String name;//姓名

    @Override
    public String toString() {
        return "Student{" +
                "sno=" + sno +
                ", name='" + name + '\'' +
                '}';
    }

    public Student(int sno, String name) {
        this.sno = sno;
        this.name = name;
    }

    public Student() {
    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
    b.  编写dao层(数据访问层)  StudentDao.java 及 StudentDaoImpl.java

        StudentDao.java 是接口  -- 只编写 声明方法
        StudentDaoImpl.java 是类-- 写 方法的具体实现
package com.ly.dao;

import com.ly.Student;

import java.util.List;

public interface StudentDao {

    List<Student> findAll();

}
package com.ly.dao.impl;

import com.ly.Student;
import com.ly.dao.StudentDao;

import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {


    @Override
    public List<Student> findAll() {

        List<Student> list = new ArrayList<>();

        Student student = new Student(1001,"lisi");
        Student student1 = new Student(1002,"lisi5");
        Student student2 = new Student(1003,"lisi4");

        list.add(student);
        list.add(student1);
        list.add(student2);
        return list;
    }


}
   c.  编写service层(业务层)  StudentService.java 及 StudentServiceImpl.java
          StudentService.java  接口
          StudentServiceImpl.java 实现类, 要把 StudentDao 作为属性

package com.ly.service;

import com.ly.Student;

import java.util.List;

public interface StudentService {

    /**
     * 查询全部
     * @return  返回 数据库的信息
     */
    List<Student> query();
}
package com.ly.service.impl;

import com.ly.Student;
import com.ly.dao.StudentDao;
import com.ly.dao.impl.StudentDaoImpl;
import com.ly.service.StudentService;

import java.util.List;

public class StudentServiceImpl implements StudentService {


    private StudentDao dao;

    public StudentDao getDao() {
        return dao;
    }

    public void setDao(StudentDao dao) {
        this.dao = dao;
    }

    @Override
    public List<Student> query() {

        return dao.findAll();
    }
}
  d. 编写 applicationContext.xml  配置 bean

     为 属性赋值时,如果 属性的数据类型 为 String+8种基本数据类型 则 使用value 进行赋值
     如果 属性的数据类型 不是 String 或 8种 基本数据类型  则 使用 ref  (引用) 赋值

<?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">




    <bean id="studentService" class="com.ly.service.impl.StudentServiceImpl">
        <property name="dao" ref="StudentDao"/>
    </bean>

    <bean id="StudentDao" class="com.ly.dao.impl.StudentDaoImpl">

    </bean>

</beans>
  e. 编写测试用例
import com.ly.Student;
import com.ly.service.StudentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestStudent {



    @Test
    public void  testQuery(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentService ser = (StudentService)context.getBean("studentService");
        List<Student> list = ser.query();
        System.out.println(list);
    }







}

相关推荐

  1. Spring-ioc-xml配置

    2024-03-23 08:36:04       47 阅读
  2. Spring-IOC-xml方式

    2024-03-23 08:36:04       59 阅读
  3. 001 spring ioc(xml)

    2024-03-23 08:36:04       29 阅读
  4. logback-spring.xml配置

    2024-03-23 08:36:04       20 阅读
  5. 使用XML方式配置IOC

    2024-03-23 08:36:04       49 阅读
  6. Spring注解配置

    2024-03-23 08:36:04       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-23 08:36:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-23 08:36:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-23 08:36:04       87 阅读
  4. Python语言-面向对象

    2024-03-23 08:36:04       96 阅读

热门阅读

  1. Python 10个面试题实例

    2024-03-23 08:36:04       39 阅读
  2. Spring 框架中都用到了哪些设计模式?

    2024-03-23 08:36:04       39 阅读
  3. uni app 空挡接龙

    2024-03-23 08:36:04       35 阅读
  4. createjs实现贪吃蛇,包含成长及游戏条件重置

    2024-03-23 08:36:04       44 阅读
  5. 软件测试简介

    2024-03-23 08:36:04       42 阅读
  6. Redis是如何避免“数组+链表”的过长问题

    2024-03-23 08:36:04       40 阅读
  7. 32、计算e

    2024-03-23 08:36:04       37 阅读
  8. Python中的函数参数传递方式是怎样的?

    2024-03-23 08:36:04       51 阅读
  9. AI大模型学习

    2024-03-23 08:36:04       31 阅读
  10. 算法体系-15 第十五节:贪心算法(下)

    2024-03-23 08:36:04       38 阅读
  11. Docker Oracle提示密码过期

    2024-03-23 08:36:04       39 阅读