Use the factory to implement IOC

Source: Internet
Author: User
Content of the IOC. properties file (configuration file ):

# Interface = implementation class
Com. Kay. Struts. Dao. userdao=Com. Kay. Struts. Dao. impl. userdaoimpl

Factory type:PackageCom. Kay. Struts. Common;

ImportJava. Io. inputstream;
ImportJava. util. hashmap;
ImportJava. util. iterator;
ImportJava. util. Map;
ImportJava. util. properties;

Public   Final   Class Iocutil
{
// File Path
Private   Static   Final String filepath = " COM/Kay/struts/common/IOC. Properties " ;
Private   Static Map < Class, Object > Map;

/**
* Initialization Method
* @ Return
*/
Private   Static Map Init (){
Inputstream Input = Iocutil. Class . Getclassloader (). getresourceasstream (filepath );

Properties pro =   New Properties ();
Try
{
Pro. Load (input );
Input. Close ();
// Obtain the key set
Iterator ITER = Pro. keyset (). iterator ();
String key =   "" ;
String Value =   "" ;
Map =   New Hashmap < Class, Object > ();
While (ITER. hasnext ()){
Key = (String) ITER. Next ();
Value = Pro. getproperty (key );
Class CIA = Class. forname (key );
Object OBJ = Class. forname (value). newinstance ();
Map. Put (linoleic, OBJ );
}
Return Map;
}
Catch (Exception E)
{
E. printstacktrace ();
Return   Null ;
}
}

Public   Static Object Lookup (class CLs ){
If (Map =   Null ){
Map =   New Hashmap < Class, Object > ();
Init ();
}
Object OBJ = Map. Get (CLS );
If (OBJ ! =   Null ){
Return OBJ;
}
Return   Null ;
}

}

Userdao interface:PackageCom. Kay. Struts. Dao;

ImportCom. Kay. Struts. Common. Exception. daoexception;
ImportCom. Kay. Struts. VO. user;

Public InterfaceUserdao
{
PublicUser findbynameandpass (string name, string pass)ThrowsDaoexception;
}

Userdao interface implementation class:PackageCom. Kay. Struts. Dao. impl;

ImportCom. Kay. Struts. Common. Exception. daoexception;
ImportCom. Kay. Struts. Dao. userdao;
ImportCom. Kay. Struts. VO. user;

Public ClassUserdaoimpl
ImplementsUserdao
{

Public User findbynameandpass (string name, string pass)
Throws Daoexception
{
User user =   Null ;
If (Name. Equals ( " Tom " ) && Pass. Equals ( " 123 " )){
User =   New User ();
User. setid ( 1 );
User. setname (name );
User. setpass (PASS );
}
Return User;
}

}

Call method:
Userdao Dao = (userdao) iocutil. Lookup (userdao. Class );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.