web.xml
<bean id="contact" class="com.vf.constructor.objectmatch.ContactBean">
<constructor-arg name="age" value="28" />
<constructor-arg name="name" value="Maggie Q" />
</bean> ContactBean.java
package com.vf.constructor.objectmatch;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ContactBean {
/**
* @param args
*/
private int age;
private String name;
public ContactBean(int age, String name) {
this.age = age;
this.name = name;
System.out.println("Age: " + age + " Name: " + name);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("web.xml");
context.getBean("contact", ContactBean.class);
}
}
Age: 28 Name: Maggie Q
本文展示了一个使用Spring框架通过构造器注入属性的例子。具体实现包括定义ContactBean类,并通过web.xml配置文件设置构造器参数。

5349

被折叠的 条评论
为什么被折叠?



