框架01 框架概念&spring简介

本文深入探讨了Spring框架的核心——IOC(控制反转)和DI(依赖注入)概念。通过实例展示了在J2SE中创建对象的传统方式,并详细解释了Spring如何通过配置文件实现对象的创建与成员变量的赋值。同时,文中还涵盖了Spring中使用构造器、工厂方法以及单例模式创建对象的方法,以及如何通过setter注入和构造器注入来实现依赖注入。
摘要由CSDN通过智能技术生成

1,框架是什么?

框架(Framework),是基于建筑概念,适用于理解软件等技术领域各种框架、架构概念。是用于承载一个系统必要功能的基础要素的集合。

2,常用的框架

  • 重量级框架ssh(spring+struct2+hibernate)
  • 轻量级框架ssm(spring+springmvc+mybatis)
    ps:博主主要学习ssm框架。

3,Spring框架是什么?

spring框架里包含spring framework ,spring boot ,spring cloud,……

4,Spring框架中包含的内容

在这里插入图片描述

5,Spring框架的核心内容

5.1,ioc

5.1.1,概念

ioc: Inversion of Control 控制反转
以前在学习Java时侯通过new创建对象,而现在通过spring创建

5.1.2,j2se阶段创建对象

public class Student01 {
    private int id;
    private String name;
    private int age;
    public Student01() {
    	}
    public Student01(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

以Student01类为例

//第一种创建方式:通过new调用构造函数
Student01 st01 = new Student01();
//第二种:单例模式
private static Student01 instance = new Student01();
private Student01(){}
public static Student01 getInstance() {
	return instance
}
//第三种:对象方法
public class Student01Factory {
    public Student01 createStudent03(){
        return new Student01();
    }
}
Student01Factory student01Factory = new StudentFactory01();
Student01 student01 = student01Factory.createStudent01();

5.1.3,spring框架创建对象

①添加依赖

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.6.RELEASE</version>
  </dependency>

②添加配置

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
	<!--第一种:构造器创建-->
	<bean id="exampleBean" class="examples.ExampleBean"/>
	<bean name="anotherExample" class="examples.ExampleBeanTwo"/>
	<!--第二种:工厂模式创建-->
	<bean id="clientService"
    	class="examples.ClientService"
    	factory-method="createInstance"/>
	</beans>
	<!--第三种:单例模式创建-->
	<bean id="serviceLocator" class="examples.DefaultServiceLocator"></bean>
	<bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/>

以student类为例举例:

 	<bean id="student01" class="com.op.ssm.ioc.Student01"></bean>

    <bean id="student02_01" class="com.op.ssm.ioc.Student02" factory-method="getInstance"></bean>
    <bean id="student02_02" class="com.op.ssm.ioc.Student02" factory-method="getInstance"></bean>

    <bean id="student03Factory" class="com.op.ssm.ioc.Student03Factory"></bean>
    <bean id="student03" factory-bean="student03Factory" factory-method="createStudent03"></bean>

③实现

@Test
    public void testMethod01(){
        // 加载spring框架配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationcontext-01.xml");

        // 获取创建好的对象
        Student01 student01 = ac.getBean("student01", Student01.class);

        System.out.println(student01);

    }

    @Test
    public void testMethod02(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationcontext-01.xml");
        Student02 student02_01 = ac.getBean("student02_01", Student02.class);
        Student02 student02_02 = ac.getBean("student02_02", Student02.class);
        System.out.println(student02_01==student02_02);
    }
    @Test
    public void testMethod03(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationcontext-01.xml");
        Student03 student03 = ac.getBean("student03", Student03.class);
        System.out.println(student03);
    }

5.2,DI

5.2.1,概念

DI: Dependency injection 依赖注入
初始化成员变量,给成员变量赋值。

5.2.2,j2se阶段成员变量赋值方法

//第一种:通过构造函数
 Student01 student01 = new Student01(1, "wjl", 1);
//第二种:通过set
Student01 student01 = new Student01();
student01.setId(2);
student01.setAge(18);
student01.setName("www");

5.2.3,spring的成员变量赋值方法

①添加配置

<!--第一种:构造注入
index 变量位置,从0开始
value将要赋的值
-->
	<bean id="student01" class="com.openlab.ssm.di.Student01">
        <constructor-arg index="0" value="1"></constructor-arg>
        <constructor-arg index="1" value="www"></constructor-arg>
        <constructor-arg index="2" value="18"></constructor-arg>
    </bean>
<!--第二种:set注入
 name写的是set方法去掉字母set后,第一个字母小写
-->
	<bean id="student02" class="com.openlab.ssm.di.Student02">
        <property name="id" value="2"></property>
        <property name="name" value="wj"></property>
        <property name="age" value="20"></property>
    </bean>

②测试

@Test
    public void testMethod01() {
        //加载配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:application-02.xml");
        //获取对象
        Student01 student01 = ac.getBean("student01", Student01.class);
        //查看
        System.out.println(student01);
    }
    @Test
    public void testMethod02() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:application-02.xml");
        //获取对象
        Student02 student02 = ac.getBean("student02", Student02.class);
        //查看
        System.out.println(student02);
    }

第一种结果测试
在这里插入图片描述
第二种结果测试
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值