I have two different applications (site + some cron utility) that are using the same database objects. I moved them into separate library, and in the applications I left only hibernate.configuration files.
Now, the problem is that I have some email notification service that uses JavaMailSender and it have properties defined in spring configuration file:
<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>
So the main problem here that it can’t see de.crm.objects.properties.NotificationsServiceProperties class because it is defined in external library and the project fails at export.
Is there any way to leave the properties class in external library and fix it?
Thank you
UPD#1: Is it possible to use objects from external library Spring’s with @Autowired annotation?
Spring cannot recognize a class/library if it is not included in the project’s classpath. So you need to ensure that your library either internal or external, is included in classpath.
@Autowired only supplies those objects which are present in spring context. If a class is outside spring context, even if it is included in classpath, it will not be recognized by @Autowired.
EDIT
First, add the class (for example,
foo.Bar) to your classpath.Second, add a new bean definition in your spring configuration xml:
Now, you can access this object using @Autowired:
P.S. If you haven’t already done so, you also need to apply
<context:annotation-config/>and<context:component-scan base-package="path.to.your.classes"/>in order to tell spring that you have configured some of your classes with annotations.For more information, refer to Spring docs.