(八)引入外部屬性檔案
阿新 • • 發佈:2021-01-29
建立properties檔案
在XML檔案中因為名稱空間
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">
引入properties檔案並配置屬性
Value中的值對應properties檔案中的key值。
<context:property-placeholder location="classpath:test.properties"/>
<bean id="userDao" class="com.cjy.sping5.UserDaoImpl">
<property name="daoId" value="${prop.name}"> </ property>
</bean>
輸出測試結果
public class UserDaoImpl implements UserDao{
private String DaoId;
public void setDaoId(String daoId) {
DaoId = daoId;
}
@Override
public void update() {
System.out.println(DaoId+"Dao update");
}
}
public void testUser()
{
ApplicationContext context= new ClassPathXmlApplicationContext("bean2.xml");
UserDao userdao= context.getBean("userDao", UserDao.class);
userdao.update();
}