spring core exp
A.java
package com.jtc.p1;
public class A {
int a;
String msg; //setter injection.
public void setA(int a){ //setter injection
System.out.println("SetA(int a) setter injection in A class");
this.a=a;
}
public void setMsg(String msg){ //setter injection.
System.out.println("setMsg(String msg) setter injection in B class");
this.msg=msg;
}
void showA(){
System.out.println("showA() method in A");
System.out.println(a);
System.out.println(msg);
}
}
B.java
package com.jtc.p1;
public class B {
int b;
String str; //constructor injection.
public B(int b, String str){ //constructor injection
System.out.println("B(int b,String str) constructor injection in B class");
this.b=b;
this.str=str;
}
void showB(){
System.out.println("ShowB() method in B class");
System.out.println(b);
System.out.println(str);
}
}
Hello.java
package com.jtc.p1;
public class B {
int b;
String str; //constructor injection.
public B(int b, String str){ //constructor injection
System.out.println("B(int b,String str) constructor injection in B class");
this.b=b;
this.str=str;
}
void showB(){
System.out.println("ShowB() method in B class");
System.out.println(b);
System.out.println(str);
}
}
Jtc.java
package com.jtc.p1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Jtc {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("spconfig.xml");
Hello h1=(Hello)ctx.getBean("hello");
h1.showHello();
}
}
Jtc.java /without spring
package com.jtc.p1;
public class Jtc {
public static void main(String[] args) {
//without ioc.
A aobj=new A();
aobj.setA(10);
aobj.setMsg("Hello A");
B bobj=new B(20,"Hello B");
Hello h1=new Hello(bobj);
h1.setAobj(aobj);
h1.showHello();
}
}
spconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
<bean id="aobj" class="com.jtc.p1.A">
<property name="a" value="10" />
<property name="msg" value="A is Property value" />
</bean>
<bean id="bobjs" class="com.jtc.p1.B">
<constructor-arg value="20"/>
<constructor-arg value="B is constructor value"/>
</bean>
<bean id="hello" class="com.jtc.p1.Hello">
<property name="aobj" ref="aobj" />
<constructor-arg ref="bobjs"></constructor-arg>
</bean>
</beans>
package com.jtc.p1;
public class A {
int a;
String msg; //setter injection.
public void setA(int a){ //setter injection
System.out.println("SetA(int a) setter injection in A class");
this.a=a;
}
public void setMsg(String msg){ //setter injection.
System.out.println("setMsg(String msg) setter injection in B class");
this.msg=msg;
}
void showA(){
System.out.println("showA() method in A");
System.out.println(a);
System.out.println(msg);
}
}
B.java
package com.jtc.p1;
public class B {
int b;
String str; //constructor injection.
public B(int b, String str){ //constructor injection
System.out.println("B(int b,String str) constructor injection in B class");
this.b=b;
this.str=str;
}
void showB(){
System.out.println("ShowB() method in B class");
System.out.println(b);
System.out.println(str);
}
}
Hello.java
package com.jtc.p1;
public class B {
int b;
String str; //constructor injection.
public B(int b, String str){ //constructor injection
System.out.println("B(int b,String str) constructor injection in B class");
this.b=b;
this.str=str;
}
void showB(){
System.out.println("ShowB() method in B class");
System.out.println(b);
System.out.println(str);
}
}
Jtc.java
package com.jtc.p1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Jtc {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("spconfig.xml");
Hello h1=(Hello)ctx.getBean("hello");
h1.showHello();
}
}
Jtc.java /without spring
package com.jtc.p1;
public class Jtc {
public static void main(String[] args) {
//without ioc.
A aobj=new A();
aobj.setA(10);
aobj.setMsg("Hello A");
B bobj=new B(20,"Hello B");
Hello h1=new Hello(bobj);
h1.setAobj(aobj);
h1.showHello();
}
}
spconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
<bean id="aobj" class="com.jtc.p1.A">
<property name="a" value="10" />
<property name="msg" value="A is Property value" />
</bean>
<bean id="bobjs" class="com.jtc.p1.B">
<constructor-arg value="20"/>
<constructor-arg value="B is constructor value"/>
</bean>
<bean id="hello" class="com.jtc.p1.Hello">
<property name="aobj" ref="aobj" />
<constructor-arg ref="bobjs"></constructor-arg>
</bean>
</beans>
Comments
Post a Comment