您的位置:首页 > 编程语言 > Java开发

spring中的constructor

2015-07-06 14:48 288 查看

【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     }   }

 

转载于:https://www.cnblogs.com/xxsc/p/4624359.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: