Spring的学习(二,ioc)

16 篇文章 0 订阅

当我们对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的依赖

 //加载测试的依赖
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

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依赖

<!--   添加IOC需要的BCCE依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.16.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.16.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.16.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.3.16.RELEASE</version>
        </dependency>
    </dependencies>

也可以只写()

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

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

 

 2,创建配置文件(applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

</beans>

2.1:以Spring new 方式创建对象

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

 <bean id="students" class="com.lsf.ssm.ioc.Students"></bean>
    //bean 创建对象     id表示名称  class 表示包名称+创建类名称

2.2:单例创建对象

 <bean id="student01" class="com.lsf.ssm.ioc.Student01" factory-method="getInstance"></bean>
  //id  名称与3.2对应,class 包名称+类名称  factory-method:因为对象是私有化所有使用方法调用

3.3:工厂方法

 <!-- 创建工厂对象   -->
    <bean id="student02Factory" class="com.lsf.ssm.ioc.Student02Factory" ></bean>

    <!-- 创建对象   -->
    <bean id="student02" factory-bean="student02Factory" factory-method="createStudent02">

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

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韶光不负

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值