`
king520
  • 浏览: 167440 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
文章分类
社区版块
存档分类
最新评论

关于资源文件中的字段是如何注入到spring bean中的

 
阅读更多

我们为了不硬编码,把一些配置型的数据或者常量配置到properties文件中:

eg:

jdbc.meeting.url = 192.168.1.6:1521:test
jdbc.meeting.username =system
jdbc.meeting.password =oracle
hibernate.show_sql=false

ftp.ipAdrress=192.168.1.33
ftp.port=21
ftp.username=guo
ftp.password=111111

ftp.localPathRoot=D://ftpLocalFiles//
ftp.remotePathRoot=/generatorFiles/

然后我们直接在代码中调用bean的一个常量获取这个值。

private String ftpIpAddress;
	private int ftpPort;
	private String ftpUserName;
	private String ftpPassword;
	
	//FTP路径配置
	private String ftpLocalPathRoot;
	private String ftpRemotePathRoot;
	
	public String getFtpLocalPathRoot() {
		return ftpLocalPathRoot;
	}

	public void setFtpLocalPathRoot(String ftpLocalPathRoot) {
		this.ftpLocalPathRoot = ftpLocalPathRoot;
	}

	public String getFtpRemotePathRoot() {
		return ftpRemotePathRoot;
	}

	public void setFtpRemotePathRoot(String ftpRemotePathRoot) {
		this.ftpRemotePathRoot = ftpRemotePathRoot;
	}

	public void setFtpIpAddress(String ftpIpAddress) {
		this.ftpIpAddress = ftpIpAddress;
	}

	public void setFtpPort(int ftpPort) {
		this.ftpPort = ftpPort;
	}

	public void setFtpUserName(String ftpUserName) {
		this.ftpUserName = ftpUserName;
	}

	public void setFtpPassword(String ftpPassword) {
		this.ftpPassword = ftpPassword;
	}
	
	public String getFtpIpAddress() {
		return ftpIpAddress;
	}

	public int getFtpPort() {
		return ftpPort;
	}

	public String getFtpUserName() {
		return ftpUserName;
	}

	public String getFtpPassword() {
		return ftpPassword;
	}
}

即可获取对应的值。做法:

配置corePropertyConfigurer

    <bean id="corePropertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:sys-config.properties</value>
			</list>
		</property>
	</bean>

配置service或bean的时候 进行注入:

 <bean id="ftpService"
	    class="com.wirelesscity.tools.ftp.FtpServiceImpl">
	    <property name="ftpIpAddress">
			<value>${ftp.ipAdrress}</value>
		</property>
		<property name="ftpPort">
			<value>${ftp.port}</value>
		</property>
		<property name="ftpUserName">
			<value>${ftp.username}</value>
		</property>
		<property name="ftpPassword">
			<value>${ftp.password}</value>
		</property>
		<property name="ftpLocalPathRoot">
			<value>${ftp.localPathRoot}</value>
		</property>
		<property name="ftpRemotePathRoot">
			<value>${ftp.remotePathRoot}</value>
		</property>
	</bean>



分享到:
评论

相关推荐

    spring动态向容器中添加bean和删除指定bean.md

    spring动态向容器中添加bean和删除指定bean,不需要重启应用

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring攻略(第二版 中文高清版).part2

    3.7 在你的Bean中引入行为 132 3.7.1 问题 132 3.7.2 解决方案 132 3.7.3 工作原理 132 3.8 为你的Bean引入状态 135 3.8.1 问题 135 3.8.2 解决方案 135 3.8.3 工作原理 135 3.9 用基于XML的配置...

    ssh(structs,spring,hibernate)框架中的上传下载

     在配置完LobHandler后, 还需要将其注入到sessionFactory的Bean中,下面是调用后的sessionFactory Bean的配置:  代码 6 将lobHandler注入到sessionFactory中的配置 1. 2. … 3. <bean id="sessionFactory" 4. ...

    Spring攻略(第二版 中文高清版).part1

    3.7 在你的Bean中引入行为 132 3.7.1 问题 132 3.7.2 解决方案 132 3.7.3 工作原理 132 3.8 为你的Bean引入状态 135 3.8.1 问题 135 3.8.2 解决方案 135 3.8.3 工作原理 135 3.9 用基于XML的配置...

    Spring核心注解深入解析:提升开发效率

    @Autowired 注解用于自动注入依赖,它可以放置在字段、构造器、setter方法或其他任何方法上,Spring容器会自动寻找并注入正确的bean。 @Configuration 和 @Bean 注解用于Java配置,允许开发者用程序的方式定义Spring...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring-data-mongodb-encrypt:轻量级的库,用于在mongodb + spring中进行简单轻松的按字段加密

    允许将任何字段都标记为@Encrypted以进行逐字段加密。 产品特点 透明地集成到spring-data-mongodb 支持嵌套的集合,地图和bean 高性能(无反射,优化了加密) 密钥版本控制(以帮助迁移到新密钥而无需转换数据)...

    Spring+3.x企业应用开发实战光盘源码(全)

     第4章:讲解如何在Spring配置文件中使用Spring 3.0的Schema格式配置Bean的内容,并对各个配置项的意义进行了深入的说明。  第5章:对Spring容器进行解构,从内部探究Spring容器的体系结构和运行流程。此外,我们...

    Spring 3 Reference中文

    第一部分 Spring framework 概述..5 第1 章 Spring Framework 介绍..6 1.1 依赖注入和控制反转.6 1.2 模块6 1.2.1 核心容器.7 1.2.2 数据访问/ 整合..7 1.2.3 Web ..8 1.2.4 AOP...

    spring security 参考手册中文版

    Spring Security 参考 1 第一部分前言 15 1.入门 16 2.介绍 17 2.1什么是Spring Security? 17 2.2历史 19 2.3版本编号 20 2.4获得Spring安全 21 2.4.1使用Maven 21 Maven仓库 21 Spring框架 22 2.4.2 Gradle 23 ...

    maven3+struts2+spring+ibatis

    maven3+struts2+spring+ibatis,本来是用maven3+struts2+spring+hibernate但考虑到hibernate在多表级联查询的时候执行效率不高,所以改用性能更好不过sql比较麻烦的的ibatis,本项目只有登录和插入数据,仅供参考: ...

    spring3.1中文参考文档

    spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................

    陈开雄 Spring+3.x企业应用开发实战光盘源码.zip

     第4章:讲解如何在Spring配置文件中使用Spring 3.0的Schema格式配置Bean的内容,并对各个配置项的意义进行了深入的说明。  第5章:对Spring容器进行解构,从内部探究Spring容器的体系结构和运行流程。此外,我们...

    springmybatis

    1. 从配置文件(通常是XML配置文件中)得到 sessionfactory. 2. 由sessionfactory 产生 session 3. 在session 中完成对数据的增删改查和事务提交等. 4. 在用完之后关闭session 。 5. 在java 对象和 数据库之间有做...

    叮当书城项目-叮当书城项目部署代码视频教程带源码(java毕业设计项目-java练手项目)

    4、controller文件是控制层,导入service层,因为service中的方法是我们使用到的,controller通过接收前端传过来的参数进行业务操作,在返回一个指定的路径或者数据表。比如bookcontroller里面就是对书籍进行增删改...

    springMVC-annotation注解介绍

    @Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入。 1、共同点 两者都可以写在字段和setter方法上。两者...

    Spring +Spring MVC+Mybatis 整合框架源码

    SSM 整合后的源码 ,适合初学者 学习。jdk eclipse 都是新版本 安装了个 sts插件Spirng(不是4的版本就行)。里面有简单注释。用的数据库mysql 忘了放上去了。...自己按着bean 字段创建表就行了 。

    fieldmeta基于springboot的字段元数据管理,通用代码生成框架.zip

    SpringBoot采用 JavaConfig的方式对Spring进行配置,并且提供了大量的注解,极大的提高了工作效率,比如@Configuration和@bean注解结合,基于@Configuration完成类扫描,基于@bean注解把返回值注入IOC容器。...

    springMVC+mybit+mysql的一个小例子

    简单实现的功能是:mysql中有user表,从页面访问,然后springMVC的控制类负责控制跳转,最终执行dao类对应的mybits配置文件中mapper文件中对应的sql语句,然后返回结果并传给前台。 以下是复制readMe.txt: 如果需要...

Global site tag (gtag.js) - Google Analytics