2012-02-01 31 views
0

我有兩個使用相同數據庫對象的不同應用程序(站點+一些cron實用程序)。我將它們移到單獨的庫中,並且在應用程序中只保留了hibernate.configuration文件。現在Hibernate + Spring MVC:外部對象庫

,問題是,我有一個使用JavaMailSender一些電子郵件通知服務,它在spring配置文件中定義的屬性:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
     <property name="host" value="mail.xxx.com" /> 
     <property name="username" value="xxx" /> 
     <property name="password" value="xxx" /> 
     <property name="javaMailProperties"> 
      <props> 
       <prop key="mail.smtp.auth">true</prop> 
       <prop key="mail.smtp.starttls.enable">true</prop> 
       <prop key="mail.smtp.ssl.trust">*</prop> 
      </props> 
     </property> 
    </bean> 
    <bean id="notificationsService" class="de.crm.NotificationsService"> 
     <property name="sender" ref="mailSender" /> 
     <property name="properties" ref="notificationsServiceProperties" /> 
    </bean> 
    <bean id="notificationsServiceProperties" class="de.crm.objects.properties.NotificationsServiceProperties"> 
     <property name="name" value="xxx" /> 
     <property name="email" value="xxx" /> 
    </bean> 

因此,這裏的主要問題,它不能看到de.crm .objects.properties.NotificationsServiceProperties類,因爲它是在外部庫中定義的,並且該項目在導出時失敗。

有什麼辦法可以在外部庫中保留屬性類並修復它嗎? 謝謝

UPD#1:是否可以使用來自外部庫的對象Spring的@Autowired註解?

+1

將外部jar添加到構建路徑中還是將外部jar放置在classpath中,從中可以讀取它? – Rocky 2012-02-01 07:46:34

+0

我已將外部jar作爲外部庫添加到Buildpath設置中 – nKognito 2012-02-01 07:51:29

回答

1
So the main problem here that it can't see de.crm.objects.properties.NotificationsServiceProperties class because 

它是在外部庫中定義和項目在導出失敗。

如果未包含在項目的類路徑中,Spring將無法識別類/庫。所以你需要確保你的類庫是內部的還是外部的,都包含在classpath中。

UPD#1: Is it possible to use objects from external library Spring's with @Autowired annotation? 

僅@Autowired提供那些存在於彈簧上下文對象。如果一個類在spring上下文之外,即使它包含在classpath中,它也不會被@Autowired識別。

編輯

首先,類(例如,foo.Bar)添加到類路徑。

其次,在Spring配置XML添加一個新的bean定義:

<bean class="foo.Bar"></bean> 

現在,您可以通過訪問該對象@Autowired:

public class SomeOtherClassInSpringContext { 
    @Autowired 
    Bar myBar; 
} 

附:如果你還沒有這樣做,你還需要申請<context:annotation-config/><context:component-scan base-package="path.to.your.classes"/>以告訴spring你已經爲你的一些類配置了註釋。請參閱Spring docs

+0

那麼有什麼方法可以將外部對象添加到Spring上下文嗎? – nKognito 2012-02-01 07:46:34

+0

請檢查我的更新答案。 – craftsman 2012-02-01 08:04:17

+0

啊,好吧,我已經做到了:)我解決了這個問題。您只需將外部jar添加到WEB-INF/lib /目錄中即可。而且您不必在項目設置中將此jar作爲外部庫添加。謝謝 – nKognito 2012-02-01 08:15:13

0

如果我理解正確,應該將包含外部類的JAR放在應用程序的類路徑中。然後你的類將被Spring的上下文看到。 這就是我們要做的,例如配置數據源。 以一般的方式,春秋可以處理你的classpath每個類...

+0

你是什麼意思?添加這個jar作爲外部庫?我已經做到了,沒有幫助 – nKognito 2012-02-01 07:31:54