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

Spring的学习(二,ioc)

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

Spring的学习(二,ioc)

当我们对spring有一点了解,下面就可以开始对spring的核心技术的学习了!

目录

IOC容器的介绍

容器:

pojo:

javabean:

IOC:

ioc中的重要接口

实现(IOC)控制反转(spring框架来创建)

在maven中我们要测试先实现Test的依赖

J2se中创建对象方法(Java创建对象的三种方式)

1,new

2,单例设计模式(饿汉式)

3,工厂创建

IOC创建对象方式

1,搭建ioc Spring的环境(添加依赖,添加配置,添加代码)

 2,创建配置文件(applicationContext.xml)

3,添加代码


IOC容器的介绍

容器:

存放许多的对象,可以管理对象的生命周期、对象与对象之间的依赖关系。

pojo:

PoJo (Plain old Java Object)这种叫法是Martin Fowler、Rebecca Parsons和losh MacKenzie在2000年的一次演讲的时候提出来的。按照Martin Fowler的解释是*Plain old Java Object"',从字面上翻译为"纯洁老式的lava对象”,但大家都使用"简单java对象"来称呼它。PO)0的内在含义是指:那些没有继承任何类、也没有实现任何接口,更没有被其它框架侵入的java对象。不允许有业务方法,也不能携带connection之类的方法,实际就是普通JavaBeans.

javabean:

JavaBean是一种JAVA语言写成的可重用组件。JavaBean符合一定规范编写的Java类,不是一种技术,而是一种规范。大家针对这种规范,总结了很多开发技巧、工具函数。符合这种规范的类,可以被其它的程序员或者框架使用。它的方法命名,构造及行为必须符合特定的约定:

1、所有属性为private。

2、这个类必须有一个公共的缺省构造函数。即是提供无参数的构造器。

3、这个类的属性使用getter和setter来访问,其他方法遵从标准命名规范。

4、这个类应是可序列化的。实现serializable接口。
因为这些要求主要是靠约定而不是靠实现接口,所以许多开发者把JavaBean看作遵从特定命名约定的POJO.
 

IOC: i Inversion of Control 控制反转 ( 以前通过new 创建对象,而现在通过spring框架来创建 )

ioc中的重要接口

org.springframework. beans和org.springframework.context包是Spring框架的loC容器的基础。

BeanFactory接口提供了一种高级的配置机制,能够管理任何类型的对象。ApplicationContext是BeanFactory的子接口。它对BeanFactory进行了补充:
1,更容易与Spring的AOP特性集成·

2,消息资源处理(用于国际化)

3,事件发布

4,应用程序层特定的上下文,如WebApplicationContext用于web应用程序。

实现(IOC)控制反转(spring框架来创建)

在maven中我们要测试先实现Test的依赖
 //加载测试的依赖
    
        
            junit
            junit
            4.12
            test
        
    

我们创建一个学生类来测试

package com.lsf.ssm.ioc;

public class Students {
    private int id;
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Students{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age=" + age +
                '}';
    }

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

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

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

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

}

J2se中创建对象方法(Java创建对象的三种方式)

1,new
import com.lsf.ssm.ioc.Students;

import static org.junit.Assert.*;
public class StudentTest {
    public static void main(String[] args) {
        Students student = new Students();
        System.out.println(student);
    }
}

2,单例设计模式(饿汉式)
package com.lsf.ssm.ioc;

public class Student01 {
    //简单单例设计模式
    private static Student01 instance = new Student01();

    private Student01(){

    }

    public static Student01 getInstance(){
        return instance;
    }
}

 测试(产生新对象):

import com.lsf.ssm.ioc.Student01;
import com.lsf.ssm.ioc.Students;
import org.junit.Test;

import static org.junit.Assert.*;
public class StudentTest {
    @Test
    public void testStudent01(){
        Students01 instanceof01  = Student01.getInstance();
        Students01 instanceof02  = Student01.getInstance();
        //返回是同一个对象
        System.out.println(instanceof01==instanceof02);
    }

}

3,工厂创建

创建一个对象

package com.lsf.ssm.ioc;

public class Student02 {
    private int id;
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Students{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age=" + age +
                '}';
    }

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

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

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

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

}

创建一个与之相当应的工厂

package com.lsf.ssm.ioc;

public class Student02Factory {
    //构建一个工程类
    public Student02 createStudent02(){
        return new Student02();
    }
}

测试产生对象

import com.lsf.ssm.ioc.Student02;
import com.lsf.ssm.ioc.Student02Factory;
import org.junit.Test;

import static org.junit.Assert.*;
public class StudentTest {
    @Test
    public void testStudent01(){
        Student02Factory student02Factory = new Student02Factory();
        Student02 student02 = student02Factory.createStudent02();
        System.out.println(student02);
    }

}

IOC创建对象方式

1,搭建ioc Spring的环境(添加依赖,添加配置,添加代码)

添加BCCE依赖


    
        
            org.springframework
            spring-beans
            4.3.16.RELEASE
        
        
            org.springframework
            spring-context
            4.3.16.RELEASE
        
        
            org.springframework
            spring-core
            4.3.16.RELEASE
        
        
            org.springframework
            spring-expression
            4.3.16.RELEASE
        
    

也可以只写()

 
        
            org.springframework
            spring-context
            4.3.16.RELEASE
        

因为spring-context包含了(spring-expression,spring-core,spring-beans)

 

 2,创建配置文件(applicationContext.xml)

一般名称就这样名命




2.1:以Spring new 方式创建对象

在配置文件中创建对象(有几个bean就有几个对象,保存在spring对象池当中)

 
    //bean 创建对象     id表示名称  class 表示包名称+创建类名称

2.2:单例创建对象

 
  //id  名称与3.2对应,class 包名称+类名称  factory-method:因为对象是私有化所有使用方法调用

3.3:工厂方法

 
    

    
    

//factory-bean 必须与创建的工厂对象名称一样  factory-method 工厂创建对象方法

3,添加代码

3.1 :添加代码获取对象(以new方式获取)

import com.lsf.ssm.ioc.Students;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.applet.AppletContext;

public class IocTest {
    @Test
    public void testStudent(){
        //加载spring配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //获取对象
        Students students = ac.getBean("students", Students.class);

        System.out.println(students);


    }
}

 

3.2,单例获取对象

import com.lsf.ssm.ioc.Student01;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.applet.AppletContext;

public class IocTest {
   
     @Test
    public void testStudent01(){
        //加载spring配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //获取对象
        Student01 students01 = ac.getBean("student01", Student01.class);
        Student01 students02 = ac.getBean("student01", Student01.class);
        System.out.println(students01 == students02);
    }
}

 3.3:工厂方式获取对象

import com.lsf.ssm.ioc.Student02;
import com.lsf.ssm.ioc.Student02Factory;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.applet.AppletContext;

public class IocTest {
    @Test
    public void testStudent02(){
        //加载spring配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //获取对象
        Student02 student02 = ac.getBean("student02",Student02.class);
        System.out.println(student02);
    }
}

 

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

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

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