1、下载最新spring包
http://repo.spring.io/release/org/springframework/spring/5.0.6.RELEASE/
解压后将spring-framework-5.0.6.RELEASE\libs下的jar放到工程lib下,并build path。
2、创建get/set类文件(jdbcMap.java),将所需参数加上get set方法。
3、创建beans.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-3.0.xsd">
<bean id="jdbcMap" class="cn.kq3.main.jdbcMap">
<property name="jdbc" value="jdbc:sqlserver://10.10.1.15:1433"/>
</bean>
</beans>
注意bean id对应类名,class对应类的位置
4、创建主程序mainApp,
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
jdbcMap obj = (jdbcMap) context.getBean("jdbcMap");
System.out.println(obj.getJdbc());
}
}