zoukankan      html  css  js  c++  java
  • spring中管理bean的方式,以及初始化参数的2种方式

    注:constructor-arg:通过构造函数注入。 
           property:通过setter对应的方法注入。

    1、定义一个Class

    public class ExceptionMonitor {

      private String LOG_FILE_PATH;// 日志文件路径
      private String PROJECT_NAME;// 项目名称

      public String getLOG_FILE_PATH() {
        return LOG_FILE_PATH;
      }

      public void setLOG_FILE_PATH(String lOG_FILE_PATH) {
        LOG_FILE_PATH = lOG_FILE_PATH;
      }

      public String getPROJECT_NAME() {
        return PROJECT_NAME;
      }

      public void setPROJECT_NAME(String pROJECT_NAME) {
        PROJECT_NAME = pROJECT_NAME;
      }

    }

    2、在applicationContext.xml中定义一个bean

    <bean class="com.search.Jobs.ExceptionMonitor">
      <property name="LOG_FILE_PATH" value="/opt/apache-tomcat-search-bridge/logs/catalina.out" />
      <property name="PROJECT_NAME" value="search_bridge" />
    </bean>

    3、定义一个类

    package com.search;

    import java.util.List;
    import java.util.Map;

    public class Person {
      private int age;
      private String name;
      private List<String> interestList;
      private Map<String, Integer> friendMap;
      private boolean isMarried;

      public Person(int age, String name, List<String> interestList, Map<String, Integer> friendMap, boolean isMarried) {
        this.age = age;
        this.name = name;
        this.interestList = interestList;
        this.friendMap = friendMap;
      this.isMarried = isMarried;
      }

      public Person() {}

    }

    <bean id="student" class="com.search.Person">
      <constructor-arg name="age" value="26" />
      <constructor-arg name="name" value="student" />
      <constructor-arg name="interestList">
      <list>
        <value>run</value>
        <value>travel</value>
        <value>photography</value>
      </list>
      </constructor-arg>
      <constructor-arg name="friendMap">
        <map>
          <entry key="tom" value="24" />
          <entry key="wilson" value="25" />
        </map>
      </constructor-arg>
      <constructor-arg name="isMarried" value="false" />
    </bean>

    程序加载次类时熟悉的值就会被初始化/注入

  • 相关阅读:
    TWinControl、TCustomControl和TGraphicControl对WM_PAINT消息的三种不同处理(虚函数的特点就是升升降降)
    VCL里的构造函数
    从良难
    TTimer源码研究
    Delphi的RTTI(许多参考链接)
    对ShortCut和TWMKey的研究
    TTimer很特殊
    TEdit的创建与显示过程
    VMware vSphere 服务器虚拟化之二十六 桌面虚拟化之View Persona Management
    Delphi Math里的基本函数,以及浮点数比较函数
  • 原文地址:https://www.cnblogs.com/zhanh247/p/11287429.html
Copyright © 2011-2022 走看看