Spring框架在Bean中的管理(第十课)

简介: Spring框架在Bean中的管理(第十课)

         

Spring框架在Bean中的管理(第十课)案例实操 了解SpringIoc第一种注入的方式 这里从java基础开始说起

利用的技术有:Java的面向对象+SpringIoc第一种方式的注入+Maven项目依赖的构建 不需要写配置文件的方式。

第一步:导入必要的Maven的坐标 在Pom.xml文件中:这里推荐一个网站 下面的网站用来查找maven的坐标用的

https://mvnrepository.com/search?q=Spring

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.22</version>
</dependency>

第二部分:回忆一下Java是如何创建对象的呢! 首先要有一个类 在这个类中 要有无参 有参 set get tostring 方法 来完成一个标准的类

public class Student {
    private String name;
    private int age;
    /*非正常的数据类型*/
    private Date birthday;
}
//这个类中的set和get方法自己去写

然后测试类:运行结果如下所示

 @Test
    public void one() {
        //创建对象1
        Student student1 = new Student();
        //存放地址编码
        System.out.println("java代码中创建对象1Student student1 = new Student();");
        System.out.println(student1);
        //创建对象2
        Student student2 = new Student();
        //存放地址编码
        System.out.println("java代码中创建对象2Student student1 = new Student();");
        System.out.println(student2);
        //创建对象3
        Student student3 = new Student();
        System.out.println("java代码中创建对象3Student student1 = new Student();");
        System.out.println(student3);
        Date date = new Date();
        System.out.println("java代码中创建 Date date=new Date() 对象");
        System.out.println(date);
    }

上面的运行结果 观察发现上面创建的三个对象中 Student student1 = new Student(); 这行代码


思考一下 能不能不用 new 关键字创建对象呀!答案是有的  在框架中:

在applicationContext.xml文件中如何创建对象的呢 关于Bean来管理

观察下面的案例

<!-- 下面代码等价于Student student1 = new Student();-->
    <!--scope属性的学习-->
    <!--scope="prototype" 创建多个对象 创建多个空间
     singleton 创建单个对象的遍历  创建单个空间  -->
    <bean id="Student" class="com.Sping.Entily.Student" scope="prototype"></bean>
 @Test
    public void two() {
        //加载文件配置信息的文件将rescure文件夹下的applicationContext.xml文件导入其中
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
  //强制转换类型
        Student student = (Student) ac.getBean("Student");
        System.out.println("Spring文件创建对象1");
        System.out.println(student);
//        Studenta
        Student studenta = (Student) ac.getBean("Student");
        System.out.println("Spring文件创建对象2");
        System.out.println(studenta);
//        Sping 框架中同一对象Ip地址  从Sping 框架中默认为单利模式 在同一块空间中
        Date date = new Date();

Student student = (Student) ac.getBean("Student");

得出结论一:id="Student" 要和getBean("Student"); 一致


第三部分:回忆一下Java是如何给对象赋值的呢! Java是这样做的  利用Set去赋值 利用get去取出值。

/*面向对象中如何用java代码进行赋值的呢?*/
    @Test
    public void two() {
        Student student = new Student();
        student.setName("我是利用Java创建出来的对象");
        student.setAge(2022);
        System.out.println(student.getName() + "=====" + student.getAge());
        System.out.println("--------------------------------------------------");
        Dog dao2 = new Dog();
        dao2.setAge(67);
        dao2.setName("我是第二只狗JAVA创建的对象哦哦哦");
        System.out.println("狗的年龄为"+dao2.getAge()+dao2.getName());
    }

Java对象取出值的结果

思考一下 能不能不用 set属性 关键字创建对象呀!答案是有的  在框架中:

<!--  Sping框架的方式给对象赋值-->
    <!--  <property name="K" value="V"></property> 一个属性对应一个值 -->
    <bean id="stu" class="com.Sping.Entily.Student">
        <!--Sping方式的赋值-->
        <property name="name" value="小王"></property>
        <property name="age" value="12"></property>
    </bean>
 <bean id="dog" class="com.Sping.Entily.Dog">
        <property name="name" value="小黑"></property>
        <property name="age" value="45"></property>
    </bean>
<bean id="dog" class="com.Sping.Entily.Dog">
    <property name="属性" value="值"></property> 在Bean里面写了一个标签property
</bean>
 @Test
    public void three() {
        /*Sping方式的赋值 第一步加载配置文件*/
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu = ac.getBean("stu", Student.class);
        System.out.println("利用Spring文件赋值");
        System.out.println(stu.getName() + "-----------" + stu.getAge());
        Dog dog = ac.getBean("dog", Dog.class);
        System.out.println("姓名为"+dog.getName()+"年龄为"+dog.getAge());
    }

这又是结果

第四部分:回忆一下Java是如何间接获得对象的呢 Java是这样做的?

public class Dog {
    private String name;
    private int age;
    /*非正常的数据类型*/
    private Date birthday;
    public Dog() {
    }
public class School {
    /*学校类*/
    public Student geStudent(){
        return new Student();
    }
    public  Dog getDao(){
        return  new Dog();
    }
    public static  Student getStudent1(){
        return  new Student();
    }
}
  @Test
    public  void three(){
        /*java代码的方式*/
        School school=new School();
        Student student=school.geStudent();
        System.out.println("java代码的方式获得"+student);
        System.out.println("----------------------------------------------------")
    }

School school=new School();

Student student=school.geStudent(); 思考一下Spring框架如何做呢?

 <!--间接方式获得-->
    <bean id="school" class="com.Sping.Entily.School"></bean>
    <bean id="student" factory-bean="school" factory-method="geStudent">
        <property name="name" value="我是间接方式获得的"></property>
        <property name="age" value="89"></property>
    </bean>
 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        Student s=ac.getBean("student",Student.class);
        System.out.println(s.getName()+"======"+s.getAge());

间接方式的获取运行结果

第五部分:Spring框架Ioc 静态方式获得

<!--  案例四/*静态方式获得*/-->
    <bean id="student1" class="com.Sping.Entily.School" factory-method="getStudent1">
        <property name="name" value="我是静态static"></property>
        <property name="age" value="12"></property>
    </bean>
 /*静态方式获得*/
    @Test
    public void four(){
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student1=ac.getBean("student1",Student.class);
        System.out.println(student1.getName()+"----------"+student1.getAge());
    }

运行结果

Spring框架在Bean中的管理(第十一课)_星辰镜的博客-CSDN博客

相关文章
|
14天前
|
安全 Java 开发者
如何在Spring框架中实现横切关注点的集中管理和重用?
【4月更文挑战第30天】如何在Spring框架中实现横切关注点的集中管理和重用?
18 0
|
14天前
|
安全 Java 程序员
Spring框架的核心特性是什么?
【4月更文挑战第30天】Spring 的特性
17 0
|
22小时前
|
开发框架 监控 Java
深入探索Spring Boot的监控、管理和测试功能及实战应用
【5月更文挑战第14天】Spring Boot是一个快速开发框架,提供了一系列的功能模块,包括监控、管理和测试等。本文将深入探讨Spring Boot中监控、管理和测试功能的原理与应用,并提供实际应用场景的示例。
14 2
|
4天前
|
XML Java 数据库连接
Spring框架与Spring Boot的区别和联系
Spring框架与Spring Boot的区别和联系
15 0
|
6天前
|
前端开发 安全 Java
使用Spring框架加速Java开发
使用Spring框架加速Java开发
52 0
|
6天前
|
设计模式 数据采集 监控
Spring日志框架
Spring日志框架
9 0
|
6天前
|
前端开发 Java 应用服务中间件
Spring MVC框架概述
Spring MVC 是一个基于Java的轻量级Web框架,采用MVC设计模型实现请求驱动的松耦合应用开发。框架包括DispatcherServlet、HandlerMapping、Handler、HandlerAdapter、ViewResolver核心组件。DispatcherServlet协调这些组件处理HTTP请求和响应,Controller处理业务逻辑,Model封装数据,View负责渲染。通过注解@Controller、@RequestMapping等简化开发,支持RESTful请求。Spring MVC具有清晰的角色分配、Spring框架集成、多种视图技术支持以及异常处理等优点。
15 1
|
14天前
|
SQL Java 数据库连接
Springboot框架整合Spring JDBC操作数据
JDBC是Java数据库连接API,用于执行SQL并访问多种关系数据库。它包括一系列Java类和接口,用于建立数据库连接、创建数据库操作对象、定义SQL语句、执行操作并处理结果集。直接使用JDBC涉及七个步骤,包括加载驱动、建立连接、创建对象、定义SQL、执行操作、处理结果和关闭资源。Spring Boot的`spring-boot-starter-jdbc`简化了这些步骤,提供了一个在Spring生态中更便捷使用JDBC的封装。集成Spring JDBC需要添加相关依赖,配置数据库连接信息,并通过JdbcTemplate进行数据库操作,如插入、更新、删除和查询。
|
14天前
|
SQL Java 数据库连接
Springboot框架整合Spring Data JPA操作数据
Spring Data JPA是Spring基于ORM和JPA规范封装的框架,简化了数据库操作,提供增删改查等接口,并可通过方法名自动生成查询。集成到Spring Boot需添加相关依赖并配置数据库连接和JPA设置。基础用法包括定义实体类和Repository接口,通过Repository接口可直接进行数据操作。此外,JPA支持关键字查询,如通过`findByAuthor`自动转换为SQL的`WHERE author=?`查询。
|
14天前
|
安全 Java 开发者
在Spring框架中,IoC和AOP是如何实现的?
【4月更文挑战第30天】在Spring框架中,IoC和AOP是如何实现的?
22 0