spring学习笔记(5)—— Bean

spring学习笔记(5)—— Bean

Bean

类别描述
名称bean
所属beans标签
功能定义Spring核心容器管理的对象
格式
属性列表id:bean的id,使用容器可以通过id值获取对应的bean , 在一个容器中的id值是唯一的
class:bean的类型 ,全路径名
name:别名同样可以获取bean,可以有多个name(使用逗号or分号or空格分割)
scope:singleton(单例 默认) prototype(非单例) 容器单例只有一个对象

bean实例化

构造方法

1.提供可以访问的构造方法
无参方法
2.配置
<bean id = "名" class="位置">

静态工厂

提供一个静态工厂

工厂里面有静态方法new需要的对象

配置

bean实例化的是工厂类,属性factory-method 是用那个方法去实例化需要的对象

代码

工厂代码

package com.test.code;

public class StudentFactory {
    public static Student OrderStudent(){
        return new Student();
    }
}

学生代码

package com.test.code;

public class Student {
    private String name;
    private String id;


    public void studentDoing(){
        System.out.println("这是个学生类");
    }



    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

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

配置文件

<?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="studentFactory" class="com.test.code.StudentFactory" factory-method="OrderStudent"/>

</beans>

测试类

package com.test.code;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student)applicationContext.getBean("studentFactory");
        student.studentDoing();
    }
}

效果:这是个学生类

实例化工厂

实例化工厂

类中有方法返回需要的对象

配置

先实例化工厂对象
实例化工厂中的对象
factory-bean从那个工厂实例化来的
factory-method 那个方法

代码

工厂类

package com.test.code;

public class StudentFactory {
    public  Student OrderStudent(){
        return new Student();
    }
}

学生类同上

配置文件

<?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="studentFactory" class="com.test.code.StudentFactory" />
    <bean id="student" factory-bean="studentFactory" factory-method="OrderStudent"/>

</beans>

测试类

package com.test.code;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student)applicationContext.getBean("student");
        student.studentDoing();
    }
}

效果:这是个学生类

使用FactoryBean实例化bean

工厂继承FactoryBean
implements FactoryBean<Student> <>里面是要实例化的对象
重写两个方法
getObject() new对象
getObjectType()要实例化的class

配置类class填入工厂类

配置单例 isSingleton()方法

代码

工厂类

package com.test.code;

import org.springframework.beans.factory.FactoryBean;

public class StudentFactory implements FactoryBean<Student> {

    @Override
    public Student getObject() throws Exception {
        return new Student();
    }

    @Override
    public Class<?> getObjectType() {
        return Student.class;
    }
}

学生类同上
配置类

<?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="student" class="com.test.code.StudentFactory"/>

</beans>

测试类

package com.test.code;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student)applicationContext.getBean("student");
        student.studentDoing();
    }
}

效果 :这是个学生类

bean生命周期

初始化容器

创建对象
执行构造方法
执行属性注入
执行bean的初始化操作

使用bean

执行业务操作

关闭/销毁容器

执行bean销毁方法

配置生命周期

方法

init-method:初始化函数
destroy-method:销毁函数
<bean id="student" class="com.test.code.Student" init-method="init" destroy-method="destory"/>

代码

学生类

package com.test.code;

public class Student {
    private String name;
    private String id;
    public  void init(){
        System.out.println("init……");
    }
    public  void destory(){
        System.out.println("destory……");
    }

    public void studentDoing(){
        System.out.println("这是个学生类");
    }



    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

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

配置

<?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="student" class="com.test.code.Student" init-method="init" destroy-method="destory"/>

</beans>

测试类

package com.test.code;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student)applicationContext.getBean("student");
        student.studentDoing();
    }
}

效果:
init……
这是个学生类

bean销毁

上边的程序可以看到实例化的bean没有运行销毁的方法

关闭容器

applicationContext.close();
效果:
init……
这是个学生类
destory……

注册关闭勾子

applicationContext.registerShutdownHook();
效果:
init……
这是个学生类
destory……

区别

关闭容器之后就使用不了容器,需要在最后使用
关闭勾子可以写在任何地方

接口写bean生命周期

InitializingBean 初始化
DisposableBean 销毁

代码

学生类

package com.test.code;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Student implements InitializingBean , DisposableBean {
    private String name;
    private String id;
    public  void init(){
        System.out.println("init……");
    }
    public  void destory(){
        System.out.println("destory……");
    }

    public void studentDoing(){
        System.out.println("这是个学生类");
    }



    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

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

    @Override
    public void destroy() throws Exception {
        System.out.println("student  init");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("student  destory");
    }
}

配置文件

<?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="student" class="com.test.code.Student" init-method="init" destroy-method="destory"/>

</beans>

测试

package com.test.code;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        applicationContext.registerShutdownHook();
        Student student = (Student)applicationContext.getBean("student");
        student.studentDoing();

    }
}

``
效果
student  destory
init……
这是个学生类
student  init
destory……






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值