六安网站建设全包,网站的产品中心怎么做,做生鲜管理系统的网站,大连鼎信网站建设公司#x1f40c;个人主页#xff1a; #x1f40c; 叶落闲庭 #x1f4a8;我的专栏#xff1a;#x1f4a8; c语言 数据结构 javaweb 石可破也#xff0c;而不可夺坚#xff1b;丹可磨也#xff0c;而不可夺赤。 Spring配置文件和容器相关 一、加载properties文件1.1加载… 个人主页 叶落闲庭 我的专栏 c语言 数据结构 javaweb 石可破也而不可夺坚丹可磨也而不可夺赤。 Spring配置文件和容器相关 一、加载properties文件1.1加载单个properties文件1.2加载多个properties文件1.3加载properties文件小结 二、容器2.1创建容器2.1.1加载类路径下的配置文件2.1.2从文件系统下加载配置文件了解2.1.3加载多个配置文件 2.2获取bean2.2.1类型强转2.2.2多个参数2.2.3按类型获取 总结 一、加载properties文件
1.1加载单个properties文件
properties文件jdbc.properties
jdbc.drivercom.mysql.jdbc.Driver
jdbc.urljdbc:mysql://127.0.0.1:3306/mybatis
jdbc.usernameroot
jdbc.password1234561.开启context命名空间
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
/beans2.使用context命名空间加载指定properties文件
context:property-placeholder locationjdbc.properties/context:property-placeholder locationjdbc.properties/3.使用属性占位符${}读取properties文件中的属性
bean classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName value${jdbc.driver}/property nameurl value${jdbc.url}/property nameusername value${jdbc.username}/property namepassword value${jdbc.password}//bean1.2加载多个properties文件
properties文件1jdbc.properties
jdbc.drivercom.mysql.jdbc.Driver
jdbc.urljdbc:mysql://127.0.0.1:3306/mybatis
jdbc.usernameroot
jdbc.password123456properties文件2jdbc2.properties
usernamezhangsan2.使用context命名空间加载指定properties文件
!--1.开启context命名空间--!--2.使用context空间加载properties文件--context:property-placeholder locationjdbc.properties,jdbc2.properties system-properties-modeNEVER/!--spring加载配置文件--!--3.使用属性占位符${}读取properties文件中的属性--bean classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName value${jdbc.driver}/property nameurl value${jdbc.url}/property nameusername value${jdbc.username}/property namepassword value${jdbc.password}//beanbean idbookDao classcom.practice.dao.impl.BookDaoImplproperty namename value${jdbc.username}/property namename2 value${username}//bean3.最理想的方式使用*.properties即表示加载所有properties文件 context:property-placeholder locationclasspath*:*.properties system-properties-modeNEVER/1.3加载properties文件小结
不加载系统属性 context:property-placeholder locationjdbc.properties system-properties-modeNEVER/加载多个properties文件 context:property-placeholder locationjdbc.properties,jdbc2.properties/加载所有properties文件 context:property-placeholder location*.properties/加载properties文件标准格式 context:property-placeholder locationclasspath:*.properties/从类路径或jar包搜索并加载properties文件 context:property-placeholder locationclasspath*:*.properties/二、容器
2.1创建容器
2.1.1加载类路径下的配置文件
public class App {public static void main(String[] args) {ApplicationContext act new ClassPathXmlApplicationContext(applicationContext.xml);BookDao bookDao (BookDao) act.getBean(bookDao);bookDao.save2();}
}2.1.2从文件系统下加载配置文件了解
public class App {public static void main(String[] args) {//从文件系统下加载配置文件参数为配置文件的绝对路径ApplicationContext act new FileSystemXmlApplicationContext(D:\\storage\\java_practice\\spring-7-30\\src\\main\\resources\\applicationContext.xml);BookDao bookDao (BookDao) act.getBean(bookDao);bookDao.save2();}
}2.1.3加载多个配置文件 ApplicationContext act new ClassPathXmlApplicationContext(bean1.xml,bean2.xml);2.2获取bean
2.2.1类型强转 BookDao bookDao (BookDao) act.getBean(bookDao);2.2.2多个参数 BookDao bookDao act.getBean(bookDao,BookDao.class);2.2.3按类型获取
对应的bean中此类型只能有一个 BookDao bookDao act.getBean(BookDao.class);总结
关于Spring的加载配置文件、容器和获取bean的方式大概就这么多欢迎各位小伙伴点赞关注