论坛首页 Java企业应用论坛

Spring app 使用包的简化和注意的问题

浏览 2590 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-10-04   最后修改:2008-12-13

      众所周知spring框架是一个非常优秀的轻量级框架工具,我们借助它可以简单的将软件各个部分割裂开以实现较低的耦合度。
那么我们在有些时候强外界发布这些软件时面临着一个选择--是否将spring的相关包一起发布,如果全部一齐发布则可能使原本非常小巧的程式变得非常庞大;
如果不发布则可能使客户端面临程式工作环境配置的复杂程度加大,在这里主要是spring框架的下载、配置和使用。
      基于以上情况我们选择一个折衷的办法:将spring工作必须的基本类文件和相关配置文件与我们的程式一起发布出去。在这里的问题就主要是包的选择(类相互的依赖关系)和框架类的一些配置文件的选择使用。
      由于我的经历有限,在此我就将我写的一个第三方eclipse插件管理器所面临的一些问题以及获得的经验和大家分享一下。在这里我将用一个简单的spring例子作为替代说明即可。

#System environment
Ubuntu 
7.10  Linux
Eclipse Platform Version:  3.3.0  + MyEclipse  6.0

      其中spring框架的加载和配置是通过MyEclipse的插件(MyEclipse-->Project Capabilities-->Add Spring ...-->Spring 2.0 Core ...)实现的。
      以下是一个简单的spring使用的程式代码:

<? 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.0.xsd"
>
    
    
    
< bean  id ="HelloWorld"  class ="springapp.hello.HelloWorld" >
        
< property  name ="message" >
            
< value > world </ value >
        
</ property >
    
</ bean >


</ beans >

 

package  springapp.hello;

public   interface  Hello  ... {
    
public  String sayHello();
}

 

package  springapp.hello;

public   class  HelloWorld  implements  Hello ... {
    
private  String message;
    
    
public  HelloWorld() ... {
        message
= null ;
    }

    
    
public  String sayHello() ... {
        
return   " Hello  " + message + " ! " ;
    }


    
public  String getMessage()  ... {
        
return  message;
    }


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


}

 

package  springapp.main;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.FileSystemXmlApplicationContext;

import  springapp.hello.Hello;

public   class  Main  ... {

    
/** */ /**
     * 
@param  args
     
*/

    
public   static   void  main(String[] args)  ... {
        ApplicationContext ctx 
=   new  FileSystemXmlApplicationContext(
                
" /src/applicationContext.xml " );
        Hello h 
=  (Hello)ctx.getBean( " HelloWorld " );
        System.out.println(h.sayHello());
    }


}


最后运行Main类就会显示一些信息:

2008 - 02 - 14   14 : 50 : 55 , 954  INFO
 
[ org.springframework.context.support.FileSystemXmlApplicationContext ]
 - Refreshing org.springframework.context.support.FileSystemXmlApplicationContext
@f3d6a5: display name
 
[ org.springframework.context.support.FileSystemXmlApplicationContext@f3d6a5 ]
;  startup date [Thu Feb 14 14:50:55 CST 2008]; root of context hierarchy
2008 - 02 - 14   14 : 50 : 56 , 013  INFO
 
[ org.springframework.beans.factory.xml.XmlBeanDefinitionReader ]
- Loading XML bean definitions from file
 
[ /home/wpc/workspace/Java/MyStudy/SimplifySpringCoreJar/src/applicationContext.xml ]
2008 - 02 - 14   14 : 50 : 56 , 197  INFO
[ org.springframework.context.support.FileSystemXmlApplicationContext ]
- Bean factory for application context
[ org.springframework.context.support.FileSystemXmlApplicationContext@f3d6a5 ]
: org.springframework.beans.factory.support.DefaultListableBeanFactory@f7f540
2008 - 02 - 14   14 : 50 : 56 , 210  INFO
[ org.springframework.beans.factory.support.DefaultListableBeanFactory ]
 - Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@f7f540
: defining beans 
[ HelloWorld ] ;  root of factory hierarchy
Hello world!

由于有个log配置文件的问题可能有些程式运行会有警告信息,这个不要紧,不再讨论范畴。
我的解决方案是:

#   For  JBoss: Avoid to setup Log4J outside  $ JBOSS_HOME / server / default / deploy / log4j . xml!
#   For  all other servers: Comment out the Log4J listener in web . xml to activate Log4J .
log4j
. rootLogger = INFO ,  stdout ,  logfile

log4j
. appender . stdout = org . apache . log4j . ConsoleAppender
log4j
. appender . stdout . layout = org . apache . log4j . PatternLayout
log4j
. appender . stdout . layout . ConversionPattern = %d %p [%c] - %m%n

log4j
. appender . logfile = org . apache . log4j . RollingFileAppender

#  The log file's location
log4j
. appender . logfile . File = springframe_log . log
log4j
. appender . logfile . MaxFileSize = 512KB

#  Keep three  backup   files .
log4j
. appender . logfile . MaxBackupIndex = 3

#  Pattern to output: data  priority  [category] -message
log4j
. appender . logfile . layout = org . apache . log4j . PatternLayout
log4j
. appender . logfile . layout . ConversionPattern = %d %p [%c] - %m%n

      文件命名为log4j.properties然后打包jar并且导入即可。
      但是如果将这个工程导出,一般是不携带spring框架类文件的,这时在外部运行Main类就可能出现问题,一般提示是spring相关的类文件无法找到。我当时就是通过这样一些错误信息一步步补全我的spring基础类文件的,应该有相关的工具但是我没有找到。
      在这里我就将我的结果给大家:

/**/**
.:
META-INF
org

./META-INF:
spring.schemas

./org:
apache
springframework

./org/apache:
commons

./org/apache/commons:
logging

./org/apache/commons/logging:
impl
Log.class
LogConfigurationException.class
LogFactory$1.class
LogFactory$2.class
LogFactory$3.class
LogFactory$4.class
LogFactory$5.class
LogFactory.class
LogSource.class

./org/apache/commons/logging/impl:
AvalonLogger.class
Jdk13LumberjackLogger.class
Jdk14Logger.class
Log4JLogger.class
LogFactoryImpl.class
LogKitLogger.class
NoOpLog.class
ServletContextCleaner.class
SimpleLog$1.class
SimpleLog.class
WeakHashtable$1.class
WeakHashtable$2.class
WeakHashtable.class
WeakHashtable$Entry.class
WeakHashtable$Referenced.class
WeakHashtable$WeakKey.class

./org/springframework:
beans
context
core
util

./org/springframework/beans:
AbstractPropertyAccessor.class
annotation
BeanInstantiationException.class
BeanMetadataElement.class
BeansException.class
BeanUtils.class
BeanWrapper.class
BeanWrapperImpl$1.class
BeanWrapperImpl.class
BeanWrapperImpl$PropertyTokenHolder.class
CachedIntrospectionResults.class
ConfigurablePropertyAccessor.class
DirectFieldAccessor$1.class
DirectFieldAccessor.class
factory
FatalBeanException.class
InvalidPropertyException.class
Mergeable.class
MethodInvocationException.class
MutablePropertyValues.class
NotReadablePropertyException.class
NotWritablePropertyException.class
NullValueInNestedPathException.class
PropertyAccessException.class
PropertyAccessor.class
PropertyAccessorUtils.class
PropertyBatchUpdateException.class
PropertyEditorRegistrar.class
PropertyEditorRegistry.class
PropertyEditorRegistrySupport$1.class
PropertyEditorRegistrySupport.class
PropertyEditorRegistrySupport$CustomEditorHolder.class
propertyeditors
PropertyMatches.class
PropertyValue.class
PropertyValues.class
PropertyValuesEditor.class
SimpleTypeConverter.class
support
TypeConverter.class
TypeConverterDelegate.class
TypeMismatchException.class

./org/springframework/beans/annotation:
AnnotationBeanUtils.class

./org/springframework/beans/factory:
access
annotation
BeanClassLoaderAware.class
BeanCreationException.class
BeanCreationNotAllowedException.class
BeanCurrentlyInCreationException.class
BeanDefinitionStoreException.class
BeanFactoryAware.class
BeanFactory.class
BeanFactoryUtils.class
BeanInitializationException.class
BeanIsAbstractException.class
BeanIsNotAFactoryException.class
BeanNameAware.class
BeanNotOfRequiredTypeException.class
CannotLoadBeanClassException.class
config
DisposableBean.class
FactoryBean.class
FactoryBeanNotInitializedException.class
generic
HierarchicalBeanFactory.class
InitializingBean.class
ListableBeanFactory.class
NamedBean.class
NoSuchBeanDefinitionException.class
ObjectFactory.class
parsing
SmartFactoryBean.class
support
UnsatisfiedDependencyException.class
wiring
xml

./org/springframework/beans/factory/access:
BeanFactoryLocator.class
BeanFactoryReference.class
BootstrapException.class
SingletonBeanFactoryLocator$1.class
SingletonBeanFactoryLocator$BeanFactoryGroup.class
SingletonBeanFactoryLocator.class

./org/springframework/beans/factory/annotation:
AnnotationBeanWiringInfoResolver.class
Autowire.class
Configurable.class
RequiredAnnotationBeanPostProcessor.class
Required.class

./org/springframework/beans/factory/config:
AbstractFactoryBean$1.class
AbstractFactoryBean.class
AutowireCapableBeanFactory.class
BeanDefinition.class
BeanDefinitionHolder.class
BeanDefinitionVisitor.class
BeanFactoryPostProcessor.class
BeanPostProcessor.class
BeanReference.class
BeanReferenceFactoryBean.class
CommonsLogFactoryBean.class
ConfigurableBeanFactory.class
ConfigurableListableBeanFactory.class
ConstructorArgumentValues.class
ConstructorArgumentValues$ValueHolder.class
CustomEditorConfigurer.class
CustomScopeConfigurer.class
DestructionAwareBeanPostProcessor.class
FieldRetrievingFactoryBean.class
InstantiationAwareBeanPostProcessorAdapter.class
InstantiationAwareBeanPostProcessor.class
ListFactoryBean.class
MapFactoryBean.class
MethodInvokingFactoryBean.class
ObjectFactoryCreatingFactoryBean$1.class
ObjectFactoryCreatingFactoryBean.class
PreferencesPlaceholderConfigurer.class
PropertiesFactoryBean.class
PropertyOverrideConfigurer.class
PropertyPathFactoryBean.class
PropertyPlaceholderConfigurer.class
PropertyPlaceholderConfigurer$PlaceholderResolvingBeanDefinitionVisitor.class
PropertyResourceConfigurer.class
ResourceFactoryBean.class
RuntimeBeanNameReference.class
RuntimeBeanReference.class
Scope.class
ServiceLocatorFactoryBean$1.class
ServiceLocatorFactoryBean.class
ServiceLocatorFactoryBean$ServiceLocatorInvocationHandler.class
SetFactoryBean.class
SingletonBeanRegistry.class
SmartInstantiationAwareBeanPostProcessor.class
TypedStringValue.class

./org/springframework/beans/factory/generic:
GenericBeanFactoryAccessor.class

./org/springframework/beans/factory/parsing:
AbstractComponentDefinition.class
AliasDefinition.class
BeanComponentDefinition.class
BeanDefinitionParsingException.class
BeanEntry.class
ComponentDefinition.class
CompositeComponentDefinition.class
ConstructorArgumentEntry.class
DefaultsDefinition.class
EmptyReaderEventListener.class
FailFastProblemReporter.class
ImportDefinition.class
Location.class
NullSourceExtractor.class
ParseState.class
ParseState$Entry.class
PassThroughSourceExtractor.class
Problem.class
ProblemReporter.class
PropertyEntry.class
ReaderContext.class
ReaderEventListener.class
SourceExtractor.class

./org/springframework/beans/factory/support:
AbstractAutowireCapableBeanFactory.class
AbstractAutowireCapableBeanFactory$ConstructorResolverAdapter.class
AbstractBeanDefinition.class
AbstractBeanDefinitionReader.class
AbstractBeanFactory$1.class
AbstractBeanFactory$2.class
AbstractBeanFactory.class
AutowireUtils$1.class
AutowireUtils.class
BeanDefinitionBuilder.class
BeanDefinitionReader.class
BeanDefinitionReaderUtils.class
BeanDefinitionRegistry.class
BeanDefinitionValidationException.class
BeanDefinitionValueResolver.class
BeanNameGenerator.class
CglibSubclassingInstantiationStrategy$1.class
CglibSubclassingInstantiationStrategy$CglibSubclassCreator$CallbackFilterImpl.class
CglibSubclassingInstantiationStrategy$CglibSubclassCreator$CglibIdentitySupport.class
CglibSubclassingInstantiationStrategy$CglibSubclassCreator.class
CglibSubclassingInstantiationStrategy$CglibSubclassCreator$LookupOverrideMethodInterceptor.class
CglibSubclassingInstantiationStrategy$CglibSubclassCreator$ReplaceOverrideMethodInterceptor.class
CglibSubclassingInstantiationStrategy.class
ChildBeanDefinition.class
ConstructorResolver$ArgumentsHolder.class
ConstructorResolver.class
DefaultBeanNameGenerator.class
DefaultListableBeanFactory.class
DefaultSingletonBeanRegistry.class
DisposableBeanAdapter.class
InstantiationStrategy.class
LookupOverride.class
ManagedList.class
ManagedMap.class
ManagedProperties.class
ManagedSet.class
MethodOverride.class
MethodOverrides.class
MethodReplacer.class
PropertiesBeanDefinitionReader.class
ReplaceOverride.class
RootBeanDefinition.class
SimpleInstantiationStrategy.class
StaticListableBeanFactory.class

./org/springframework/beans/factory/wiring:
BeanConfigurerSupport.class
BeanWiringInfo.class
BeanWiringInfoResolver.class
ClassNameBeanWiringInfoResolver.class

./org/springframework/beans/factory/xml:
AbstractBeanDefinitionParser.class
AbstractSimpleBeanDefinitionParser.class
AbstractSingleBeanDefinitionParser.class
BeanDefinitionDecorator.class
BeanDefinitionDocumentReader.class
BeanDefinitionParser.class
BeanDefinitionParserDelegate.class
BeansDtdResolver.class
DefaultBeanDefinitionDocumentReader.class
DefaultDocumentLoader.class
DefaultNamespaceHandlerResolver.class
DelegatingEntityResolver.class
DocumentDefaultsDefinition.class
DocumentLoader.class
NamespaceHandler.class
NamespaceHandlerResolver.class
NamespaceHandlerSupport.class
ParserContext.class
PluggableSchemaResolver.class
ResourceEntityResolver.class
SimplePropertyNamespaceHandler.class
spring-beans-2.0.dtd
spring-beans-2.0.xsd
spring-beans.dtd
spring-tool-2.0.xsd
spring-util-2.0.xsd
UtilNamespaceHandler$1.class
UtilNamespaceHandler.class
UtilNamespaceHandler$ConstantBeanDefinitionParser.class
UtilNamespaceHandler$ListBeanDefinitionParser.class
UtilNamespaceHandler$MapBeanDefinitionParser.class
UtilNamespaceHandler$PropertiesBeanDefinitionParser.class
UtilNamespaceHandler$PropertyPathBeanDefinitionParser.class
UtilNamespaceHandler$SetBeanDefinitionParser.class
XmlBeanDefinitionParser.class
XmlBeanDefinitionReader.class
XmlBeanDefinitionStoreException.class
XmlBeanFactory.class
XmlReaderContext.class

./org/springframework/beans/propertyeditors:
ByteArrayPropertyEditor.class
CharacterEditor.class
CharArrayPropertyEditor.class
ClassArrayEditor.class
ClassEditor.class
CustomBooleanEditor.class
CustomCollectionEditor.class
CustomDateEditor.class
CustomMapEditor.class
CustomNumberEditor.class
FileEditor.class
InputStreamEditor.class
LocaleEditor.class
PatternEditor.class
PropertiesEditor.class
ResourceBundleEditor.class
StringArrayPropertyEditor.class
StringTrimmerEditor.class
URIEditor.class
URLEditor.class

./org/springframework/beans/support:
ArgumentConvertingMethodInvoker.class
MutableSortDefinition.class
PagedListHolder.class
PagedListSourceProvider.class
PropertyComparator.class
RefreshablePagedListHolder.class
ResourceEditorRegistrar.class
SortDefinition.class

./org/springframework/context:
access
ApplicationContextAware.class
ApplicationContext.class
ApplicationContextException.class
ApplicationEvent.class
ApplicationEventPublisherAware.class
ApplicationEventPublisher.class
ApplicationListener.class
ConfigurableApplicationContext.class
event
HierarchicalMessageSource.class
i18n
Lifecycle.class
MessageSourceAware.class
MessageSource.class
MessageSourceResolvable.class
NoSuchMessageException.class
ResourceLoaderAware.class
support

./org/springframework/context/access:
ContextBeanFactoryReference.class
ContextJndiBeanFactoryLocator.class
ContextSingletonBeanFactoryLocator.class
DefaultLocatorFactory.class

./org/springframework/context/event:
AbstractApplicationEventMulticaster.class
ApplicationEventMulticaster.class
ConsoleListener.class
ContextClosedEvent.class
ContextRefreshedEvent.class
EventPublicationInterceptor.class
SimpleApplicationEventMulticaster$1.class
SimpleApplicationEventMulticaster.class
SourceFilteringListener.class

./org/springframework/context/i18n:
LocaleContext.class
LocaleContextHolder.class
SimpleLocaleContext.class

./org/springframework/context/support:
AbstractApplicationContext$1.class
AbstractApplicationContext$BeanPostProcessorChecker.class
AbstractApplicationContext.class
AbstractMessageSource.class
AbstractRefreshableApplicationContext.class
AbstractXmlApplicationContext.class
ApplicationContextAwareProcessor.class
ApplicationObjectSupport.class
ClassPathXmlApplicationContext.class
DefaultMessageSourceResolvable.class
DelegatingMessageSource.class
FileSystemXmlApplicationContext.class
GenericApplicationContext.class
MessageSourceAccessor.class
MessageSourceResourceBundle.class
ReloadableResourceBundleMessageSource.class
ReloadableResourceBundleMessageSource$PropertiesHolder.class
ResourceBundleMessageSource.class
ResourceMapFactoryBean.class
StaticApplicationContext.class
StaticMessageSource.class

./org/springframework/core:
annotation
AttributeAccessor.class
AttributeAccessorSupport.class
BridgeMethodResolver.class
CollectionFactory$BackportConcurrentCollectionFactory.class
CollectionFactory.class
CollectionFactory$CommonsCollectionFactory.class
CollectionFactory$JdkCollectionFactory.class
ConstantException.class
Constants.class
ControlFlow.class
ControlFlowFactory.class
ControlFlowFactory$Jdk13ControlFlow.class
ControlFlowFactory$Jdk14ControlFlow.class
Conventions.class
enums
ErrorCoded.class
GenericCollectionTypeResolver.class
io
JdkVersion.class
LocalVariableTableParameterNameDiscoverer.class
LocalVariableTableParameterNameDiscoverer$FindConstructorParameterNamesClassVisitor.class
LocalVariableTableParameterNameDiscoverer$FindMethodParameterNamesClassVisitor.class
LocalVariableTableParameterNameDiscoverer$LocalVariableTableVisitor.class
LocalVariableTableParameterNameDiscoverer$ParameterNameDiscoveringVisitor.class
MethodParameter.class
NestedCheckedException.class
NestedExceptionUtils.class
NestedIOException.class
NestedRuntimeException.class
OrderComparator.class
Ordered.class
OverridingClassLoader.class
ParameterNameDiscoverer.class
PrioritizedParameterNameDiscoverer.class
ReflectiveVisitorHelper$1.class
ReflectiveVisitorHelper.class
ReflectiveVisitorHelper$ClassVisitMethods$1.class
ReflectiveVisitorHelper$ClassVisitMethods.class
SpringVersion.class
style
task

./org/springframework/core/annotation:
AnnotationAwareOrderComparator.class
AnnotationUtils.class
Order.class

./org/springframework/core/enums:
AbstractCachingLabeledEnumResolver$1.class
AbstractCachingLabeledEnumResolver.class
AbstractGenericLabeledEnum.class
AbstractLabeledEnum.class
LabeledEnum$1.class
LabeledEnum$2.class
LabeledEnum.class
LabeledEnumResolver.class
LetterCodedLabeledEnum.class
ShortCodedLabeledEnum.class
StaticLabeledEnum.class
StaticLabeledEnumResolver.class
StringCodedLabeledEnum.class

./org/springframework/core/io:
AbstractResource.class
ByteArrayResource.class
ClassPathResource.class
DefaultResourceLoader.class
DescriptiveResource.class
FileSystemResource.class
FileSystemResourceLoader.class
InputStreamResource.class
InputStreamSource.class
Resource.class
ResourceEditor.class
ResourceLoader.class
support
UrlResource.class

./org/springframework/core/io/support:
EncodedResource.class
LocalizedResourceHelper.class
PathMatchingResourcePatternResolver.class
PropertiesLoaderSupport.class
PropertiesLoaderUtils.class
ResourceArrayPropertyEditor.class
ResourcePatternResolver.class
ResourcePatternUtils.class

./org/springframework/core/style:
DefaultToStringStyler.class
DefaultValueStyler.class
StylerUtils.class
ToStringCreator.class
ToStringStyler.class
ValueStyler.class

./org/springframework/core/task:
AsyncTaskExecutor.class
SimpleAsyncTaskExecutor$1.class
SimpleAsyncTaskExecutor.class
SimpleAsyncTaskExecutor$ConcurrencyThrottleAdapter.class
SimpleAsyncTaskExecutor$ConcurrencyThrottlingRunnable.class
SyncTaskExecutor.class
TaskExecutor.class
TaskRejectedException.class
TaskTimeoutException.class

./org/springframework/util:
AntPathMatcher.class
Assert.class
AutoPopulatingList.class
AutoPopulatingList$ElementFactory.class
AutoPopulatingList$ElementInstantiationException.class
AutoPopulatingList$ReflectiveElementFactory.class
CachingMapDecorator.class
ClassLoaderUtils.class
ClassUtils.class
CollectionUtils.class
comparator
ConcurrencyThrottleSupport.class
CustomizableThreadCreator.class
DefaultPropertiesPersister.class
FileCopyUtils.class
Log4jConfigurer.class
MethodInvoker.class
NumberUtils.class
ObjectUtils.class
PathMatcher.class
PatternMatchUtils.class
PropertiesPersister.class
ReflectionUtils$1.class
ReflectionUtils$2.class
ReflectionUtils$3.class
ReflectionUtils.class
ReflectionUtils$FieldCallback.class
ReflectionUtils$FieldFilter.class
ReflectionUtils$MethodCallback.class
ReflectionUtils$MethodFilter.class
ResourceUtils.class
ResponseTimeMonitor.class
ResponseTimeMonitorImpl.class
StopWatch$1.class
StopWatch.class
StopWatch$TaskInfo.class
StringUtils.class
SystemPropertyUtils.class
TypeUtils.class
WeakReferenceMonitor$1.class
WeakReferenceMonitor.class
WeakReferenceMonitor$MonitoringProcess.class
WeakReferenceMonitor$ReleaseListener.class
xml

./org/springframework/util/comparator:
BooleanComparator.class
ComparableComparator.class
CompoundComparator.class
InvertibleComparator.class
NullSafeComparator.class

./org/springframework/util/xml:
DomUtils.class
SimpleSaxErrorHandler.class
SimpleTransformErrorListener.class
XmlValidationModeDetector.class

*/

 */可能可以再次简化,但是我没有继续进行,如果有兴趣可以继

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics