标签:2.4 val frame ica err tostring factor new load
<?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"> <!-- 注意 在使用构造方法的时候一旦写了一个有参的构造方法那么就会覆盖掉原本无参的构造方法 需要手动的添加无参构造器 否则会出现错误 No default constructor found index 代表参数的位置 从0开始计算 type 指的是参数的类型,在有多个构造函数时,可以用type来区分,要是能确定是那个构造函数,可以不用写type value 给基本类型赋值 ref 给引用类型赋值 --> <bean id="person_con" class="com.suncl.model.People"> <constructor-arg index="0" type="java.lang.Long" value="1"> </constructor-arg> <constructor-arg index="1" type="com.suncl.model.Student" ref="student_con"></constructor-arg> </bean> <bean id="student_con" class="com.suncl.model.Student"> <constructor-arg index="0" type="java.lang.String" value="sName"></constructor-arg> </bean> </beans>
package com.suncl.model; /** * Created by SCL-PC on 2019/2/27. */ public class Student { private String name; /** * 注意 在使用构造方法的时候一旦写了一个有参的构造方法那么就会覆盖掉原本无参的构造方法 需要手动的添加无参构造器 否则会出现错误 No default constructor found */ public Student() { } public Student(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ‘}‘; } }
package com.suncl.model; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; /** * Created by SCL-PC on 2019/2/26. */ public class People { private Long pid; private String pname; private Student students; private List lists; private Set sets; private Map maps; private Properties properties; /** * 注意 在使用构造方法的时候一旦写了一个有参的构造方法那么就会覆盖掉原本无参的构造方法 需要手动的添加无参构造器 否则会出现错误 No default constructor found */ public People(){ } /** * 有参构造器 * @param pid * @param students */ public People(Long pid, Student students) { this.pid = pid; this.students = students; } public Long getPid() { return pid; } public void setPid(Long pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Student getStudents() { return students; } public void setStudents(Student students) { this.students = students; } public List getLists() { return lists; } public void setLists(List lists) { this.lists = lists; } public Set getSets() { return sets; } public void setSets(Set sets) { this.sets = sets; } public Map getMaps() { return maps; } public void setMaps(Map maps) { this.maps = maps; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "People{" + "pid=" + pid + ", pname=‘" + pname + ‘\‘‘ + ", students=" + students + ", lists=" + lists + ", sets=" + sets + ", maps=" + maps + ", properties=" + properties + ‘}‘; } }
package com.suncl.test; import com.suncl.model.People; import com.suncl.model.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by SCL-PC on 2019/2/27. */ public class TestDI { public static void main(String[] args) { ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml"); /** * 构造器注入 */ People peopleCon = actx.getBean("person_con",People.class); System.out.println(peopleCon.toString()); Student studentCon = actx.getBean("student_con",Student.class); System.out.println(studentCon.toString()); } }
信息: Loading XML bean definitions from class path resource [applicationContext.xml] People{pid=1, pname=‘null‘, students=Student{name=‘sName‘}, lists=null, sets=null, maps=null, properties=null} Student{name=‘sName‘}
<?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="people" class="com.suncl.model.People" > <!--set方式注入Long类型--> <property name="pid" value="1"></property> <!--set方式注入String类型--> <property name="pname" value="suncl"></property> <!--set方式注入对象--> <property name="students"> <ref bean="student"/> </property> <!--set方式注入 List 三个元素--> <property name="lists"> <list> <value>1</value> <ref bean="student"/> <value>vae1</value> </list> </property> <!--set方式注入 Set 三个元素 --> <property name="sets"> <set> <value>1</value> <ref bean="student"/> <value>vae</value> </set> </property> <!--set方式注入 Map 2个元素 --> <property name="maps"> <map> <entry key="m1" value="1"></entry> <entry key="m2" > <ref bean="student" /> </entry> </map> </property> <!--set方式注入 Properties 2个元素 --> <property name="properties"> <props> <prop key="p1">p1</prop> <prop key="p2">p2</prop> </props> </property> </bean> <!--使用set方式注入--> <bean id="student" class="com.suncl.model.Student"> <property name="name" value="sName"></property> </bean> </beans>
package com.suncl.model; /** * Created by SCL-PC on 2019/2/27. */ public class Student { private String name; /** * 注意 在使用构造方法的时候一旦写了一个有参的构造方法那么就会覆盖掉原本无参的构造方法 需要手动的添加无参构造器 否则会出现错误 No default constructor found */ public Student() { } public Student(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ‘}‘; } }
package com.suncl.model; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; /** * Created by SCL-PC on 2019/2/26. */ public class People { private Long pid; private String pname; private Student students; private List lists; private Set sets; private Map maps; private Properties properties; /** * 注意 在使用构造方法的时候一旦写了一个有参的构造方法那么就会覆盖掉原本无参的构造方法 需要手动的添加无参构造器 否则会出现错误 No default constructor found */ public People(){ } /** * 有参构造器 * @param pid * @param students */ public People(Long pid, Student students) { this.pid = pid; this.students = students; } public Long getPid() { return pid; } public void setPid(Long pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Student getStudents() { return students; } public void setStudents(Student students) { this.students = students; } public List getLists() { return lists; } public void setLists(List lists) { this.lists = lists; } public Set getSets() { return sets; } public void setSets(Set sets) { this.sets = sets; } public Map getMaps() { return maps; } public void setMaps(Map maps) { this.maps = maps; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "People{" + "pid=" + pid + ", pname=‘" + pname + ‘\‘‘ + ", students=" + students + ", lists=" + lists + ", sets=" + sets + ", maps=" + maps + ", properties=" + properties + ‘}‘; } }
package com.suncl.test; import com.suncl.model.People; import com.suncl.model.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by SCL-PC on 2019/2/27. */ public class TestDI { public static void main(String[] args) { ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml"); /** * set方式注入 */ People people = actx.getBean("people",People.class); System.out.println(people.toString()); Student student = actx.getBean("student",Student.class); System.out.println(student.toString()); } }
信息: Loading XML bean definitions from class path resource [applicationContext.xml] People{pid=1, pname=‘suncl‘, students=Student{name=‘sName‘}, lists=[1, Student{name=‘sName‘}, vae1], sets=[1, Student{name=‘sName‘}, vae], maps={m1=1, m2=Student{name=‘sName‘}}, properties={p2=p2, p1=p1}} Student{name=‘sName‘}
<?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"> <!--依赖注入 DI--> <bean id="student" class="com.suncl.model.Student" > <property name="name" value="zhangsan"></property> </bean> <bean id="school" class="com.suncl.model.School"> <property name="student" ref="student"></property> </bean> </beans>
package com.suncl.model; /** * Created by SCL-PC on 2019/2/27. */ public class School { private Student student; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } @Override public String toString() { return "School{" + "student=" + student + ‘}‘; } }
package com.suncl.model; /** * Created by SCL-PC on 2019/2/27. */ public class Student { private String name; /** * 注意 在使用构造方法的时候一旦写了一个有参的构造方法那么就会覆盖掉原本无参的构造方法 需要手动的添加无参构造器 否则会出现错误 No default constructor found */ public Student() { } public Student(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ‘}‘; } }
package com.suncl.test; import com.suncl.model.People; import com.suncl.model.School; import com.suncl.model.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by SCL-PC on 2019/2/27. */ public class TestDI { public static void main(String[] args) { ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml"); /** * 依赖注入 创建学校的时候默认注入了一个学生 */ School school = actx.getBean("school",School.class); System.out.println(school.toString()); } }
二月 27, 2019 11:45:34 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] School{student=Student{name=‘zhangsan‘}}
标签:2.4 val frame ica err tostring factor new load
原文地址:https://www.cnblogs.com/reload-sun/p/10447848.html