个人主页: 叶落闲庭
我的专栏:
c语言
数据结构
javaweb
石可破也,而不可夺坚;丹可磨也,而不可夺赤。
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis
jdbc.username=root
jdbc.password=123456
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
beans>
<context:property-placeholder location="jdbc.properties"/><context:property-placeholder location="jdbc.properties"/>
${}
读取properties文件中的属性<bean class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis
jdbc.username=root
jdbc.password=123456
jdbc2.properties
username=zhangsan
<context:property-placeholder location="jdbc.properties,jdbc2.properties" system-properties-mode="NEVER"/>
<bean class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="bookDao" class="com.practice.dao.impl.BookDaoImpl">
<property name="name" value="${jdbc.username}"/>
<property name="name2" value="${username}"/>
bean>
*.properties
即表示加载所有properties文件 <context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>
<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
<context:property-placeholder location="jdbc.properties,jdbc2.properties"/>
<context:property-placeholder location="*.properties"/>
<context:property-placeholder location="classpath:*.properties"/>
<context:property-placeholder location="classpath*:*.properties"/>
public class App {
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao = (BookDao) act.getBean("bookDao");
bookDao.save2();
}
}
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();
}
}
ApplicationContext act = new ClassPathXmlApplicationContext("bean1.xml","bean2.xml");
BookDao bookDao = (BookDao) act.getBean("bookDao");
BookDao bookDao = act.getBean("bookDao",BookDao.class);
BookDao bookDao = act.getBean(BookDao.class);
关于Spring的加载配置文件、容器和获取bean的方式大概就这么多,欢迎各位小伙伴点赞+关注!!!