org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void mainString[] args{ ApplicationContext context=new Configuration file :context.x
Trang 1SPRING A.Spring basic.
1.ApplicationContext and property Initialiation
(ApplicationContext và khởi tạo các thuộc tính ban đầu)
Ví dụ:
Trang 2public void sayHello(){
System.out.print("Hello "+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
Trang 3org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args){
ApplicationContext context=new
Configuration file :context.xml
<?xml version="1.0" encoding="UTF-8"?>
<bean id="hello" class="org.quangthao.Hello">
<property name="name" value="edu"/>
Jul 22, 2013 1:38:47 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [context.xml]
Jul 22, 2013 1:38:48 AM org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons
INFO: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@195d4fe: defining beans
[hello,triangleBean]; root of factory hierarchy
Hello edu
2.Using constructor injection (Sử dụng hàm khởi tạo)
Ví dụ 1:Khởi tạo 2 giá trị type vào height trong configuration file
Trang 4Class Triangle:
package org.koushik.brains;
public class Triangle {
private String type;
private int height;
this.type = type;
this.height = height;
}
public void draw(){
System.out.print("Type :"+type+" Height: "+height );}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getHeight() {
return height;
}
Trang 5public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
<bean id="triangleBean" class="org.koushik.brains.Triangle">
<constructor-arg index="0" value="Hinh vuong"/>
<constructor-arg index="1" value="20"/>
</bean>
</beans>
Kết quả :
Type :Hinh vuong Height: 20
Có thể kết hợp thêm thuộc tính type để ta có thể xác định kiểu của value truyền vào:
<?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
Trang 6<bean id="triangleBean" class="org.koushik.brains.Triangle">
<constructor-arg type="String" value="Hinh vuong"/>
<constructor-arg type="int" value="20"/>
</bean>
</beans>
Kết quả :
Type :Hinh vuong Height: 20
Note :Nếu không thêm index và type thì chường trình sẽ hiểu constructor-arg đầu tiên có index là 0 và lần lượt những constructor sau sẽ có index tăng dần.
Trang 7System out println ( "This is Json Output Generator" ) }
//DI via constructor
public OutputHelper(IOutputGenerator outputGenerator){
this.outputGenerator outputGenerator ;
}
}
3 Spring configuration
Trang 8See below Spring bean configuration, Spring will DI above “JsonOutputGenerator” into this
“OutputHelper” class, via constructor “public OutputHelper(IOutputGenerator outputGenerator)“.
Trang 9public class App {
public static void main(String[] args) {
ApplicationContext context = new
private String name;
private String address;
private int age ;
public Customer(String name, String address, int age) {
this.name name ;
this.address address ;
this.age age ;
Trang 10public Customer(String name, int age, String address) {
this.name name ;
this.age age ;
this.address address ;
}
//getter and setter methods
public String toString(){
return " name : " name + "\n address : "
+ address + "\n age : " age ;
<bean id="CustomerBean" class= "com.mkyong.common.Customer">
<constructor-arg type="java.lang.String">
<value>mkyong</value>
</constructor-arg>
Trang 11<constructor-arg type="java.lang.String">
Trang 13public void setC(Point c) {
C = c;
}
public void draw(){
System.out.println("A("+A.getX()+","+ getY()+")");
System.out.println("B("+B.getX()+","+ getY()+")");
System.out.println("C("+C.getX()+","+ getY()+")");
public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
<bean id="triangleBean" class="org.koushik.brains.Triangle">
<property name="A" ref="pointA"></property>
<property name="B" ref="pointB"></property>
<property name="C" ref="pointC"></property>
</bean>
Trang 14<bean id="pointA" class="org.koushik.brains.Point">
<property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
<bean id="pointB" class="org.koushik.brains.Point">
<property name="x" value="1"></property>
<property name="y" value="2"></property>
</bean>
<bean id="pointC" class="org.koushik.brains.Point">
<property name="x" value="3"></property>
<property name="y" value="4"></property>
public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
ClassPathXmlApplicationContext(new String[] {"context.xml", "daos.xml"
});
Triangle triangle=(Triangle)context.getBean("triangleBean");triangle.draw();
}
Trang 15<bean id="hello" class="org.quangthao.Hello">
<property name="name" value="edu"/>
</bean>
<bean id="triangleBean" class="org.koushik.brains.Triangle">
<property name="A" ref="pointA"></property>
<property name="B" ref="pointB"></property>
<property name="C" ref="pointC"></property>
<bean id="pointA" class="org.koushik.brains.Point">
<property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
<bean id="pointB" class="org.koushik.brains.Point">
<property name="x" value="1"></property>
<property name="y" value="2"></property>
</bean>
<bean id="pointC" class="org.koushik.brains.Point">
<property name="x" value="3"></property>
<property name="y" value="4"></property>
Trang 164.Inner bean,aliases and idref
-Ta có thể lồng thẻ bean trong thẻ bean.
-thẻ alias dùng để tạo các bí danh thông qua một tên -idref dùng để gọi một bean khác thông qua id
Trang 17public void draw(){
System.out.println("A("+A.getX()+","+ getY()+")");
System.out.println("B("+B.getX()+","+ getY()+")");
System.out.println("C("+C.getX()+","+ getY()+")");
<bean id="hello" class="org.quangthao.Hello">
<property name="name" value="edu"/>
</bean>
<bean id="triangleBean" class="org.koushik.brains.Triangle" name="triangle-name">
<property name="A">
<bean class="org.koushik.brains.Point">
<property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
</property>
<property name="B">
<bean class="org.koushik.brains.Point">
<property name="x" value="1"></property>
<property name="y" value="2"></property>
</bean>
Trang 18<property name="C">
<bean class="org.koushik.brains.Point">
<property name="x" value="3"></property>
<property name="y" value="4"></property>
public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
Trang 19org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
5.Initializing collections.(Khởi tạo collection )
Truyền một danh sách các đối tượng (kiểu List,Set,Map,Property)
Class Triangle:
package org.koushik.brains;
import java.util.List;
public class Triangle {
private List<Point> points;
public void draw(){
Trang 20for(Point point:points){
System.out.println("("+point.getX()+","+point.getY()+")");
}}
public List<Point> getPoints() {
return points;
}
public void setPoints(List<Point> points) {
this.points = points;
public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
Trang 21<bean id="triangleBean" class="org.koushik.brains.Triangle" >
<property name="points">
<list>
<ref bean="pointA"/>
<ref bean="pointB"/>
<ref bean="pointC"/>
</list>
</property>
</bean>
<bean id="pointA" class="org.koushik.brains.Point">
<property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
<bean id="pointB" class="org.koushik.brains.Point">
<property name="x" value="1"></property>
<property name="y" value="2"></property>
</bean>
<bean id="pointC" class="org.koushik.brains.Point">
<property name="x" value="3"></property>
<property name="y" value="4"></property>
Trang 22Set addressSet;
Map addressMap;
Properties addressProp;
// a setter method to set List
public void setAddressList(List addressList) {
this.addressList = addressList;
}
// prints and returns all the elements of the list
public List getAddressList() {
System.out.println("List Elements :" + addressList);
return addressList;
}
// a setter method to set Set
public void setAddressSet(Set addressSet) {
this.addressSet = addressSet;
}
// prints and returns all the elements of the Set
public Set getAddressSet() {
System.out.println("Set Elements :" + addressSet);
return addressSet;
}
// a setter method to set Map
public void setAddressMap(Map addressMap) {
this.addressMap = addressMap;
}
// prints and returns all the elements of the Map
public Map getAddressMap() {
System.out.println("Map Elements :" + addressMap);
return addressMap;
}
// a setter method to set Property
public void setAddressProp(Properties addressProp) {
this.addressProp = addressProp;
}
// prints and returns all the elements of the Property
public Properties getAddressProp() {
System.out.println("Property Elements :" + addressProp);
return addressProp;
}
}
Trang 23public class MainApp {
public static void main(String[] args) {
ApplicationContext context =new
ClassPathXmlApplicationContext("Beans.xml");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
<! results in a setAddressList(java.util.List) call >
<property name="addressList">
<list>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</list>
</property>
<! results in a setAddressSet(java.util.Set) call >
Trang 24<property name="addressSet">
<set>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</set>
</property>
<! results in a setAddressMap(java.util.Map) call >
<property name="addressMap">
<map>
<entry key="1" value="INDIA"/>
<entry key="2" value="Pakistan"/>
<entry key="3" value="USA"/>
<entry key="4" value="USA"/>
</map>
</property>
<! results in a setAddressProp(java.util.Properties) call >
<property name="addressProp">
<props>
<prop key="one">INDIA</prop>
<prop key="two">Pakistan</prop>
<prop key="three">USA</prop>
<prop key="four">USA</prop>
Jul 22, 2013 6:01:45 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [Beans.xml]
Jul 22, 2013 6:01:45 PM org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons
INFO: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@13c6a22: defining beans
[javaCollection]; root of factory hierarchy
List Elements :[INDIA, Pakistan, USA, USA]
Set Elements :[INDIA, Pakistan, USA]
Map Elements :{1=INDIA, 2=Pakistan, 3=USA, 4=USA}
Property Elements :{two=Pakistan, one=INDIA, three=USA, four=USA}
Trang 256.Bean autowiring (Tự động tìm các object vào sử dụng chúng trong bean)
autowire có 4 giá trị :no,byName,byType,constructor
Class Triangle:
package org.koushik.brains;
public class Triangle {
private Point pointA;
private Point pointB;
private Point pointC;
public Point getPointA() {
return pointA;
}
Trang 26public void setPointA(Point pointA) {
this.pointA = pointA;
}
public Point getPointB() {
return pointB;
}
public void setPointB(Point pointB) {
this.pointB = pointB;
}
public Point getPointC() {
return pointC;
}
public void setPointC(Point pointC) {
this.pointC = pointC;
}
public void draw(){
System.out.println("A("+pointA.getX()+","+pointA.getY()+")");
System.out.println("B("+pointB.getX()+","+pointB.getY()+")");
System.out.println("C("+pointC.getX()+","+pointC.getY()+")");
}
}
Class DrawingApp:
package org.koushik.brains;
Trang 27import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
<bean id="pointA" class="org.koushik.brains.Point">
<property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
<bean id="pointB" class="org.koushik.brains.Point">
<property name="x" value="1"></property>
<property name="y" value="2"></property>
</bean>
<bean id="pointC" class="org.koushik.brains.Point">
<property name="x" value="3"></property>
<property name="y" value="4"></property>
</bean>
</beans>
Trang 28Kết quả :
A(0,0)
B(1,2)
C(3,4)
7.Understanding bean scope.(Hiểu về phạm vi bean)
*Singleton :đây là thuộc tính scope mặc định,khi không khai báo scope thì
applicate sẽ hiểu scope là singleton.Với scope này ,thì đối tượng được tạo đó sẽ là duy nhất.Ta xem ví dụ sau:
Trang 29public void sayHello(){
System.out.println("Hello "+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
public class Main {
public static void main(String[] args){
ApplicationContext context=new
ClassPathXmlApplicationContext("beansHello.xml");
Hello hello_1=(Hello)context.getBean("hello");
hello_1.setName("A DUCK");
Trang 30<?xml version="1.0" encoding="UTF-8"?>
<bean id="hello" class="org.quangthao.Hello" scope="singleton" >
<property name="name" value=""/>
Jul 22, 2013 5:16:18 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [beansHello.xml]
Jul 22, 2013 5:16:18 PM org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons
INFO: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@127fa12: defining beans [hello]; root
Trang 31public void sayHello(){
System.out.println("Hello "+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
public class Main {
public static void main(String[] args){
ApplicationContext context=new
ClassPathXmlApplicationContext("beansHello.xml");
Hello hello_1=(Hello)context.getBean("hello");
hello_1.setName("A DUCK");
<bean id="hello" class="org.quangthao.Hello" scope="prototype">
<property name="name" value=""/>
</bean>
</beans>
Trang 32Kết quả :
Jul 22, 2013 5:24:32 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@82701e: startup date [Mon Jul 22 17:24:32 ICT 2013]; root of context hierarchy
Jul 22, 2013 5:24:32 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [beansHello.xml]
Jul 22, 2013 5:24:32 PM org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons
INFO: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@1d2b01b: defining beans [hello]; root
of factory hierarchy
Hello A DUCK
Hello
8.ApplicationContextAware and BeanNameAware
Sự kiện và lấy giá trị beanName và applicationContext
private Point pointA;
private Point pointB;
private Point pointC;
private String beanName;
private ApplicationContext context;
public Point getPointA() {
return pointA;
}
public void setPointA(Point pointA) {
this.pointA = pointA;
}
Trang 33public Point getPointB() {
return pointB;
}
public void setPointB(Point pointB) {
this.pointB = pointB;
}
public Point getPointC() {
return pointC;
}
public void setPointC(Point pointC) {
this.pointC = pointC;
}
public void draw(){
System.out.println("A("+pointA.getX()+","+pointA.getY()+")");
System.out.println("B("+pointB.getX()+","+pointB.getY()+")");
System.out.println("C("+pointC.getX()+","+pointC.getY()+")");
Trang 34public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
Trang 35<property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
<bean id="pointB" class="org.koushik.brains.Point">
<property name="x" value="1"></property>
<property name="y" value="2"></property>
</bean>
<bean id="pointC" class="org.koushik.brains.Point">
<property name="x" value="3"></property>
<property name="y" value="4"></property>
Jul 23, 2013 11:03:43 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [context.xml]
Jul 23, 2013 11:03:43 AM org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons
INFO: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@1d2b01b: defining beans
[hello,triangleBean,pointA,pointB,pointC]; root of factory hierarchy
Bean name is :triangleBean
set context and context is :
org.springframework.context.support.ClassPathXmlApplicationContext@82701e: startup date [Tue Jul 23 11:03:43 ICT 2013]; root of context hierarchy
Trang 36public int getX() {
public class Triangle {
private List<Point> points;
public List<Point> getPoints() {
return points;
}
public void setPoints(List<Point> points) {
this.points = points;
}
public void draw(){
for(Point point:points){
System.out.println("A("+point.getX()+","+point.getY()+")");
}}
Trang 37<bean id="triangle1" class="org.koushik.brains.Triangle"
parent="parentTriangle" scope="prototype">
<property name="points">
<list merge="true"><! marge="true" Giữ giá trị cũ của biến points và thêm giá trị mới.marge="false" thì ngược lại,xóa hết giá trị cũ của biến points và thêm giá trị mới >
<ref bean="pointB"/>
</list>
</property>
</bean>
<bean id="triangle2" class="org.koushik.brains.Triangle"
parent="parentTriangle" scope="prototype">
</bean>
<bean id="pointA" class="org.koushik.brains.Point">
<property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
<bean id="pointB" class="org.koushik.brains.Point">
<property name="x" value="1"></property>
<property name="y" value="2"></property>
</bean>
Trang 38<bean id="pointC" class="org.koushik.brains.Point">
<property name="x" value="3"></property>
<property name="y" value="4"></property>
public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
Jul 23, 2013 12:14:49 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [context.xml]
Jul 23, 2013 12:14:49 PM org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons
INFO: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@16acdd1: defining beans
[hello,parentTriangle,triangle1,triangle2,pointA,pointB,pointC]; root of factory hierarchy
public class DrawingApp {
public static void main(String[] args){
Trang 39ApplicationContext context=new
Jul 23, 2013 12:19:54 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [context.xml]
Jul 23, 2013 12:19:54 PM org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons
INFO: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@17f409c: defining beans
[hello,parentTriangle,triangle1,triangle2,pointA,pointB,pointC]; root of factory hierarchy
Trang 40public class Triangle implements InitializingBean ,DisposableBean{
private Point pointA;
private Point pointB;
private Point pointC;
public Point getPointA() {
return pointA;
}
public void setPointA(Point pointA) {
this.pointA = pointA;
}
public Point getPointB() {
return pointB;
}
public void setPointB(Point pointB) {
this.pointB = pointB;
}
public Point getPointC() {
return pointC;
}