标签:
【application.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dao" class="com.demo.dao.PersonDAOImpl">
<constructor-arg index="0" type="java.lang.String">
<value>dingding</value>
</constructor-arg>
<constructor-arg index="1" type="java.lang.Integer">
<value>12</value>
</constructor-arg>
</bean>
</beans>
--------------------------------------------
【ReadApplicationContext.java】
public class ReadApplicationContext {
public static void main(String[] args) {
//spring 配置文件的路径,从类路径下加载
String url="applicationContext.xml";
//String url1="ioc/applicationContext.xml";
//读取spring 的配置文件,获取唯一对象名
ApplicationContext act= new ClassPathXmlApplicationContext(url);
//ClassPathXmlApplicationContext act= new ClassPathXmlApplicationContext(url);
//从spring容器中获取boy 的对象
PersonDAO dao1=(PersonDAO)act.getBean("dao");
}
}
----------------------------------
【PersonDAOImpl.java】
package com.demo.dao;
import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set;
public class PersonDAOImpl implements PersonDAO {
private String name; private Integer age; public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public PersonDAOImpl(){} public PersonDAOImpl(Integer age,String name){ this.name=name; this.age=age; System.out.println("我是有参数的构造方法:"+name+"--"+age); } public PersonDAOImpl(String name,Integer age){ this.name=name; this.age=age; System.out.println("我是有参数的构造方法:"+name+"--"+age); } public void save() { // TODO Auto-generated method stub } }
标签:
原文地址:http://www.cnblogs.com/xxsc/p/4624359.html