zoukankan      html  css  js  c++  java
  • 使用service&scope 进行注入

    @service 声明该类为一个bean,bean的名称为类名首字母小写(customerService)

    @Scope("prototype")则声明为一个原子类型,既每个getbean方法返回一个实例

    package spring_service;
    
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Service;
    
    /**
     * Created by luozhitao on 2017/8/10.
     */
    @Service
    @Scope("prototype")
    public class CustomerService {
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        private String message;
    
    
    }
    package spring_service;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Created by luozhitao on 2017/8/10.
     */
    public class service_app {
    
        public static void main(String [] args){
    
            ApplicationContext context=new ClassPathXmlApplicationContext("bean_service.xml");
    
            CustomerService customerService=(CustomerService)context.getBean("customerService");
    
            customerService.setMessage("spring server method");
    
            System.out.println(customerService.getMessage());
    
    
    
    
        }
    
    
    }
    <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="spring_service" />
    
    </beans>

    bean.xml看起来就非常简练。

  • 相关阅读:
    毛笔算法 毛笔签名效果
    手写输入控件
    全栈工程师成长路线
    配置msdtc
    流行的广告轮播(图片轮播)JS代码!!
    水晶报表放上去网站会爆:bobj错误的
    查询所有存储过程
    VB.NET and C# Comparison
    SQL查询案例:多行转换为一行
    SQL Server行列转换[转]
  • 原文地址:https://www.cnblogs.com/luo-mao/p/7340600.html
Copyright © 2011-2022 走看看