Advertisement
package decodejava;
public class Company
{
public void info()
{
System.out.println("Company A");
}
}
package decodejava;
public class Employee
{
Company company ;
String name;
int yearsOfExp;
//Constructor is a must when using autowire="constructor"
public Employee(Company company, String name, int yearsOfExp)
{
this.company = company;
this.name = name;
this.yearsOfExp = yearsOfExp;
}
public Company getCompany()
{
return company;
}
public String getName()
{
return name;
}
public int getYearsOfExp()
{
return yearsOfExp;
}
}
Advertisement
package decodejava;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Utility
{
public static void main(String[] args)
{
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.beans.xml");
Employee employeeBean = context.getBean("EmployeeBean", Employee.class);
System.out.println("Employee Name : " + employeeBean.getName());
System.out.println("Years of experience : " + employeeBean.getYearsOfExp());
employeeBean.getCompany().info();
}
}
<?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 ="EmployeeBean" class="decodejava.Employee" autowire = "constructor">
<constructor-arg index="1" value="Employee1"/>
<constructor-arg index="2" value="7"/>
</bean>
<bean id ="CompanyBean" class="decodejava.Company">
</bean>
</beans>
Jul 17, 2018 11:21:23 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1ee0005: startup date [Tue Jul 17 11:21:23 IST 2018]; root of context hierarchy
Jul 17, 2018 11:21:23 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config.beans.xml]
Employee Name : Employee1
Years of experience : 7
Company A
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement