zoukankan      html  css  js  c++  java
  • day38 08-Spring的id、name和scope顺序

    访问的路径的是/login.id不允许出现特殊的字符。/是特殊的字符。Struts 2已经没有/,action的名字已经不带/了。现在的开发中一般使用id这个属性即可。


     这个类在被Spring创建的时候是一个单例的还是一个多例的?

    reuqest和session都是在web开发中使用.它们的区别是web的域不同.

    package cn.itcast.spring3.demo3;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Bean的作用范围
     * @author zhongzh
     *
     */
    
    
    public class SpringTest3 {
        @Test
        public void demo1(){
            ApplicationContext applicationContext = new  ClassPathXmlApplicationContext("applicationContext.xml");
            //这是工厂对象    
            Customer c1 = (Customer) applicationContext.getBean("customer");
             System.out.println(c1);
             
             Customer c2 = (Customer) applicationContext.getBean("customer");
             System.out.println(c2);
             //如果c1和c2的地址一样,那么它只被实例化一次
             
        
        
        
        }
        
        
        
    
    }
    package cn.itcast.spring3.demo3;
    
    public class Customer {
    
        public Customer() {
            super();
            System.out.println("Customer类被实例化.........");
        }
        
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- 别去schema,schema是文件,本地的文件,你得引那个头 -->
    
    <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">
    <!-- demo1快速入门================================================= -->
    <!-- 把接口和实现类在这个配置文件中配置,有了实现类的全路径之后到时候才能用工厂反射 -->
       
       <!-- 通过一个<bean>标签来设置类的信息,通过id属性为类起个标识. -->
        <!-- 接口,实现类,配置文件也都有了 -->
        <!-- 现在有一个工厂Spring为我们提供好了,其实就是解析这个XML文件 -->
        <!-- 这个工厂你自己写会不会写?你用dom4j找里面的bean标签,找到class的属性值,然后就可以Class.forName()反射生成类的实例.其实Spring
             也是这么做的,只不过工厂由Spring提供好了
         -->
        <bean id="helloService" class="cn.itcast.spring3.demo1.HelloServiceImpl">
             <!-- 使用<property>标签注入属性 
             value指的是普通值
             ref指的是对象
             -->
        <property name="info"  value="传智播客"></property>
        </bean>
        <!-- demo1快速入门 -->
        <!-- demo2Bean的实例化 -->
        <!-- 默认情况下使用的就是无参数的构造方法. -->
        <!-- 
        <bean id="bean1" class="cn.itcast.spring3.demo2.Bean1"></bean>
        -->
        <bean name="bean1" class="cn.itcast.spring3.demo2.Bean1"></bean>
        <!-- 第二种使用静态工厂实例化 不能写class了,因为现在不是由Spring直接帮你创建对象了-->
        <bean id="bean2" class="cn.itcast.spring3.demo2.Bean2Factory" factory-method="getBean2"></bean>
        <!-- 第三种使用实例工厂实例化 -->
        <bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
        <!-- 要先把Bean3Factory实例化 -->
        <bean id="bean3Factory" class="cn.itcast.spring3.demo2.Bean3Factory"></bean>
        <!-- demo2Bean的实例化======================  end-->
        
        <!-- demo3Bean的作用范围======================= -->
        <bean id="customer" class="cn.itcast.spring3.demo3.Customer" scope="prototype">
        </bean>
    </beans>

    这就是bean的id和scope这几个属性。如果是scope="prototype",那么被获取多次就被实例化多次;如果是scope="singleton",那么被获取多次也只被实例化一次。

  • 相关阅读:
    SCOM 初探 [SCOM应用系列之一]
    SCOM 安装部署 [SCOM应用系列之二]
    CMMI 配置管理(Configuration Management)系列(1) 简介
    设计模式总结之创建型设计模式
    tabbar图片渲染的问题
    react实现自定义hooks(节流和防抖)
    前端工程化5js源码编译和ast
    react实现自定义hooks(跑马灯)
    react实现自定义hooks(倒计时)
    react实现自定义hooks(移动端拖拽)
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/6721914.html
Copyright © 2011-2022 走看看