Spring Framework Depedency Injection Bag 3

Spring

Di tutorial sebelumnya kita sudah membuat program sederhana Depedency Injection.

Pada tutorial ini kita akan menyambung project sebelumnya dengan menambahkan sebuah bean baru

Di tutorial in, kita hanya menyambung project sebelumnya.

Saya buat 3 class model dengan nama HelloA,HelloB dan HelloC.

Class Model

File : HelloA.java

package com.timposu;

public class HelloA {

	private String message;

	public String getmessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

Class diatas akan di inject dengan class HelloB di bawah ini

File : HelloB.java

package com.timposu.belajarspring.model;

public class HelloB {

	private Hello hello; //memanggil class Hello

	public DataHello(Hello hello) {
		super();
		this.hello = hello;
	}

	public Hello getHello() {
		return hello;
	}

	public void setHello(Hello hello) {
		this.hello = hello;
	}

}

File : HelloC.java

package com.timposu.belajarspring.model;

public class HelloC {
	
	private HelloA helloA;
	
	private HelloB helloB;

	public HelloC(HelloA helloA, HelloB helloB) {
		super();
		this.helloA = helloA;
		this.helloB = helloB;
	}

	public HelloA getHelloA() {
		return helloA;
	}

	public void setHelloA(HelloA helloA) {
		this.helloA = helloA;
	}

	public HelloB getHelloB() {
		return helloB;
	}

	public void setHelloB(HelloB helloB) {
		this.helloB = helloB;
	}
	
	
}

Seperti yang dilihat diaatas class HelloC membutuhkan 2 class HelloA dan HelloB.

Spring Java Config Hello World

Disini kita buat bean baru HelloC yang akan menginject bean yang dibutuhkan.

package com.timposu.belajarspring;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.timposu.belajarspring.model.HelloB;
import com.timposu.belajarspring.model.HelloC;
import com.timposu.belajarspring.model.HelloA;

@Configuration
public class SpringConfiguration {
	
	@Bean(name = "ucup")
	public HelloA sayHello1() {
		HelloA hello = new HelloA();
		hello.setMessage("Ucup");
		return hello;
	}
	
	@Bean(name = "timposu")
	public HelloA sayHello2() {
		HelloA hello = new HelloA();
		hello.setMessage("Timposu");
		return hello;
	}
	
	@Bean
	public HelloB hello(@Qualifier("timposu") HelloA hello) {
		HelloB helloB = new HelloB(hello);
		return helloB;
	}
	
	@Bean
	public HelloC helloC(@Qualifier("ucup") HelloA a, HelloB b) {
		HelloC c = new HelloC(a, b);
		return c;
	}
	
}

Main Class

Panggil class Configuration dengan AnnotationConfigApplicationContext

File App.java

package com.timposu.belajarspring.belajarspring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.timposu.belajarspring.belajarspring.model.HelloB;
import com.timposu.belajarspring.belajarspring.model.HelloC;

public class App 
{
    public static void main( String[] args )
    {
    	 ApplicationContext context = 
         		new AnnotationConfigApplicationContext(SpringConfiguration.class);
    	 
    	 HelloC helloC = context.getBean(HelloC.class);
    	 System.out.println(helloC.getHelloA().getMessage());
         System.out.println(helloC.getHelloB().getHello().getMessage());  
     }
}

Run

Ucup
Timposu

DONE.

Download

Download Download : BelajarSpringJavaConfig.zip

Tonton Video


Related Post