ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【原理+实战+视频+源码】Spring学习总结:IOC基础

2021-08-04 21:04:17  阅读:176  来源: 互联网

标签:name Spring class 源码 Animal spring IOC public String


}




Student



package com.kevin.spring.demo2.entity;

/**

  • 学生

*/

public class Student extends Person{

/**

 * 身高

 */

public int height;



/**

 * 有参构造函数

 * @param name

 * @param height

 */

public Student(String name,int height) {

    this.name = name;

    this.height = height;

}



@Override

public String toString() {

    return "Student{" +

            "height=" + height +

            ", name='" + name + '\'' +

            '}';

}

}




student.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">

<bean id="kevin" class="com.kevin.spring.demo2.entity.Student">

    <constructor-arg name="name" value="kevin"></constructor-arg>

    <constructor-arg name="height" value="170"></constructor-arg>

</bean>



<!--使用索引指定参数-->

<bean id="maomao" class="com.kevin.spring.demo2.entity.Student">

    <constructor-arg index="0" value="maomao"></constructor-arg>

    <constructor-arg index="1" value="100"></constructor-arg>

</bean>



测试类



package com.kevin.spring.demo2.test;

import com.kevin.spring.demo2.entity.Person;

import com.kevin.spring.demo2.entity.Student;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {

    ApplicationContext ctx = new ClassPathXmlApplicationContext("student.xml");

    Person kevin = ctx.getBean("kevin", Student.class);

    Person maomao = ctx.getBean("maomao", Student.class);

    System.out.println(maomao);

    System.out.println(kevin);

}

}




输出



信息: Loading XML bean definitions from class path resource [student.xml]

Student{height=100, name=‘maomao’}

Student{height=170, name=‘kevin’}




通过属性赋值



Animal



package com.kevin.spring.demo3.entity;

/**

  • 动物

*/

public class Animal {

/**

 * 动物名称

 */

private String name;



public Animal() {

}



public Animal(String name) {

    this.name = name;

}



public String getName() {

    return name;

}



public void setName(String name) {

    this.name = name;

}



@Override

public String toString() {

    return "Animal{" +

            "name='" + name + '\'' +

            '}';

}

}




animal.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">

<bean id="dog" class="com.kevin.spring.demo3.entity.Animal">

    <property name="name" value="dog"></property>

</bean>





<bean id="cat" class="com.kevin.spring.demo3.entity.Animal" p:name="cat"></bean>



测试



package com.kevin.spring.demo3.test;

import com.kevin.spring.demo3.entity.Animal;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

  • 测试类

*/

public class Test {

public static void main(String[] args) {

    ApplicationContext ctx = new ClassPathXmlApplicationContext("animal.xml");

    Animal dog = ctx.getBean("dog",Animal.class);

    Animal cat = ctx.getBean("cat",Animal.class);

    System.out.println(cat);

    System.out.println(dog);

}

}




输出结果



信息: Loading XML bean definitions from class path resource [animal.xml]

Animal{name=‘cat’}

Animal{name=‘dog’}




对象引用



Tyre



package com.kevin.spring.demo4.entity;

/**

  • 轮胎

  • @author: kevin

  • @Date: 2018/12/8

*/

public class Tyre {

private String name;



public Tyre(String name) {

    this.name = name;

}



public String getName() {

    return name;

}



public void setName(String name) {

    this.name = name;

}



@Override

public String toString() {

    return "Tyre{" +

            "name='" + name + '\'' +

            '}';

}

}




Car



package com.kevin.spring.demo4.entity;

/**

*/

public class Car {

private String name;



private Tyre tyre;



public Car(String name, Tyre tyre) {

    this.name = name;

    this.tyre = tyre;

}



public String getName() {

    return name;

}



public void setName(String name) {

    this.name = name;

}



public Tyre getTyre() {

    return tyre;

}



public void setTyre(Tyre tyre) {

    this.tyre = tyre;

}



@Override

public String toString() {

    return "Car{" +

            "name='" + name + '\'' +

            ", tyre=" + tyre +

            '}';

}

}




测试



package com.kevin.spring.demo4.test;

import com.kevin.spring.demo4.entity.Car;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {

    ApplicationContext ctx = new ClassPathXmlApplicationContext("car.xml");

    Car bike = ctx.getBean("bike", Car.class);

    System.out.println(bike);

}

}




输出结果



信息: Loading XML bean definitions from class path resource [car.xml]

Car{name=‘bike’, tyre=Tyre{name=‘自行车轮胎’}}




对象作用域



> 在大多数情况下,单例bean是很理想的方案。初始化和垃圾回收对象实例所带来的的成本只留给一些小规模任务,在这些任务中,让对象保持无状态并且在应用中反复重用这些对象可能并不合理。在这种情况下,将class声明为单例的bean会被污染,稍后重用的时候会出现意想不到的问题。 -《spring实战》



Spring定义了多种作用域,可以基于这些作用域创建bean,包括:



| 作用域 | 描述 |

| --- | --- |

| 单例(Singleton) | 在整个应用中,只创建bean的一个实例 |

| 原型(Prototype) | 每次注入或者通过spring应用上下文获取的时候,都会创建一个新的bean实例 |

| 会话(Session) | 在web应用中,为每个会话创建一个bean实例 |

| 请求(Request) | 在web应用中,为每个请求创建一个bean实例 |



1、spring中默认是单例的,我们通过之前的代码演示下



<?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">

<bean id="dog" class="com.kevin.spring.demo3.entity.Animal">

    <property name="name" value="dog"></property>

</bean>





<bean id="cat" class="com.kevin.spring.demo3.entity.Animal" p:name="cat"></bean>



测试



package com.kevin.spring.demo3.test;

import com.kevin.spring.demo3.entity.Animal;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

  • 测试类

*/

public class Test {

public static void main(String[] args) {

    ApplicationContext ctx = new ClassPathXmlApplicationContext("animal.xml");

    Animal dog1 = ctx.getBean("dog",Animal.class);

    Animal dog2 = ctx.getBean("dog",Animal.class);



    System.out.println(dog1 == dog2);



}

}




输出结果



true




这样验证了从容器中取回的对象默认是单例的。



2、设置成Prototype



<?xml version="1.0" encoding="UTF-8"?>

最后

整理的这些资料希望对Java开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

image

image

再免费分享一波我的Java专题面试真题+视频学习详解+Java进阶学习书籍

lic static void main(String[] args) {

    ApplicationContext ctx = new ClassPathXmlApplicationContext("animal.xml");

    Animal dog1 = ctx.getBean("dog",Animal.class);

    Animal dog2 = ctx.getBean("dog",Animal.class);



    System.out.println(dog1 == dog2);



}

}




输出结果



true




这样验证了从容器中取回的对象默认是单例的。



2、设置成Prototype



<?xml version="1.0" encoding="UTF-8"?>

最后

整理的这些资料希望对Java开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

[外链图片转存中…(img-kNH2h7Sg-1628081441658)]

[外链图片转存中…(img-0P46nF8E-1628081441660)]

再免费分享一波我的Java专题面试真题+视频学习详解+Java进阶学习书籍

其实面试这一块早在第一个说的25大面试专题就全都有的。以上提及的这些全部的面试+学习的各种笔记资料,我这差不多来回搞了三个多月,收集整理真的很不容易,其中还有很多自己的一些知识总结。正是因为很麻烦,所以对以上这些学习复习资料感兴趣,

标签:name,Spring,class,源码,Animal,spring,IOC,public,String
来源: https://blog.csdn.net/AUZKAY/article/details/119392997

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有