栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

框架01 框架概念&spring简介

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

框架01 框架概念&spring简介

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框架创建对象

①添加依赖

 
      org.springframework
      spring-context
      5.2.6.RELEASE
  

②添加配置



	
	
	
	
	
	
	
	
	

以student类为例举例:

 	

    
    

    
    

③实现

@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的成员变量赋值方法

①添加配置


	
        
        
        
    

	
        
        
        
    

②测试

@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);
    }

第一种结果测试

第二种结果测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/967212.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号