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方法就可以优雅的停止
}