Config.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.xsd">
<bean id="injectBean" class="spring.byType.Inject" autowire="byType">
<property name="name">
<value>mukesh</value>
</property>
</bean>
<bean id="personBean" class="spring.byType.Person">
<property name="com">
<value>google</value>
</property>
</bean>
</beans>
Inject.java
---------------
package spring.byType;
public class Inject {
private String name;
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person
person) {
this.person = person;
}
public void setName(String name)
{
this.name = name;
}
public String getName() {
return name;
}
}
Main.java
------------
package spring.byType;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
public class Main {
public static void main(String[] args) {
Resource res = new FileSystemResource("src/main/java/spring/byType/config.xml");
BeanFactory beanFactory = new XmlBeanFactory(res);
Inject demo = (Inject)
beanFactory.getBean("injectBean");
System.out.println(demo.getName());
System.out.println(demo.getPerson().getCom());
}
}
Person.java
------------
package spring.byType;
public class Person {
private String com;
public String getCom() {
return com;
}
public void setCom(String com) {
this.com = com;
}
}
output
------------
mukesh
google
No comments:
Post a Comment