对学习SpringIOC的一些总结(xml)

SpringIOC总结

IOC容器概念

​ IoC全称为Inversion of Control,意为控制反转,是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入,还有一种方式叫“依赖查找”。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。

IOC容器初始化方式

ClassPathXmlApplicationContext

public static void main( String[] args ) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applcationContext.xml");
}
//"applcationContext.xml"为配置文件的名字
//可以使用通配符*例如:applcation*.xml表示所有以applcation开头的所有配置文件

AnnotationConfigApplicationContext

public static void main( String[] args ) {
    ApplicationContext ctx = new AnnotationConfigApplicationContext("applcationContext.xml");
}
//"applcationContext.xml"为配置文件的名字
//可以使用通配符*例如:applcation*.xml表示所有以applcation开头的所有配置文件

Bean的作用域

  • prototype(非单例)

  • singleton(默认)(单例)

<bean id="laowang" class="com.wb.test.Person" scope="prototype">
        <property name="age" value="23"/>
        <property name="name" value="唐宋"/>
        <property name="sex" value=""/>
        <property name="hobby" value="dnf"/>
</bean>
//通过scope设置作用域,不设置的时候为默认值singleton

Bean的生命周期

  • Singleton Bean的生命周期

    容器初始化的时候就会实例化,容器关闭的时候被销毁

  • Prototype Bean的生命周期

    使用该bean的时候才会被实例化,容器关闭的时候被销毁

  • 如何指定生命周期的回调方法

    xml中的init-method、destroy-method

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Person {
    private String name;
    private String sex;
    private int age;
    private String hobby;
      
    public void init(){
        System.out.println(name + "已创建");
  
    }
    public void destroy(){
        System.out.println(name + "已销毁");
  
    }
}
<bean id="laowang" class="com.wb.test.Person" init-method="init" destroy-method="destroy">
        <property name="age" value="23"/>
        <property name="name" value="唐宋"/>
        <property name="sex" value=""/>
        <property name="hobby" value="dnf"/>
</bean>
//init和destroy是person类中的方法名

Bean懒加载

lazy-init属性

默认是false

<bean id="laowang" class="com.wb.test.Person" lazy-init="true">
        <property name="age" value="23"/>
        <property name="name" value="唐宋"/>
        <property name="sex" value=""/>
        <property name="hobby" value="dnf"/>
</bean>
//当lazy-init属性设置为true是表示为该bean为懒加载,只有在使用该bean的时候才会实例化该bean

Bean初始化方式

构造方法方式(最常用)

静态工厂方法

<bean id="stu" class="com.lanou.test.StudentGetStatic" factory-method="studentGet">    		<constructor-arg name="name" value="学生" />
</bean>
//静态工厂的方法不需要额外创建bean
/*
* 被实例化的类
* */
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Student {    
    private String name;    
    public Student(String name) {        
        this.name = name;    
    }    
}
/*
* 静态工厂类
* */
public class StudentGetStatic {    
    public static Student studentGet(String name){        
        Student stu = new Student(name);        
        return stu;    
    }
}

实例工厂方法

<bean id="studentGet" class="com.lanou.test.StudentGet" />
<bean id="student" factory-bean="studentGet" factory-method="studentGet">
    <constructor-arg name="name" value="学生" />
</bean>
//实例化工厂方法需要额外实例化一个bean来调用工厂的方法
/*
* 被实例化的类
* */
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Student {    
    private String name;    
    public Student(String name) {        
        this.name = name;    
    }    
}
/*
* 非静态工厂类
* */
public class StudentGetStatic {    
    public Student studentGet(String name){        
        Student stu = new Student(name);        
        return stu;    
    }
}

如何优雅的停止非Web Spring应用

public static void main( String[] args ) {    
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applcationContext.xml"); 
    ctx.registerShutdownHook();//初始化容器的时候调用registerShutdownHook方法就可以优雅的停止    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值