课程学习Spring

课程学习Spring

spring初见

将maven文件配置好后就可以很方便的学习spring

先创建一个学生类来进行学习:

package com.lean_spring.entity;

public class Student {
    private Integer ID;
    private String name;
    private char gender;
    private Double salary;
    private String intro;

    @Override
    public String toString() {
        return "Student{" +
                "ID=" + ID +
                ", name='" + name + '\'' +
                ", gender=" + gender +
                ", salary=" + salary +
                ", intro='" + intro + '\'' +
                '}';
    }

    public Integer getID() {
        return ID;
    }

    public void setID(Integer ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

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

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    public String getIntro() {
        return intro;
    }

    public void setIntro(String intro) {
        this.intro = intro;
    }
}

在到main方法中来测试它:

ackage com.lean_spring.test;

import com.lean_spring.entity.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStudent {
    public static void main(String[] args) {
        Student student = new Student();
        student.setID(1);
        student.setGender('男');
        student.setIntro("shuai");
        student.setName("name");
        student.setSalary(10.0);
        System.out.println(student);//使用syot需要先在student中重写tostring方法,我们可以使用spring容器来存储并且输出它
     

        //加载spring容器
        ClassPathXmlApplicationContext app= new ClassPathXmlApplicationContext("spring_config.xml");//相对路劲
        Student st=app.getBean("student",Student.class);
        System.out.println(st);

    }
}

使用spring容器首先要先在spring文件中来给他赋值定义:在res文件下创建一个spring的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">
<!-- spring ioc 控制反转-->
    <bean id="student" class="com.lean_spring.entity.Student">
        <property name="gender" value="男"></property>
        <property name="intro" value="shuai"></property>
        <property name="salary" value="10.00"></property>
        <property name="ID" value="100"></property>
        <property name="name" value="name"></property>
    </bean>
</beans>

设置好后就可以使用spring容器来运行测试student类了。

spring测试学习

ssm:spring springmvc mybatis

main方法限制:一个类中只有一个main方法

所以使用单元测试的方法Junit中的test方法

test方法是方法级所以应该在类中,可以配合before方法和after方法来使用用可以打成一个回环的效果。

权限必须是public 返回值必须是void 参数必须是空的 方法命名就要使用测试的某个单元来命名

package com.lean_spring.test;

import com.lean_spring.entity.Student;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStudent {
    public static void main(String[] args) {
        Student student = new Student();
        student.setID(1);
        student.setGender('男');
        student.setIntro("shuai");
        student.setName("name");
        student.setSalary(10.0);
        System.out.println(student);

        //加载spring容器
        ClassPathXmlApplicationContext app= new ClassPathXmlApplicationContext("spring_config.xml");//相对路劲
        Student st=app.getBean("student",Student.class);
        System.out.println(st);

    }
    private ClassPathXmlApplicationContext app;
    @Before  //在每次运行test方法之前都会运行一次before,一般用于初始化公共资源
    public void init(){
        app= new ClassPathXmlApplicationContext("spring_config.xml");
        System.out.println("运行了before方法");
    }


    @Test
    public void testStudent(){
        Student st=app.getBean("student",Student.class);
        System.out.println(st);
    }
    @Test
    public void testStudent2(){
        Student st=app.getBean("student",Student.class);
        System.out.println(st);
    }

    @After //与before相反每次运行test方法之后都会运行一次after方法,一般用于销毁资源或者关闭
    public void destory(){
        System.out.println("运行了destory方法");
    }
}

spring ioc (控制反转)是一种设计思想,将设计好的对象交给spring容器来处理。

在传统的javaSE设计是通过new的设置创建了一个对象,程序员主动创建了一个依赖对象

了解反转之前先知道正转,正转:传统是由我们主动去创建,控制一个对象或者依赖对象(即是自己使用需要则new一个,不需要就销毁它)

控制反转有以下几个关键点:

关键点是谁控制谁: IOC容器控制对象

控制什么 :控制外部资源获取(不止对象还有文件)

什么叫反转 :有容器来控制对象和依赖

为什么要反转: 由容器来查找和注入依赖对象,对象只是被动的接收依赖对象(极大的减轻程序员的精力,可以更好的实现业务)
那些地方反转了: 依赖对象的获取被反转了

在IOC中又含有一个重要的东西:

DI(依赖注入)

组件之间依赖关系由容器在运行期间决定;无需任何代码即可向指定目标所需要的资源来完成自身义务逻辑,而不需要关心资源来自何处,由谁决定或者由谁实现。

依赖的关键是以下几点:

谁依赖谁:应用程序依赖了IOC容器

为什么需要依赖:应用程序需要IOC容器来提供对象所需要的外部资源

谁注入了谁:IOC容器注入应用程序某个对象,应用程序的依赖对象

注入了什么:注入了某个对象所需要的外部资源(包括对象,资源,常量数据)

依赖注入(DI):明确描述了被注入对象对象依赖IOC容器配置对象

IOC容器主要是负责实例化,定位,配置应用程序中的对象以及建立这些对象之间的依赖

应用程序无需在代码中new相关对象,这些对象有IOC进行组装

Spring IOC容器管理的对象,称为bean

bean是spring容器初始化,装配以及管理的对象

//h获取bean 的4中方法
    @Test
    public void testStudent(){
        Student st=(Student)app.getBean("student");
        System.out.println(st);
    }
    @Test
    public void testStudent2(){
        Student st=app.getBean("student",Student.class);
        System.out.println(st);
    }
    @Test
    public void testStudent3(){
        //如果同一个类在springIOC容器中有两个或者多个相同的bean对象,则会抛出异常。
        Student st =app.getBean(Student.class);
        System.out.println(st);
    }
    @Test
    public void testStudent4(){
        //获取springIOC中所有关于student的bean对象
        Map<String,Student> map=app.getBeansOfType(Student.class);
        //将map遍历出来
        Set<Map.Entry<String,Student>> set=map.entrySet();
        for (Map.Entry<String,Student>stringStudentEntry:set){
            System.out.println(stringStudentEntry.getKey()
                    +""+stringStudentEntry.getValue());
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值