2012-02-01 65 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