1. Trang chủ
  2. » Công Nghệ Thông Tin

Spring tutorials

202 928 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 202
Dung lượng 2,91 MB

Nội dung

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="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- 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="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- 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="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- 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 conguraon See below Spring bean configuration, Spring will DI above “JsonOutputGenerator” into this “OutputHelper” class, via constructor “public OutputHelper(IOutputGenerator outputGenerator)“. <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-2.5.xsd"> <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="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-2.5.xsd"> <bean id="CustomerBean" class="com.mkyong.common.Customer"> <constructor-arg type="java.lang.String"> <value>mkyong</value> </constructor-arg> [...]... } beans.xml Kết quả: Jul 22, 2013 5:16:18 PM org.springframework.context.support.AbstractApplicationContext... 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/springbeans.xsd"> Kết quả : Jul 22, 2013 5:24:32 PM org.springframework.context.support.AbstractApplicationContext... System.out.println("C("+C.getX()+","+C.getY()+")"); } } context.xml ... triangle=(Triangle)context.getBean("triangleBean"); triangle.draw(); } } context.xml ... triangle=(Triangle)context.getBean("triangleBean"); triangle.draw(); } } context.xml . name; } } Class Main: package org.quangthao; import org.springframework.cglib.proxy.Factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public. 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- 2.5.xsd"> <bean. 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- 2.5.xsd"> <bean

Ngày đăng: 06/05/2014, 13:55

Xem thêm

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w