??個(gè)人主頁(yè): ?? 葉落閑庭
??我的專(zhuān)欄:??
c語(yǔ)言
數(shù)據(jù)結(jié)構(gòu)
javaweb
石可破也,而不可奪堅(jiān);丹可磨也,而不可奪赤。
一、加載properties文件
1.1加載單個(gè)properties文件
- properties文件:
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis
jdbc.username=root
jdbc.password=123456
- 1.開(kāi)啟context命名空間
<?xml version="1.0" encoding="UTF-8"?>
<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>
- 2.使用context命名空間,加載指定properties文件
<context:property-placeholder location="jdbc.properties"/><context:property-placeholder location="jdbc.properties"/>
- 3.使用屬性占位符
${}
讀取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>
1.2加載多個(gè)properties文件
- properties文件1:
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis
jdbc.username=root
jdbc.password=123456
- properties文件2:
jdbc2.properties
username=zhangsan
- 2.使用context命名空間,加載指定properties文件
<!--1.開(kāi)啟context命名空間-->
<!--2.使用context空間加載properties文件-->
<context:property-placeholder location="jdbc.properties,jdbc2.properties" system-properties-mode="NEVER"/>
<!--spring加載配置文件-->
<!--3.使用屬性占位符${}讀取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>
<bean id="bookDao" class="com.practice.dao.impl.BookDaoImpl">
<property name="name" value="${jdbc.username}"/>
<property name="name2" value="${username}"/>
</bean>
- 3.最理想的方式,使用
*.properties
即表示加載所有properties文件
<context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>
1.3加載properties文件小結(jié)
- 不加載系統(tǒng)屬性
<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
- 加載多個(gè)properties文件
<context:property-placeholder location="jdbc.properties,jdbc2.properties"/>
- 加載所有properties文件
<context:property-placeholder location="*.properties"/>
- 加載properties文件標(biāo)準(zhǔn)格式
<context:property-placeholder location="classpath:*.properties"/>
- 從類(lèi)路徑或jar包搜索并加載properties文件
<context:property-placeholder location="classpath*:*.properties"/>
二、容器
2.1創(chuàng)建容器
2.1.1加載類(lèi)路徑下的配置文件
public class App {
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao = (BookDao) act.getBean("bookDao");
bookDao.save2();
}
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-617686.html
2.1.2從文件系統(tǒng)下加載配置文件(了解)
public class App {
public static void main(String[] args) {
//從文件系統(tǒng)下加載配置文件,參數(shù)為配置文件的絕對(duì)路徑
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加載多個(gè)配置文件
ApplicationContext act = new ClassPathXmlApplicationContext("bean1.xml","bean2.xml");
2.2獲取bean
2.2.1類(lèi)型強(qiáng)轉(zhuǎn)
BookDao bookDao = (BookDao) act.getBean("bookDao");
2.2.2多個(gè)參數(shù)
BookDao bookDao = act.getBean("bookDao",BookDao.class);
2.2.3按類(lèi)型獲取
- 對(duì)應(yīng)的bean中此類(lèi)型只能有一個(gè)
BookDao bookDao = act.getBean(BookDao.class);
總結(jié)
關(guān)于Spring的加載配置文件、容器和獲取bean的方式大概就這么多,歡迎各位小伙伴點(diǎn)贊+關(guān)注?。。?/font>文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-617686.html
到了這里,關(guān)于Spring的加載配置文件、容器和獲取bean的方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!