• 03spring初始化销毁自动装配


    <bean id="users" class="com.fz.entity.User" init-method="abc" destroy-method="destory" scope="singleton">
    </bean>

    spring容器使用注解
    beans.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 注解扫描相关包路径 -->
    <context:annotation-config/>
    <context:component-scan base-package="com.fz.entity"/>

    </beans>

    初始化方法
    配置方式
    init-method="abc" 容器在构造方法完成后,直接执行abc方法

    scope="prototype" 如果是多实例,则是每次使用,都要执行构造方法,同时执行初始化方法

    scope="singleton" 单例模式,是默认的,是立即装载 都要执行构造方法,同时执行初始化方法

    注解方式
    package com.fz.entity;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Controller;
    import org.springframework.stereotype.Service;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    /**
    * Created by webrx on 2017-07-03.
    */
    //@Controller("uok")
    //@Component("uok")
    @Service("uok") //@Scope("prototype")
    @Scope("singleton")
    public class User {
    public User(){
    System.out.println("构造方法()..");
    }
    @PostConstruct 初始化方法
    public void init(){
    System.out.println("init--初始化...");
    }
    @PreDestroy
    public void destory(){
    System.out.println("销毁方法");
    }
    public void show(){
    System.out.println("数据显示........");
    }
    @PostConstruct
    public void abc(){
    System.out.println("abc..");
    }
    }


    销毁方法
    配置方式
    destroy-method="destory"

    当容器销毁对象时,会自动调用destory方法
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    User u = ctx.getBean("users",User.class);
    ctx.close();

    注解方式
    package com.fz.entity;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Controller;
    import org.springframework.stereotype.Service;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    /**
    * Created by webrx on 2017-07-03.
    */
    //@Controller("uok")
    //@Component("uok")
    @Service("uok") //@Scope("prototype")
    @Scope("singleton")
    public class User {
    public User(){
    System.out.println("构造方法()..");
    }
    @PostConstruct
    public void init(){
    System.out.println("init--初始化...");
    }
    @PreDestroy //销毁方法
    public void destory(){
    System.out.println("销毁方法");
    }
    public void show(){
    System.out.println("数据显示........");
    }
    @PostConstruct
    public void abc(){
    System.out.println("abc..");
    }
    }

    自动装配
    <!-- autowire自动装配 简化spring配置
    no不使用自动装配
    byName 根据名称(set方法名来的)去查找相应的bean,如果有则装配上
    byType 根据类型进行自动装配 不用管bean的id.但是同一种类型的bean只能有一个。建议慎用
    constructor 当通过构造器 注入 实例化bean时 适用byType的方式 装配构造方法
    -->
    byName
    byType
    default-autowire="byName"

    <bean id="user" class="com.fz.entity.User" autowire="byType"/>

    <bean id="book" class="com.fz.entity.Book">
    <property name="id" value="100"/>
    <property name="name" value="《计算机编程入门》"/>
    </bean>

    <bean id="st" class="com.fz.entity.Student">
    <property name="sno" value="111"/>
    <property name="name" value="李四国"/>
    </bean>

    <bean id="sts" class="com.fz.entity.Student">
    <property name="sno" value="222"/>
    <property name="name" value="张三丰"/>
    </bean>

    <?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd" default-autowire="byName">

    <!-- 注解扫描相关包路径 -->
    <context:annotation-config/>
    <context:component-scan base-package="com.fz.entity"/>

    <!-- -->
    <!--
    <bean id="user" class="com.fz.entity.User">
    <property name="book" ref="book"/>
    </bean>
    -->

    <bean id="user" class="com.fz.entity.User"/>

    <bean id="book" class="com.fz.entity.Book">
    <property name="id" value="100"/>
    <property name="name" value="《计算机编程入门》"/>
    </bean>

    <bean id="st" class="com.fz.entity.Student">
    <property name="sno" value="111"/>
    <property name="name" value="李四国"/>
    </bean>

    <bean id="student" class="com.fz.entity.Student">
    <property name="sno" value="222"/>
    <property name="name" value="张三丰"/>
    </bean>

    </beans>

    怕什么真理无穷,进一步有一步的欢喜
  • 相关阅读:
    数字类型内置方法
    流程控制之while循环
    流程控制之if判断
    基本运算符
    格式化输出的三种方式
    Python与用户交互
    解压缩
    布尔值(bool)
    django基础 -- 8.cookie 和 session
    为博客园文章添加目录的方法
  • 原文地址:https://www.cnblogs.com/Mkady/p/7201213.html
Copyright © 2020-2023  润新知