Last active
September 16, 2016 12:37
-
-
Save dimMaryanto93/466c313fb585e753cd2e33e6fa6b7660 to your computer and use it in GitHub Desktop.
Spring Framework IOC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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