You are on page 1of 11

By gi mnh s phn tch v d ny.

Trc ht chng ta hy xem hm main trong class App Code: (Click Here to Select All)
public static void main(String[] args){

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

XeMay xehaiky = (XeMay) context.getBean("xebonky"); XeMay xebonky = (XeMay) context.getBean("xehaiky");

System.out.println(xehaiky.XeNoMay()); System.out.println(xebonky.XeNoMay()); }

ng ra nu bnh thng th chng ta thng vit nh sau : Code: (Click Here to Select All)
public static void main2(String[] args){

DongCo dongcohaiky = new DongCoHaiKy(); DongCo dongcobonky = new DongCoBonKy();

XeMay xehaiky = new XeMay(dongcohaiky); XeMay xebonky = new XeMay(dongcobonky);

System.out.println(xehaiky.XeNoMay()); System.out.println(xebonky.XeNoMay());

So snh ta c th thy y trong hm main chng ta khng cn dng ton t new khi to cc i tng xehaiky v xebonky. Chng ta cng khng phi to cc i tng ng c truyn vo cho cc i tng ca class XeMay. Vy th - ai to ra cc i tng xehaiky, xebonky? - V chng ly cc i tng DongCo t u ? - V file beans.xml c ngha v vai tr nh th no? Trc khi tr li nhng cu hi ny mnh mun nhc li phn gii thiu v IoC v DI mnh ni l dng IoC thay i trch nhim to ra i tng dongco ca class XeMay cho mt thnh phn no bn ngoi. Trong bi helloworld ny thnh phn bn ngoi to ra cc i tng xemayhaiky, xemaybonky v cc i tng DongCo l cc thnh phn ca spring framework m chng ta s dng( l IoC container). Trong Spring cc i tng chnh xy dng ng dng th c qun l bi Spring IoC container. V IoC container gi cc i tng l cc bean. Mt bean ch n gin l mt i tng c khi to v qun l bi Spring IoC container. Ta bit l gia cc bean th c s ph thuc ln nhau vy lm sao Spring Framework bit c chng ph thuc ln nhau nh th no ? Lm sao bit c cn to ra i tng no, truyn cc loi DongCo vo cc i tng ca class XeMay ra sao , Spring s dng hai cch( hai loi metadata) ngi lp trnh c th cu hnh cc bean (to ra nh th no, ph thuc vo ai , ) l dng Annotation( Java 5 mi h tr Annotation) v file XML . Trong v d ny mnh s dng file beans.xml cu hnh cc bean. Trc khi phn tch file beans.xml nu bn no cha bit v xml th c th tham kho ti y : http://vi.wikipedia.org/wiki/XML. File beans.xml : Code: (Click Here to Select All)
<?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.xsd">

<bean id="dongcohaiky" class="example.com.DongCoHaiKy"></bean> <bean id="dongcobonky" class="example.com.DongCoBonKy"></bean>

<bean id="xehaiky" class="example.com.XeMay"> <constructor-arg name="dongco" ref="dongcohaiky"></constructor-arg> </bean>

<bean id="xebonky" class="example.com.XeMay"> <constructor-arg ref="dongcobonky"></constructor-arg> </bean> </beans>

Trc ht l phn t beans y l phn t root ca file xml. N s cha tt c cc phn t bean ca chng ta v ngoi ra n cn c mt s thuc tnh nhng trong khun kh bi ny mnh ngh rng chng ta khng phi quan tm n chng. By gi chng ta s xt ti phn t bean. Code: (Click Here to Select All)
<bean id="dongcohaiky" class="example.com.DongCoHaiKy"></bean>

Vi phn t bean ny chng ta s cu hnh cho IoC container to ra mt bean ca class "example.com.DongCoHaiKy". Ta thy phn t ny c hai thuc tnh id v class. Thuc tnh id c dng nh danh. Mt bean c th c nhiu id, nhng cc id khng c trng nhau.Cn thuc tnh class l dng ch bean c to thnh t class no. Nhng vi phn t bean ny n li c phn t con : Code: (Click Here to Select All)
<bean id="xehaiky" class="example.com.XeMay"> <constructor-arg name="dongco" ref="dongcohaiky"></constructor-arg> </bean>

Trong v d mnh a ra th constructor ca class XeMay c mt i s truyn vo. V phn t con <constructor-arg> dng cu hnh cho IoC container bit l phi truyn i s no vo cho constructer ca class XeMay. Ta thy phn t <constructor-arg > c thuc tnh ref = dongcohaiky cu hnh cho IoC container bit c l truyn bean no vo cho contructer ca class XeMay. Gi tr ca thuc tnh ref l id ca bean m chng ta mun truyn vo. By gi chng ta hy xem li hm main, dng u tin l : Code: (Click Here to Select All)
ApplicationContext context =

new ClassPathXmlApplicationContext("beans.xml");

Dng ny dng khi to IoC container vi phn cu hnh l file beans.xml. V i tng context l i din cho container , n c trch nhim khi to, cu hnh cc i tng v qun l s phuc thuc gia cc i tng trong container.

Bn c th hnh dung IoC container theo hnh trn. Code: (Click Here to Select All)
XeMay xehaiky = (XeMay) context.getBean("xebonky");

y ta dng phng thc getBean() ca ApplicationContext interface truy cp bean c id =xebonky m chng ta cu hnh trong file beans. Xml cu hnh cc bean th bn s phi s dng phn t <bean></bean> trong file xml. Vi phn t ny th cc bn s lm vic vi cc vn sau : Namimg beans. Instantiating beans. Bean scopes. Dependency injection Autowiring collaboratiors. Lazy-initialized beans. Initialization callbacks. Destruction callbacks. Chng ta s tng bc lm vic vi cc vn ny. Naming beans: beans container c th qun l c cc bean th chng phi c c nh danh (ging nh con ngi phi c tn vy). Spring cho php chng ta c th t cho bean mt nh danh hoc c th l nhiu nh danh. Chng ta cu hnh trn file xml th chng ta c th dng hai thuc tnh l id

v name t nh danh cho bean. Nu chng ta s dng id th n phi l duy nht trong trong bean container. Khi bn mun nhiu nh danh cho mt bean bn c th dng thuc tnh name v cc nh danh vi name c th cch nhau bng khong trng, du phy hoc du chm phy. Nu bn khng t nh danh cho bean th bean container s t sinh ra cho bean mt nh danh. Vi hai cch nh danh trn th cc thuc tnh thuc phn t <bean>. Bn c th thm nh danh cho bean bng cch dng alias. Bn c th dng phn t <alias> thm nh danh cho bean. d hiu hn by gi mnh s ly mt v d v nh danh cho bean:

File: context.xml Code: (Click Here to Select All)


<?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/springbeans.xsd">

<bean id="name1" name="name2,name3,name4" class="java.lang.String"/> <alias name="name1" alias="namex1"/> <alias name="name1" alias="namex2"/>

</beans>

File: Main.java Code: (Click Here to Select All)


import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource;

public class Main { public static void main(String[] a) { BeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml"));

String s1 = (String) factory.getBean("name1"); String s2 = (String) factory.getBean("name2"); String s3 = (String) factory.getBean("name3"); String s4 = (String) factory.getBean("name4"); String s5 = (String) factory.getBean("namex1"); String s6 = (String) factory.getBean("namex2");

System.out.println((s1 == s2)); System.out.println((s2 == s3)); System.out.println((s3 == s4)); System.out.println((s4 == s5)); System.out.println((s5 == s6)); } }

Qua v d va ri cc bn c th hiu c c php t nh danh cho bean. Khi container to mt bean th lm sao n bit c l cn to bean ca class no. Vi XML th chng ta s dng thuc tnh class trong phn t <bean>. Nh chng ta bit trong lp trnh java c hai cch chng ta to ra i tng. Cch th nht l dng ton t new ( thng qua constructor). Cch th hai l dng hm static ca mt class no (thng l mt Factory). V d nh : XeMay xemay = new XeMay(); // qua constructor XeMay xemay = HonDa.createXeMay(); //qua static method l createXeMay().

V spring cng h tr chng ta hai cch to ra bean bng constructor v static method. Khi to bng constructor: Code: (Click Here to Select All)
<bean id="exampleBean" class="examples.ExampleBean"/> <bean name="anotherExample" class="examples.ExampleBeanTwo"/>

Nh v d th thuc tnh class c gn bng tn ca class m n mun to bean. Khi to bng static method : Gi s ta c class sau : Code: (Click Here to Select All)
public class ClientService { private static ClientService clientService = new ClientService(); private ClientService() {} public static ClientService createInstance() { return clientService; } }

V khi to i tng cho class th ta phi cu hnh nh sau : Code: (Click Here to Select All)
<bean id="clientService" class="examples.ClientService" factorymethod="createInstance"/>

Khi thuc tnh class s c gn bng tn ca Class c phng thc static nh v d trn. Nhng khi m khi to mt i tng (bean) t mt i tng (bean) khc th sao. Spring lm nh v d sau : Code: (Click Here to Select All)
public class DefaultServiceLocator { private static ClientService clientService = new ClientServiceImpl(); private DefaultServiceLocator() {} public ClientService createClientServiceInstance() {

return clientService; } }

Code: (Click Here to Select All)


<!-- the factory bean, which contains a method called createInstance() --> <bean id="serviceLocator" class="examples.DefaultServiceLocator"> <!-- inject any dependencies required by this locator bean --> </bean> <!-- the bean to be created via the factory bean --> <bean id="clientService" factory-bean="serviceLocator" factory-method = "createClientServiceInstance"/>

bean scope. Khi bn cu hnh mt bean, v bean container s to i tng(bean) cho bn.Bn s mun bean container to cho bn my i tng nh vy, hay l ch cn to mt i tng thi thi , V cu hnh vic ny bn phi s dng thuc tnh scope. Scope s c nhng gi tr sau :

Singleton : Vi mi bean container ch tao duy nht mt i tng Prototype: to mt i tng mi khi c yu cu Request : to mt i tng cho mi ln c HTTP request. Session : to mt i tng cho mi HTTP session. globalSession : to mt i tng cho mt global HTTP session.

Trong phm vi bi ny mnh ch mun gii thiu n Singleton v Prototype. V y l hai gi tr thng c dng nht. Cn ba thuc tnh cn li ch s dng trong ng dng web. V gi tr mc nh ca scope l singleton. Scope = singleton ngha l trong mt bean container ch to ra mt i tng duy nht ca bean . d hiu chng ta ly v d nh sau : File CustomerService.java Code: (Click Here to Select All)
package com.example;

public class CustomerService {

String message;

public String getMessage() { return message; }

public void setMessage(String message) { this.message = message; } }

File cu hnh beans.xml: Code: (Click Here to Select All)


<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="customerService" class="com.example.CustomerService" scope="singleton" />

</beans>

File App.java Code: (Click Here to Select All)


package com.example;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.example.CustomerService;

public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"beans.xml"});

CustomerService custA = (CustomerService)context.getBean("customerService"); custA.setMessage("Message by custA"); System.out.println("Message : " + custA.getMessage());

CustomerService custB = (CustomerService)context.getBean("customerService"); System.out.println("Message : " + custB.getMessage()); } }

Khi chng ta chy ng dng ny th kt qu thu c s l : Code: (Click Here to Select All)
Message : Message by custA Message : Message by custA

Nh bn c th thy y khi chng ta yu cu bean container hai ln nhng ch c mt i tng c to ra.

Cn vi scope=prototype th mi ln chng ta yu cu bean container s to cho chng ta mt i tng mi. V vi v d trn nu chng ta thay i scope = prototype nh sau : File beans.xml Code: (Click Here to Select All)
<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="customerService" class="com.mkyong.customer.services.CustomerService" scope="prototype"/>

</beans>

Khi kt qu thu c s l : Code: (Click Here to Select All)


Message : Message by custA Message : null

Nh vy vi cch cu hnh th hai th mi ln chng ta gi phng thc getBean() th bean container phi to mt i tng mi.

You might also like