web.xml
<bean id="contactBean" class="com.vf.constructor.objectmatch.Contact" init-method="init" destroy-method="destroy">
<property name="name" value="charles fels"></property>
</bean>
Contact.java
package com.vf.constructor.objectmatch;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Contact {
/**
* @param args
*/
private String name;
private int age;
private static Logger logger = Logger.getLogger(Contact.class.getPackage().getName());
public void init() {
if(name == null) {
logger.debug("You are not authenticated.");
throw new IllegalArgumentException ("Initialization failed.");
}
}
public void destroy() {
logger.debug("Destroying starting.");
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public String toString() {
return "Name: " + name + " Age: " + age;
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("web.xml");
Contact contact = context.getBean("contactBean", Contact.class);
logger.debug(contact);
((AbstractApplicationContext) context).close();
}
}
DEBUG - Name: charles fels Age: 0
DEBUG - Destroying starting.