Monday 1 April 2013

Autowire a Bean Spring

SpringBeans.xml


 
 
 
  
  
 
  
 
  
 
 
Customer.java
package com;

import org.springframework.beans.factory.annotation.Autowired;

public class Customer {

 @Autowired
 private Person person;
 private int type;
 private String action;

 public Person getPerson() {
  return person;
 }

 public void setPerson(Person person) {
  this.person = person;
 }

 public int getType() {
  return type;
 }

 public void setType(int type) {
  this.type = type;
 }

 public String getAction() {
  return action;
 }

 public void setAction(String action) {
  this.action = action;
 }

 @Override
 public String toString() {
  return "Customer [person=" + person + ", type=" + type + ", action="
    + action + "]";
 }

 
}
Person.java
package com;

public class Person {
 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 @Override
 public String toString() {
  return "Person [name=" + name + "]";
 }

 
}
App.java
package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
 public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");

  Customer cust = (Customer) context.getBean("customer");
  Person per = (Person) context.getBean("person");
  
  System.out.println(cust);
  System.out.println(per);
 }
}
Output: 
Customer [person=Person [name=harit], type=1, action=buy]
Person [name=harit]

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...