加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ApplicationContext.xml文件配置的两种方式

发布时间:2020-12-16 05:29:32 所属栏目:百科 来源:网络整理
导读:今天无意中发现在 ApplicationContext.xml文件中 获取外置文件的参数采用的是context,而不是 spring,就想着在spring的IOC中是不是也有一种方式可以解决这个问题,经过查找,发现是可以的通过某些内置的类,就可以实现: 废话少说: 第一种: beans xmlns="

今天无意中发现在ApplicationContext.xml文件中获取外置文件的参数采用的是context,而不是 spring,就想着在spring的IOC中是不是也有一种方式可以解决这个问题,经过查找,发现是可以的通过某些内置的类,就可以实现:

废话少说:

第一种:

<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">

<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />

。。。。。。。。。。
</bean>

第二种通过IOC/DI形式注入(此处为转载):

< ?xml version="1.0" encoding="UTF-8"?>

  < !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

  < beans>

  < bean id="configproperties"

  class="org.springframework.beans.factory.config.PropertiesFactoryBean">

  < property name="location" value="file:config.properties"/>

  < /bean>

  < bean id="propertyConfigurer"

  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  < property name="properties" ref="configproperties"/>

  < /bean>

  < bean id="tjtaskcode" class="TJTaskCode">

  < property name="taskcode" value="${TJ.TaskCode}"/>

  < /bean>

  < /beans>

  Config.properties文件的配置

  本例中我提供一对简单的数据用于示范:

  #Transaction Journal Task Codes

  TJ.TaskCode = 1034,1035,1037,1038,1040,1057,1058,1074

  TJ.TaskCode是键,1034,1074是值;

  .Java Bean的定义

  定义Java Bean TJTaskCode.Java用于存放所需要的数值:

  

public class TJTaskCode {

  private String taskcode;

  public void setTaskcode(String taskcode) {

  this.taskcode = taskcode;

  }

  public String getTaskcode() {

  return this.taskcode;

  }

  }

******************

测试程序TestAccessProperties.java的执行

  

import org.springframework.context.ApplicationContext;

  import org.springframework.context.support.ClassPathXmlApplicationContext;

  import com.td.cc.audit.impl.TJTaskCode;

  public class TestAccessProperties {

  public static void main(String[] args) {

  ApplicationContext context;

  context = new ClassPathXmlApplicationContext("applicationContext.xml"); TJTaskCode taskcode1 = (TJTaskCode)context.getBean("tjtaskcode");

  String taskcode2 = taskcode1.getTaskcode();

  System.out.println(taskcode2);

  if (taskcode2.indexOf("1034")!=-1) //

  {

  System.out.println("Y");

  } else{

  System.out.println("N");

  }

  }

  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读