2012-02-02 90 views
3

我有一個只能在標準JVM上運行的應用程序 - 沒有像JBoss或Tomcat這樣的應用程序服務器。是否有可能通過使用彈簧(我需要spring-jdbc)通過applicationContext.xml配置正常運行?我沒有找到任何教程。是否可以在沒有應用程序服務器的情況下運行Spring?

SOLUTION

第一部分是從下方加入到這一點pom.xml的答案和第二部分是(在我的情況)。

<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <archive> 
        <manifest> 
         <mainClass>package.MainClass</mainClass> 
        </manifest> 
       </archive> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

回答

4

是 - 所有你需要的是JVM來啓動Java主類,使得使用Spring FW的。

這裏是使用它來初始化春天JDBC的context.xml和代碼的例子:

<!-- DATASOURCE used for object stores --> 
<bean id="dataSourceForObjects" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close"> 
    <property name="driverClassName" value="com.${job.repository.db.type}.jdbc.Driver" /> 
    <property name="url" value="jdbc:${job.repository.db.type}://${db.host}:${job.repository.db.port}/${db.schema}" /> 
    <property name="username" value="${db.user}" /> <!-- your user id. e.g. root--> 
    <property name="password" value="${db.password}" /> <!-- your password--> 
    <property name="maxIdle" value="10" /> 
    <property name="maxActive" value="100" /> 
    <property name="maxWait" value="10000" /> 
    <property name="validationQuery" value="select 1" /> 
    <property name="testOnBorrow" value="false" /> 
    <property name="testWhileIdle" value="true" /> 
    <property name="timeBetweenEvictionRunsMillis" value="1200000" /> 
    <property name="minEvictableIdleTimeMillis" value="1800000" /> 
    <property name="numTestsPerEvictionRun" value="5" /> 
    <property name="defaultAutoCommit" value="true" /> 
</bean> 
    <bean id="objectStoreDao" class="com.pursway.core.dao.objectStore.ObjectStoreJdbcImpl"> 
    <property name="dataSource" ref="dataSourceForObjects"/> 
</bean> 

這裏是java代碼示例:

... 
ApplicationContext context = new FileSystemXmlApplicationContext(ExecutionController.BASIC_CONFIG_FILES); 
jobExplorer = (JobExplorer)context.getBean("jobExplorer"); 
workFlowDao = (WorkFlowDao)context.getBean("workFlowDao"); 
.... 

祝你好運!

6

是的,它可以只是一個依賴注入框架。

下面是一個例子與Swing:Spring & Swing

所有你需要的是使用ClassPathXmlApplicationContext的或類似的加載方面的主要方法或somwhere。

ApplicationContext context = 
    new ClassPathXmlApplicationContext("applicationContext.xml") 
相關問題