Xml配置里的bean自动装配与使用外部属性文件
来源:互联网 发布:怎样重新注册淘宝账号 编辑:程序博客网 时间:2024/06/14 11:39
1.Xml配置里的bean自动装配
</bean> <!-- 自动装配 将当前实例中所有的对象属性 全部进行填充 byType对象属性所在的类 在容器中 只能拥有一个实例 多个实例是相对的 如果b中是个集合可以装配多个A byName根据当前对象属性的名称来查找对应的实例 --> <bean id="b" class="cn.et.lesson01.autowire.B" autowire="byType" scope="prototype"> </bean>
2. 使用外部属性文件
在配置文件里配置 Bean 时, 有时需要在Bean的配置里混入系统部署的细节信息
PropertyPlaceholderConfigurer 从属性文件里加载属性, 并使用这些属性来替换变量.
配置context:<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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd" >
Bean与context读取文件资源的优先级:
Bean读取资源文件:优先读取配置文件
Context命名空间读取资源文件:优先读取系统文件
读取配置文件的两种方法:配置文件
url=jdbc:mysql://localhost:3306/shopdriverClass=com.mysql.jdbc.DriveruserName1=rootpassword=123
读取:<!-- ${username}是个关键字 默认获取操作系统的用户名Admimistrator-->方法1<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:cn/et/lesson02/jdbc/jdbcmysql.properties"></property></bean>方法2<context:property-placeholder location="classpath:cn/et/lesson02/jdbc/jdbcmysql.properties" /> <bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="${url}"></property> <property name="username" value="${userName1}"></property> <property name="password" value="${password}"></property> <property name="driverClassName" value="${driverClass}"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSouce"></property> </bean>
阅读全文
0 0
- Xml配置里的bean自动装配与使用外部属性文件
- Spring4深入理解IOC&DI02----Bean配置--自动装配、bean之间的继承与依赖、使用外部属性文件
- 简化Spring的xml文件配置-自动装配Bean属性
- 10008---XML 配置里的 Bean 自动装配
- Spring XML配置自动装配的Bean
- Spring全家桶(二)Bean之间的关系、自动装配、作用域和使用外部文件
- 简化Spring的XML配置(一)——自动装配bean属性
- Spring XML 配置里的bean 的自定装配
- Spring-Bean配置-使用外部属性文件
- Spring IOC使用(2) bean的生命周期,配置属性,自动装配
- spring-xml配置bean自动装配
- Spring的《XML显式装配bean》- XML自动装配
- 使用注解+xml配置完成spring的bean的装配
- Spring 配置使用 - Bean 自动装配
- (笔记)Spring实战_最小化Spring XML配置(1)_自动装配Bean属性
- 《Spring3实战》摘要(3-1)--最小化Spring XML配置之自动装配Bean属性
- 【Spring】装配Bean-(1)-使用XML配置
- Spring装配Bean---使用xml配置
- 移动端页面开发的常见问题
- ES6-JS 基本类型和引用类型详解
- stm32函数中大数组问题
- 剑指offer-树的子结构 判断二叉树B是不是A的子结构
- javaDoc 批量生成指令
- Xml配置里的bean自动装配与使用外部属性文件
- C语言union的用法
- Java 构造函数的详解
- err_name_not_resolved 问题
- 基于token的多平台身份认证架构设计
- Swift开源项目精选
- 从百度云下载批量视频到本地
- cocoapods的使用
- 链式前向星教学