Integration of spring and eclipse RCP

Source: Internet
Author: User
Two methods are provided based on the location where the spring configuration file is stored in the project.

1. The configuration file is stored in the source code root directory.

Suppose I have a project named admin_console. I put the spring configuration file myspring. XML in the source code under the src directory, as shown in
Admin_console
-- SRC
-- CN // package name
-- Com
-- Chengang
--- ...... // Source code class
-- Myspring. xml // spring configuration file, which is located at the same level as the CN directory under the src directory
-- Bin
-- Lib
-- Icons
-- Properties

In this way, we can load myspring. xml classloader oldloader = thread. currentthread (). getcontextclassloader () in the RCP program ();
Try {
Thread. currentthread (). setcontextclassloader (this. getclass (). getclassloader ());
CTX = new classpathxmlapplicationcontext ("/myspring. xml ");
} Finally {
Thread. currentthread (). setcontextclassloader (oldloader );
}

2. The configuration file is stored in a subdirectory of the Project root directory.

The project root directory and source code root directory are different concepts. For example, in the project structure, SRC is the source code root directory, and admin_console is the project root directory, so properties is a sub-directory under the project root directory.

If myspring. XML is put into the properties directory, the above read code is useless. The read method is as follows: classloader oldloader = thread. currentthread (). getcontextclassloader ();
Try {
Thread. currentthread (). setcontextclassloader (this. getclass (). getclassloader ());
CTX = new filesystemxmlapplicationcontext (projectutil. tofullpath ("properties/myspring. xml "));
} Finally {
Thread. currentthread (). setcontextclassloader (oldloader );
}
Projectutil. tofullpath is a method written by myself. It mainly gets the absolute path of myspring. xml. The Code is as follows: Import java. Io. ioexception;
Import java. Io. inputstream;
Import java.net. malformedurlexception;
Import java.net. url; import org. Eclipse. Core. runtime. filelocator;
Import org. Eclipse. Core. runtime. path;
Import org. Eclipse. UI. plugin. Unzip actuiplugin; import com. wxxr. Management. admin. Console. adminconsoleplugin ;/**
* It is used for plug-in and non-plug-in projects and provides common methods and interfaces between the two.
* @ Author chengang 2006-3-30
*/
Public class projectutil {Private Static abstractuiplugin plugin = adminconsoleplugin. getdefault (); Private projectutil (){}/**
* Determine whether the current running status is in Plug-in Mode
* @ Return true = plug-in running
*/
Private Static Boolean isplugin (){
Return plugin! = NULL;
} Public static URL geturl (string path ){
If (isplugin () // if it is a plug-in
Return filelocator. Find (plugin. getbundle (), new path (PATH), null );
Else
Try {
Return new URL ("file:" + path );
} Catch (malformedurlexception e ){
Throw new runtimeexception (path + "is error", e );
}
} Public static inputstream getinputstream (string path ){
URL url = geturl (PATH );
Try {
Return URL. openstream ();
} Catch (ioexception e ){
Throw new runtimeexception (E );
}
} Public static string tofullpath (string path ){
If (isplugin ()){
Try {
Return filelocator. tofileurl (projectutil. geturl (PATH). getpath ();
} Catch (ioexception e ){
Throw new runtimeexception (path + "tofullpath is fault", e );
}
} Else {
Return path;
}
}}

Iii. Summary

Which of the above two methods is better? It should be the second type. In general, the source code compilation file will be packaged into a jar package (in fact, it is okay not to pack it into a jar package. I have tried to deploy the class file loose many times before, if you want to modify the class, you can only deploy and overwrite the class after the modification, which looks quite convenient. However, this method is not the best practice and is not recommended for official release. It may cause problems in other dependent classes .). If the first method is used after the project is packaged, myspring. XML will be packaged into the jar file, which is not conducive to the dynamic modification of myspring in the future. If the second method is used, there is no such disadvantage.

Most of the time, there is no problem in running the RCP program in the eclipse development environment. However, after the project is exported, an error cannot be found in the configuration file (not just spring) in an independent environment. The solution is basically the same.

Related Article

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.