Spring is Java util. The assignment process of properties of type properties is resolved
•
Java
This article mainly introduces spring as Java util. The assignment process of properties is analyzed. The example code introduced in this paper is very detailed, which has certain reference value for everyone's study or work. Friends in need can refer to it
The properties class represents a persistent set of properties. Properties can be saved in or loaded from a stream. Each key and its corresponding value in the attribute list is a string. In spring, you can use it to store information about connecting to the database.
DataSource. java
package com.gong.spring.beans; import java.util.Properties; public class DataSource { private Properties properties; public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "DataSource [properties=" + properties + "]"; } }
applicationContext. xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="com.gong.spring.beans.DataSource"> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">123456</prop> <prop key="jdbcUrl">jdbc:MysqL:///test</prop> <prop key="driverClass">com.MysqL.jdbc.Driver</prop> </props> </property> </bean> </beans>
Main. java
package com.gong.spring.beans; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //1.创建spring的IOC容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.从容器中获取Bean实例 DataSource dataSource = (DataSource) ctx.getBean("dataSource"); System.out.println(dataSource.toString()); } }
Output:
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码