Spring_引用外部属性文件_bean配置_找不到文件_怎么指定文件路径

今天使用Spring引用外部属性文件时,发现引用不了?

Spring 引用外部属性文件_bean配置_怎么指定文件路径

我写的测试
1、一个使用外部属性文件的应用类

package com.exec.Ioc.T5;
//应用类,定义了引用URL和端口号
public class Application {
	private String url;
	private String port;
	
	public String getPort() {
		return port;
	}
	public void setPort(String port) {
		this.port = port;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
}

2、引用文件test.properties放置位置和内容内容(引用的内容要是键值对形式)
我放在包内
在这里插入图片描述
内容:
url = http://127.0.0.1
port = 5901

3、配置xml文件:

	<!-- 方式一 -->
	<bean id="data" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="locations">
			<list>
				 <!-- 包内文件指定层级结构的路径,resources文件下的文件直接写文件名称加后缀 -->
				<value>classpath:com/exec/Ioc/T5/test.properties</value>
			</list>
		</property>
	</bean>
	
	<!-- 方式二:单个文件可使用这种方式 -->
	<!-- <context:property-placeholder location="classpath:com/exec/Ioc/T5/test.properties"/> -->
	
	<bean id="application_bean" class="com.exec.Ioc.T5.Application">
		<property name="port" value="${port}"></property>	
		<property name="url" value="${url}"></property>	
	</bean>

方式一:
首先需要 class 引入org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer这个配置类,并且这种方式可以引入多个文件,使用locations配置list集合的值,指向多个文件路径;
其次我的配置文件放在com.exec.Ioc.T5/test.properties包内,但是在路径中却不能这么写,因为在文件解析后实际的文件路劲会把.间的连接变成文件的层级结构,那么文件路径应为com/exec/Ioc/T5/test.properties
方式二:
这中方式小巧便捷,<context:property-placeholder>标签会实现对类的引用,不需要我们手动写了,但是只能用于映入单个文件,并且两种方式不可一起起作用,这种方式实际上也是使用了方式一。
4、调用main方法测试:

package com.exec.Ioc.T5;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test_Main {
	//main方法测试
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		ApplicationContext apc=new ClassPathXmlApplicationContext("T5.xml");
		Application app=apc.getBean("application_bean",Application.class);
		System.out.println("URL:"+app.getUrl());
		System.out.println("Port:"+app.getPort());
		
	}
}	

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴收到您的请求。我可以为您提供一份Java代码,用于解析本地spring bean配置XML文件,并获取提供给外部使用的接口,然后输出结果信息包括ID、接口、别名和引用等。 这里是代码: ``` import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.io.ClassPathResource; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class SpringBeanParser { public static void main(String[] args) { // 设置XML文件路径和需要获取的bean名称 final String beanName = "myBean"; final String xmlPath = "applicationContext.xml"; // 创建GenericApplicationContext实例 GenericApplicationContext context = new GenericApplicationContext(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context); // 将XML文件中的bean定义读入到Spring上下文中 try { reader.loadBeanDefinitions(new ClassPathResource(xmlPath)); } catch (BeanDefinitionStoreException ex) { System.out.println("无法解析bean定义:" + ex.getMessage()); return; } // 在Spring上下文中查bean定义 BeanDefinition beanDefinition; try { beanDefinition = context.getBeanFactory().getBeanDefinition(beanName); } catch (NoSuchBeanDefinitionException ex) { System.out.println("不到名为" + beanName + "的bean定义:" + ex.getMessage()); return; } // 获取bean定义中的ID、接口、别名和引用 final String id = beanDefinition.getBeanClassName(); final List<String> interfaces = Arrays.asList(beanDefinition.getInterfaceNames()); final String alias = String.join(",", context.getAliases(beanName)); final List<String> refs = Arrays.stream(beanDefinition.getDependsOn()) .map(ref -> context.getBean(ref).toString()) .collect(Collectors.toList()); // 打印结果 System.out.println("ID: " + id); System.out.println("接口: " + interfaces); System.out.println("别名: " + alias); System.out.println("引用: " + refs); } } ``` 请确认您已将XML文件命名为`applicationContext.xml`并放在类路径中,以使程序正确运行。 如果您对此有任何疑问或需要进一步的帮助,请随时联系我。谢谢!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值