Commit 3578f569 authored by Ahsanul Alam's avatar Ahsanul Alam

added dependency injection

spring framework integration
parent 40ddf102
...@@ -13,7 +13,11 @@ ...@@ -13,7 +13,11 @@
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/asm-7.3.1.jar"/> <classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/asm-7.3.1.jar">
<attributes>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/asm-analysis-7.3.1.jar"/> <classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/asm-analysis-7.3.1.jar"/>
<classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/asm-commons-7.3.1.jar"/> <classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/asm-commons-7.3.1.jar"/>
<classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/asm-tree-7.3.1.jar"/> <classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/asm-tree-7.3.1.jar"/>
...@@ -103,5 +107,6 @@ ...@@ -103,5 +107,6 @@
<classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/velocity-1.7.jar"/> <classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/velocity-1.7.jar"/>
<classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/xmlpull-1.1.3.1.jar"/> <classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/xmlpull-1.1.3.1.jar"/>
<classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/xstream-1.4.17.jar"/> <classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts-2.5.28.1/lib/xstream-1.4.17.jar"/>
<classpathentry kind="lib" path="C:/Users/Dohatec/Downloads/struts2-spring-plugin-2.5.28.2.jar"/>
<classpathentry kind="output" path="build/classes"/> <classpathentry kind="output" path="build/classes"/>
</classpath> </classpath>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="brand1" class="dependency_injection.Bike"/>
<bean id="car" class="dependency_injection.Car">
<constructor-arg ref="brand1" />
</bean>
</beans>
...@@ -39,4 +39,13 @@ ...@@ -39,4 +39,13 @@
org.apache.struts2.tiles.StrutsTilesListener org.apache.struts2.tiles.StrutsTilesListener
</listener-class> </listener-class>
</listener> </listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app> </web-app>
\ No newline at end of file
...@@ -10,13 +10,13 @@ ...@@ -10,13 +10,13 @@
<!-- <body> --> <!-- <body> -->
<h4>Welcome To Nepal</h4> <h4>Welcome To Nepal</h4>
<%-- <s:bean name="test.Login" var="hello_bin"> --%> <s:bean name="test.LoginAction" var="hello_bin">
<%-- <s:param name="message">Hello, this is bean tag!</s:param> --%> <s:param name="message">Hello, this is bean tag</s:param>
<%-- </s:bean> --%> </s:bean>
<s:property value="message"/> <s:property value="message"/>
<%-- <p>Bean tage message property value is: <s:property value="hello_bin.message"/> </p> --%> <p>Bean text message property value is: <s:property value="#hello_bin.message"/> </p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<!-- </body> --> <!-- </body> -->
......
...@@ -54,6 +54,12 @@ ...@@ -54,6 +54,12 @@
<action name="register" class="test.RegistrationAction"> <action name="register" class="test.RegistrationAction">
<result name="SUCCESS" type="tiles">registration-success</result> <result name="SUCCESS" type="tiles">registration-success</result>
</action> </action>
<!-- <action name="beanTagAction" class="controllers.BeanTagAction"> -->
<!-- <result name="SUCCESS">/login.jsp</result> -->
<!-- </action> -->
</package> </package>
......
package contollers;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.inject.Inject;
import dependency_injection.I_Vehicle;
public class BeanTagAction extends ActionSupport{
@Inject
I_Vehicle obj;
public I_Vehicle getVehicle() {
return obj;
}
public String execute() {
return SUCCESS;
}
}
package dependency_injection;
public class Bike implements I_Brand {
// public void run() {
// System.out.println("Bike is running...");
// }
public String brandName() {
return "Brand of bike is : Honda";
}
}
package dependency_injection;
public class Car implements I_Vehicle {
I_Brand brand;
public Car(I_Brand brand) {
this.brand = brand;
}
public void run() {
System.out.println("Car is running");
String brnd = brand.brandName();
System.out.println("Brand name: "+brnd);
}
public I_Brand getBrand() {
return brand;
}
public void setBrand(I_Brand brand) {
this.brand = brand;
}
}
package dependency_injection;
public interface I_Brand {
String brandName();
}
package dependency_injection;
public interface I_Vehicle {
void run();
}
...@@ -54,6 +54,12 @@ ...@@ -54,6 +54,12 @@
<action name="register" class="test.RegistrationAction"> <action name="register" class="test.RegistrationAction">
<result name="SUCCESS" type="tiles">registration-success</result> <result name="SUCCESS" type="tiles">registration-success</result>
</action> </action>
<!-- <action name="beanTagAction" class="controllers.BeanTagAction"> -->
<!-- <result name="SUCCESS">/login.jsp</result> -->
<!-- </action> -->
</package> </package>
......
package test; package test;
import dependency_injection.I_Vehicle;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dependency_injection.Car;
import dependency_injection.Bike;
public class LoginAction { public class LoginAction {
...@@ -18,6 +24,7 @@ public class LoginAction { ...@@ -18,6 +24,7 @@ public class LoginAction {
return abc; return abc;
} }
public void setAbc(int abc) { public void setAbc(int abc) {
this.abc = abc; this.abc = abc;
} }
...@@ -33,6 +40,16 @@ public class LoginAction { ...@@ -33,6 +40,16 @@ public class LoginAction {
public String execute() throws Exception{ public String execute() throws Exception{
message = ad.getUser(email, password); message = ad.getUser(email, password);
System.out.println("Message is: "+message);
//dependency injection test
//I_Vehicle vlc = new Car();
//vlc.run();
ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath: applicationContext.xml");
I_Vehicle vcle = (I_Vehicle) appContext.getBean("car");
vcle.run();
if(message.startsWith("Sorry")) return "FAILURE"; if(message.startsWith("Sorry")) return "FAILURE";
return "SUCCESS"; return "SUCCESS";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment