Home > Java > Spring Bean Autowiring Dengan @Autowired Annotation

Spring Bean Autowiring Dengan @Autowired Annotation

February 19, 2014 Leave a comment Go to comments

Pada Spring framework, @Autowired annotation dapat digunakan untuk melakukan wire bean di setter method, constructor atau field/property. @Autowired annotation melakukan auto wire bean berdasar type data matching ( byType ).


Person class.

public class Person {

private  int id;
private  String name;
private  String address;
public void setId(int id) {
this.id = id;
public int getId() {
return id;
public void setName(String name) {
this.name = name;
public String getName() {
return name;
public void setAddress(String address) {
this.address = address;
public String getAddress() {
return address;

Customer class.

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

public class Customer {

private Person person;
private String loyalty;

public void setLoyalty(String loyalty) {
this.loyalty = loyalty;
public String getLoyalty() {
return loyalty;

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

public void displayInfo(){
System.out.println("Id Customer: " +person.getId());
System.out.println("Name Customer: "+person.getName());
System.out.println("Address Customer: "+person.getAddress());
System.out.println("Loyalty Customer: "+getLoyalty());

Bean Configuration

<context:annotation-config />

<bean id="person" class="com.person.Person">
<property name="id" value="1" ></property>
<property name="name" value="Ben"></property>
<property name="address" value="Indonesia"></property>

<bean id="customer" class="com.person.Person">
<property name="loyalty" value="Gold"></property>


Capture  Result

Capture Result

Referensi : dari berbagai sumber.
Semoga bermanfaat.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: