SPRING
A.Spring basic.
!"
Ví dụ:
Mô hình :
Class Hello:
package org.quangthao;
public class Hello {
String name;
public Hello() {
super();
}
public Hello(String name) {
super();
this.name = name;
}
public void sayHello(){
System.out.print("Hello "+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Class Main:
package org.quangthao;
import org.springframework.cglib.proxy.Factory;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args){
ApplicationContext context=new
ClassPathXmlApplicationContext("context.xml");
Hello hello=(Hello)context.getBean("hello");
hello.sayHello();
}
}
Configuration file :context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" />xmlns:xsi=" />xsi:schemaLocation=" /> />2.5.xsd">
<bean id="hello" class="org.quangthao.Hello">
<property name="name" value="edu"/>
</bean>
<bean id="triangleBean"
class="org.koushik.brains.Triangle"></bean>
</beans>
Kết quả:
Jul 22, 2013 1:38:47 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1256ea2: startup date
[Mon Jul 22 01:38:47 ICT 2013]; root of context hierarchy
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
#$%&%'()*&+"
Ví dụ 1:Khởi tạo 2 giá trị type vào height trong configuration file .
Class Triangle:
package org.koushik.brains;
public class Triangle {
private String type;
private int height;
public Triangle() {
super();
}
public Triangle(String type, int height) {
super();
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;
}
Class DrawingApp:
package org.koushik.brains;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
ClassPathXmlApplicationContext("context.xml");
Triangle triangle=(Triangle)context.getBean("triangleBean");
triangle.draw();
}
}
context.xml
Theo index :theo tham số truyền vào,tham số đầu tiên có index bằng 0,và tham số
tiếp theo sẽ là 1,…2…3…
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" />xmlns:xsi=" />xsi:schemaLocation=" /> />2.5.xsd">
<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=" />xmlns:xsi=" />xsi:schemaLocation=" /> />2.5.xsd">
<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.
Ví dụ 2 :
1. IOutputGenerator
An interface and implementation class of it.
package com.mkyong.output;
public interface IOutputGenerator
{
public void generateOutput();
}
package com.mkyong.output.impl;
import com.mkyong.output.IOutputGenerator;
public class JsonOutputGenerator implements IOutputGenerator
{
public void generateOutput(){
System.out.println("This is Json Output Generator");
}
}
2. Helper class
A helper class, later use Spring to DI the IOutputGenerator, via constructor.
package com.mkyong.output;
import com.mkyong.output.IOutputGenerator;
public class OutputHelper {
IOutputGenerator outputGenerator;
public void generateOutput() {
outputGenerator.generateOutput();
}
//DI via constructor
public OutputHelper(IOutputGenerator outputGenerator){
this.outputGenerator = outputGenerator;
}
}
3. Spring conguraon
See below Spring bean configuration, Spring will DI above “JsonOutputGenerator” into this
“OutputHelper” class, via constructor “public OutputHelper(IOutputGenerator outputGenerator)“.
<beans xmlns=" />xmlns:xsi=" />xsi:schemaLocation=" /> />
<bean id="OutputHelper" class="com.mkyong.output.OutputHelper">
<constructor-arg>
<ref bean="JsonOutputGenerator" />
</constructor-arg>
</bean>
<bean id="JsonOutputGenerator"
class="com.mkyong.output.impl.JsonOutputGenerator" />
</beans>
4. Run it
Load everything, and run it.
package com.mkyong.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.output.OutputHelper;
public class App {
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext(
"SpringBeans.xml");
OutputHelper output =
(OutputHelper)context.getBean("OutputHelper");
output.generateOutput();
}
}
Ví dụ 3:
package com.mkyong.common;
public class Customer
{
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;
}
public 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;
}
}
<beans xmlns=" />xmlns:xsi=" />xsi:schemaLocation=" /> />
<bean id="CustomerBean" class="com.mkyong.common.Customer">
<constructor-arg type="java.lang.String">
<value>mkyong</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>188</value>
</constructor-arg>
<constructor-arg type="int">
<value>28</value>
</constructor-arg>
</bean>
</beans>
,'&-'%
Ví dụ 1:
Class Point:
package org.koushik.brains;
public class Point {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Class Triangle:
package org.koushik.brains;
public class Triangle {
private Point A;
private Point B;
private Point C;
public Point getA() {
return A;
}
public void setA(Point a) {
A = a;
}
public Point getB() {
return B;
}
public void setB(Point b) {
B = b;
}
public Point getC() {
return C;
}
public void setC(Point c) {
C = c;
}
public void draw(){
System.out.println("A("+A.getX()+","+A.getY()+")");
System.out.println("B("+B.getX()+","+B.getY()+")");
System.out.println("C("+C.getX()+","+C.getY()+")");
}
}
Class DrawingApp:
package org.koushik.brains;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
ClassPathXmlApplicationContext("context.xml");
Triangle triangle=(Triangle)context.getBean("triangleBean");
triangle.draw();
}
}
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" />xmlns:xsi=" />xsi:schemaLocation=" /> />2.5.xsd">
<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>
<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>
Kết quả :
A(0,0)
B(1,2)
C(3,4)
Note :Ta cũng có thể khai báo các đối tượng riêng một file xml,và liên kết các đối
tượng đó riêng một file.
Class Point và class Triangle như ở trên:
Class DrawingApp:
package org.koushik.brains;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
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();
}
}
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" /> xmlns:xsi=" /> xsi:schemaLocation=" /> />beans.xsd">
<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>
</beans>
daos.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" /> xmlns:xsi=" /> xsi:schemaLocation=" /> />beans.xsd">
<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>
Kết quả :
A(0,0)
B(1,2)
C(3,4)
./%%0
-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 .
Class Triangle:
package org.koushik.brains;
public class Triangle {
private Point A;
private Point B;
private Point C;
public Point getA() {
return A;
}
public void setA(Point a) {
A = a;
}
public Point getB() {
return B;
}
public void setB(Point b) {
B = b;
}
public Point getC() {
return C;
}
public void setC(Point c) {
C = c;
}
public void draw(){
System.out.println("A("+A.getX()+","+A.getY()+")");
System.out.println("B("+B.getX()+","+B.getY()+")");
System.out.println("C("+C.getX()+","+C.getY()+")");
}
}
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" /> xmlns:xsi=" /> xsi:schemaLocation=" /> />beans.xsd">
<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>
</property>
<property name="C">
<bean class="org.koushik.brains.Point">
<property name="x" value="3"></property>
<property name="y" value="4"></property>
</bean>
</property>
</bean>
<alias name="triangleBean" alias="triangle-alias"/>
</beans>
Class DrawingApp:
Ta có thể gọi theo tên bí danh đã được xây dựng bằng hàm alias trong file
context.xml.
package org.koushik.brains;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
ClassPathXmlApplicationContext("context.xml");
Triangle triangle=(Triangle)context.getBean("triangle-
alias");
triangle.draw();
}
}
Kết quả:
A(0,0)
B(1,2)
C(3,4)
Ta có thể gọi thông qua thuộc tính name của bean:
package org.koushik.brains;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
ClassPathXmlApplicationContext("context.xml");
Triangle triangle=(Triangle)context.getBean("triangle-
name");
triangle.draw();
}
}
Kết quả :
A(0,0)
B(1,2)
C(3,4)
12&%3"
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(){
for(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;
}
}
Class DrawingApp:
package org.koushik.brains;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args){
ApplicationContext context=new
ClassPathXmlApplicationContext("context.xml");
Triangle triangle=(Triangle)context.getBean("triangleBean");
triangle.draw();
}
}
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" /> xmlns:xsi=" /> xsi:schemaLocation=" /> />beans.xsd">
<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>
</bean>
</beans>
Kết quả :
(0,0)
(1,2)
(3,4)
Ví dụ 2:
Class JavaCollection:
package com.tutorialspoint;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class JavaCollection {
List addressList;
Set 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;
}
}
Class MainApp:
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =new
ClassPathXmlApplicationContext("Beans.xml");
JavaCollection
jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressList();
jc.getAddressSet();
jc.getAddressMap();
jc.getAddressProp();
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" /> xmlns:xsi=" /> xsi:schemaLocation=" /> />beans.xsd">
<bean id="javaCollection"
class="com.tutorialspoint.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 >
<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>
</props>
</property>
</bean>
</beans>
Kết quả :
Jul 22, 2013 6:01:45 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@18ee9d6: startup date
[Mon Jul 22 18:01:45 ICT 2013]; root of context hierarchy
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}
456&78 &9+'%)*&
:&&"
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;
}