package com.bjsxt.pojo; public class People { private int id; private String name; /** *注意这里一旦使用了有参的构造器之后就必须生成这个 * 无参的构造器不然spring会报错No matching constructor found in class ‘People‘ */ public People() { } public People(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "People [id=" + id + ", name=" + name + "]"; } }
<?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="peo" class="com.bjsxt.pojo.People" /> <!--使用有参构造器注入对象 需要set方法和有参构造器支持--> <bean id="peo2" class="com.bjsxt.pojo.People"> <constructor-arg index="0" value="1"></constructor-arg> <constructor-arg index="1" value="zhangsan"></constructor-arg> </bean> </beans>
package com.bjsxt.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.pojo.People; import com.bjsxt.pojo.PeopleFactory; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); People peo = ac.getBean("peo",People.class); System.out.println(peo); People peo2 = ac.getBean("peo2",People.class); System.out.println(peo2); } }
二月 27, 2019 10:59:41 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Wed Feb 27 22:59:41 CST 2019]; root of context hierarchy 二月 27, 2019 10:59:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] People [id=0, name=null] People [id=1, name=zhangsan]
package com.bjsxt.pojo; public class People { private int id; private String name; /** *注意这里一旦使用了有参的构造器之后就必须生成这个 * 无参的构造器不然spring会报错No matching constructor found in class ‘People‘ */ public People() { } public People(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "People [id=" + id + ", name=" + name + "]"; } }
package com.bjsxt.pojo; public class PeopleFactory { /** * 注意这里千万不能定义为stataic的 * @return */ public People newInstance(){ return new People(1,"测试"); } }
<?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="peo" class="com.bjsxt.pojo.People" /> <!–使用有参构造器注入对象 需要set方法和有参构造器支持–> <bean id="peo2" class="com.bjsxt.pojo.People"> <constructor-arg index="0" value="1"></constructor-arg> <constructor-arg index="1" value="zhangsan"></constructor-arg> </bean>--> <bean id="factory" class="com.bjsxt.pojo.PeopleFactory"></bean> <bean id="peo3" factory-bean="factory" factory-method="newInstance"></bean> </beans>
package com.bjsxt.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.pojo.People; import com.bjsxt.pojo.PeopleFactory; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // People peo = ac.getBean("peo",People.class); // System.out.println(peo); // // People peo2 = ac.getBean("peo2",People.class); // System.out.println(peo2); People peo = ac.getBean("peo3",People.class); System.out.println(peo); } }
二月 27, 2019 11:12:24 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Wed Feb 27 23:12:24 CST 2019]; root of context hierarchy 二月 27, 2019 11:12:24 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] People [id=1, name=测试]
package com.bjsxt.pojo; public class PeopleFactory1 { /** * 注意这里静态工厂需要定义为statis的 * @return */ public static People newInstance(){ return new People(1,"测试"); } }
package com.bjsxt.pojo; public class People { private int id; private String name; /** *注意这里一旦使用了有参的构造器之后就必须生成这个 * 无参的构造器不然spring会报错No matching constructor found in class ‘People‘ */ public People() { } public People(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "People [id=" + id + ", name=" + name + "]"; } }
<?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="peo" class="com.bjsxt.pojo.People" /> <!–使用有参构造器注入对象 需要set方法和有参构造器支持–> <bean id="peo2" class="com.bjsxt.pojo.People"> <constructor-arg index="0" value="1"></constructor-arg> <constructor-arg index="1" value="zhangsan"></constructor-arg> </bean>--> <!--<bean id="factory" class="com.bjsxt.pojo.PeopleFactory"></bean>--> <!--<bean id="peo3" factory-bean="factory" factory-method="newInstance"></bean>--> <!--静态工厂注入--> <bean id="peo4" class="com.bjsxt.pojo.PeopleFactory1" factory-method="newInstance" /> </beans>
package com.bjsxt.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.pojo.People; import com.bjsxt.pojo.PeopleFactory; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // People peo = ac.getBean("peo",People.class); // System.out.println(peo); // // People peo2 = ac.getBean("peo2",People.class); // System.out.println(peo2); // People peo3 = ac.getBean("peo3",People.class); // System.out.println(peo3); People peo4 = ac.getBean("peo4",People.class); System.out.println(peo4); } }
二月 27, 2019 11:16:54 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Wed Feb 27 23:16:54 CST 2019]; root of context hierarchy 二月 27, 2019 11:16:55 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] People [id=1, name=测试]
原文:https://www.cnblogs.com/reload-sun/p/10447755.html