03- spring bean标签

6.1 bean标签

6.1.1 bean标签作用

#bean标签作用:配置javaBean对象。spring框架遇到bean标签,默认调用无参数构造方法实例化对象。

6.1.2 bean标签属性

属性 说明
id bean的唯一标识名称。
class 类的全限定名称。
scope 设置bean的作用范围。
取值: singleton : 单例,默认值。 prototype:多例
init-method 指定类中初始化方法的名称,在构造方法执行完毕后立即执行。
destroy-method 指定类中销毁方法名称,在销毁spring容器前执行。

6.1.3 bean作用范围

6.2 项目环境

6.2.1 项目结构

6.2.2 singleton标签

1) 编写bean.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">
    <!--配置dao,说明:
        标签:
            bean:配置javaBean对象
        属性:
            id:bean的唯一标识名称
            class:类的全路径信息
            scope:设置bean的作用范围
                取值:
                    singleton:单例(默认)
                    prototype:多例
        细节:
            默认使用无参数构造方法,创建对象
    -->
    <!--配置dao-->
    <bean id="customerDao" class="cn.guardwhy.dao.impl.CustomerDaoImpl" 
    scope="singleton"></bean>
</beans>

6.2.3 表现层(Controller)

  • CustomerController
package cn.guardwhy.controller;

import cn.guardwhy.dao.CustomerDao;
import cn.guardwhy.service.CustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 客户controller
 */
public class CustomerController {
    
    
    public static void main(String[] args) {
    
    

        // 1.加载spring配置文件,创建ioc容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

        // 2.获取两次dao对象,检查是否是同一个对象
        CustomerDao customerDao1 = (CustomerDao) context.getBean("customerDao");
        CustomerDao customerDao2 = (CustomerDao) context.getBean("customerDao");
        System.out.println(customerDao1 == customerDao2);
        // 3.条件判断
        System.out.println(customerDao1.hashCode());
        System.out.println(customerDao2.hashCode());
    }
}

6.2.4 执行结果

6.2.5 prototype标签

1) 编写bean.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">
    <!--配置dao,说明:
        标签:
            bean:配置javaBean对象
        属性:
            id:bean的唯一标识名称
            class:类的全路径信息
            scope:设置bean的作用范围
                取值:
                    singleton:单例(默认)
                    prototype:多例
        细节:
            默认使用无参数构造方法,创建对象
    -->
    <!--配置dao-->
    <bean id="customerDao" class="cn.guardwhy.dao.impl.CustomerDaoImpl" 
    scope="prototype"></bean>
</beans>

6.2.6 执行结果

6.3 初始化和销毁

6.3.1 编写bean.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">
    <!--配置dao,说明:
        标签:
            bean:配置javaBean对象
        属性:
            id:bean的唯一标识名称
            class:类的全路径信息
            scope:设置bean的作用范围
                取值:
                    singleton:单例(默认)
                    prototype:多例
                    init-method:初始化操作,在调用构造方法执行后执行
                    destroy-method:销毁操作,在销毁spring IOC容器前执行
            注意:默认使用无参数构造方法,创建对象
        -->
    <!--配置dao-->
    <bean id="customerDao" class="cn.guardwhy.dao.impl.CustomerDaoImpl" 
    scope="singleton" init-method="init" destroy-method="destroy"></bean>
</beans>

6.3.2 表现层(Controller)

  • CustomerController
package cn.guardwhy.controller;

import cn.guardwhy.dao.CustomerDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 客户controller
 */
public class CustomerController {
    
    
    public static void main(String[] args) {
    
    
        /**
         bean标签作用:配置javaBean对象
            属性:id:唯一标识名称,class:指定类的全路径
             scope:设置bean的作用范围
             属性取值:
             singleton:单例。默认值
             prototype:多例
             init-method:初始化
             destroy-method:执行销毁操作
         注意:默认使用无参数构造方法实例化
         */

        // 1.加载spring配置文件,创建ioc容器,大工厂接口中没有销毁的方法,在实现类中才存在
        ClassPathXmlApplicationContext context = 
        new ClassPathXmlApplicationContext("bean.xml");
        // 2.获取dao对象
        CustomerDao customerDao = (CustomerDao) context.getBean("customerDao");
        // 3.保存客户
        customerDao.saveCustomer();
        // 4.销毁spring容器
        context.close();
    }
}

6.3.3 执行结果

猜你喜欢

转载自blog.csdn.net/hxy1625309592/article/details/113553730
今日推荐