spring5 bean的自动装配和引入外部文件
1.emp
package com.cj.spring5; public class Emp { private String name; private Dept dept; public void setName(String name) { this.name = name; } public void setDept(Dept dept) { this.dept = dept; } }
2.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="emp" class="com.cj.spring5.Emp" autowire="byName"></bean> <bean id="dept" class="com.cj.spring5.Dept"/> </beans>
3.引入外部文件
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" 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"> <bean id="emp" class="com.cj.spring5.Emp" autowire="byName"></bean> <bean id="dept" class="com.cj.spring5.Dept"/> <!-- 引入外部属性文件--> <context:property-placeholder location="classpath*:jdbc.properties"/> <bean id="druid" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${prop.driverClassName}"></property> <property name="url" value="${prop.url}"/> <property name="username" value="${prop.username}"/> <property name="password" value="${prop.password}"/> </bean> </beans>
jdbc.properties
prop.driverClassName = com.mysql.jdbc.Driver
prop.url = jdbc:mysql://203.195.254.63:3306
prop.username = myTest
prop.password = jx22hmyDtzfHHtLy