zoukankan      html  css  js  c++  java
  • Bean作用域实例

    1.单例

    <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-2.5.xsd">
    
           <bean id="customerService" 
                class="com.yiibai.customer.services.CustomerService" />
            
    </beans>

    执行结果:

    package com.yiibai.common;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.yiibai.customer.services.CustomerService;
    
    public class App 
    {
        public static void main( String[] args )
        {
            ApplicationContext context = 
             new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
    
            CustomerService custA = (CustomerService)context.getBean("customerService");
            custA.setMessage("Message by custA");
            System.out.println("Message : " + custA.getMessage());
            
            //retrieve it again
            CustomerService custB = (CustomerService)context.getBean("customerService");
            System.out.println("Message : " + custB.getMessage());
        }
    }

    输出结果:

    Message : Message by custA
    Message : Message by custA 

    2.原型

    在原型作用域,必须为每个 getBean()方法中调用返回一个新的实例。

    <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-2.5.xsd">
    
       <bean id="customerService" class="com.yiibai.customer.services.CustomerService" 
             scope="prototype"/>
            
    </beans>

    3. Bean作用域注释

    注释来定义 bean 的作用域

    package com.yiibai.customer.services;
    
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Service;
    
    @Service
    @Scope("prototype")
    public class CustomerService 
    {
        String message;
        
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }

    启用自动组件扫描

    <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-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
           <context:component-scan base-package="com.yiibai.customer" />
            
    </beans>
  • 相关阅读:
    jQuery的Autocomplete插件的远程url取json数据的问题
    DataTable 转换成 Json的3种方法
    IFRAME练习 各种调用
    <%: Html.DisplayNameFor与 <%: Html.DisplayFor的区别
    MVC中@Html.DisPlayFor(model=>model.newsName)和 @Model.newsName的区别
    编程思想:如何设计一个好的通信网络协议
    编程思想:巧用位运算重构代码
    编程思想:小谈网关项目中的设计模式
    Unity应用架构设计(13)——日志组件的实施
    Unity应用架构设计(12)——AOP思想的实践
  • 原文地址:https://www.cnblogs.com/diaoniwa/p/6490828.html
Copyright © 2011-2022 走看看