ApplicationContext.xml文件配置的两种方式
来源:互联网 发布:高一优化方案英语答案 编辑:程序博客网 时间:2024/06/14 23:40
今天无意中发现在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,1035,1037,1038,1040,1057,1058,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");
}
}
}
- ApplicationContext.xml文件配置的两种方式
- spring的applicationContext.xml 两种配置方式
- applicationContext.xml的两种加载方式
- applicationContext.xml加载的两种方式:
- applicationContext.xml 配置的两种方法
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- ApplicationContext的三种实现方式以及在web.xml配置的两种方式
- J2EE 加载Spring的applicationContext.xml文件的两种方式
- Spring配置文件applicationContext.xml的两种拆分方式
- Spring配置文件applicationContext.xml的两种拆分方式
- applicationContext.xml文件的配置
- 配置applicationcontext.xml文件
- applicationContext.xml文件配置
- Android图形子系统详解
- 黑马程序员JAVA基础-面向对象
- android 开发--获取文件的扩展名和去掉文件的扩展名
- TSLIB移植
- Linux设备模型之input子系统详解
- ApplicationContext.xml文件配置的两种方式
- ThinkPHP分页的实现
- Android Studio使用教程(一)
- Java程序优化的一些最佳实践
- 排它平方数
- linux 3.4.10 内核内存管理源代码分析2:伙伴系统
- 宋宝华 《Linux设备驱动开发详解》示例代码之基本字符设备驱动
- getopt_long() 参数详解
- Java中的equals学习小结