Skip to content

Instantly share code, notes, and snippets.

@dimMaryanto93
Last active September 16, 2016 12:37
Show Gist options
  • Save dimMaryanto93/466c313fb585e753cd2e33e6fa6b7660 to your computer and use it in GitHub Desktop.
Save dimMaryanto93/466c313fb585e753cd2e33e6fa6b7660 to your computer and use it in GitHub Desktop.
Spring Framework IOC
package com.hotmail.dimmaryanto.software;
import com.hotmail.dimmaryanto.software.beans.RefBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext springContext = new ClassPathXmlApplicationContext("/SpringConfig.xml");
RefBean bean = springContext.getBean(RefBean.class);
System.out.println("Bean relasi atribute pesan bernilai: " + bean.getDbean().getPesan());
}
}
package com.hotmail.dimmaryanto.software;
import com.hotmail.dimmaryanto.software.beans.Bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext springContext = new ClassPathXmlApplicationContext("/SpringConfig.xml");
Bean b = springContext.getBean(Bean.class);
System.out.println(b.getPesan());
}
}
package com.hotmail.dimmaryanto.software;
import com.hotmail.dimmaryanto.software.beans.DataBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext springContext = new ClassPathXmlApplicationContext("/SpringConfig.xml");
DataBean bean = springContext.getBean(DataBean.class);
System.out.println(bean.getMessage());
}
}
package com.hotmail.dimmaryanto.software;
import com.hotmail.dimmaryanto.software.beans.RefBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext springContext = new ClassPathXmlApplicationContext("/SpringConfig.xml");
RefBean ref = springContext.getBean(RefBean.class);
System.out.println("keterangan: "
+ ref.getNama()
+ " ke "
+ ref.getDbean().getMessage());
}
}
package com.hotmail.dimmaryanto.software.beans;
public class Bean {
private String pesan;
public String getPesan() {
return pesan;
}
public void setPesan(String pesan) {
this.pesan = pesan;
}
}
package com.hotmail.dimmaryanto.software.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component(value = "aBean")
public class Bean {
@Value("Halo ini dari @Component")
private String pesan;
public String getPesan() {
return pesan;
}
public void setPesan(String pesan) {
this.pesan = pesan;
}
}
package com.hotmail.dimmaryanto.software.beans;
public class DataBean {
private String message;
// contructor injection
public DataBean(String message) {
this.message = message;
}
// setter injection
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hotmail.dimmaryanto.software</groupId>
<artifactId>tutorial.springframework-ioc</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Tutorial Spring Framework Inversion Of Control</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
</dependencies>
</project>
package com.hotmail.dimmaryanto.software.beans;
public class RefBean {
private String nama;
private DataBean dbean;
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public DataBean getDbean() {
return dbean;
}
public void setDbean(DataBean dbean) {
this.dbean = dbean;
}
}
package com.hotmail.dimmaryanto.software.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RefBean {
private String nama;
private Bean dbean;
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public Bean getDbean() {
return dbean;
}
@Autowired
public void setDbean(Bean dbean) {
this.dbean = dbean;
}
}
<?xml version="1.0" encoding="windows-1252"?>
<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-4.3.xsd
">
<bean id="aBean" class="com.hotmail.dimmaryanto.software.beans.Bean">
<!-- meng inject nilai menggunakan setter injection -->
<property name="pesan" value="Nama saya, Dimas Maryanto"/>
</bean>
<bean id="dataBean" class="com.hotmail.dimmaryanto.software.beans.DataBean">
<!-- meng inject nilai menggunakan constructor injection -->
<constructor-arg name="message" value="Constructor injection"/>
</bean>
</beans>
<?xml version="1.0" encoding="windows-1252"?>
<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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache">
<context:component-scan base-package="com.hotmail.dimmaryanto.software"/>
</beans>
<?xml version="1.0" encoding="windows-1252"?>
<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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache">
<context:component-scan base-package="com.hotmail.dimmaryanto.software"/>
<bean id="dataBean" class="com.hotmail.dimmaryanto.software.beans.DataBean">
<constructor-arg name="message" value="Constructor injection"/>
</bean>
<bean id="dBean" class="com.hotmail.dimmaryanto.software.beans.RefBean">
<property name="nama" value="Ini bean yang memiliki ref"/>
<!-- ref digunakan mengambil id dari bean yang ada -->
<property name="dbean" ref="dataBean"/>
</bean>
</beans>
<?xml version="1.0" encoding="windows-1252"?>
<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-4.3.xsd
">
<bean id="aBean" class="com.hotmail.dimmaryanto.software.beans.Bean">
<property name="pesan" value="Nama saya, Dimas Maryanto"/>
</bean>
<bean id="dataBean" class="com.hotmail.dimmaryanto.software.beans.DataBean">
<constructor-arg name="message" value="Constructor injection"/>
</bean>
<bean id="dBean" class="com.hotmail.dimmaryanto.software.beans.RefBean">
<property name="nama" value="Ini bean yang memiliki ref"/>
<!-- ref digunakan mengambil id dari bean yang ada -->
<property name="dbean" ref="dataBean"/>
</bean>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment