Reputation: 93
I have two java projects as epService and epEntity (factory classes for db access). And there is another Spring project as epWeb which contains controllers and this is a Rest API. Now, I want to autowire a class which is inside epEntity to the spring project epWeb. I have successfully autowired classes within that epWeb project but I was unable to autowire a class from another project Anyone have a suggestion for doing that, please let me know. If this is an irrelevant question for stackoverflow, please delete this.
The class where I autowire public class Mapper {
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Autowired
private AppointmentFactory af;
@Autowired
private AppointmentController ac;
}
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.mobios.ep.web.controllers" />
<!-- <context:component-scan base-package="com.mobios.ep.services" />
<context:component-scan base-package="com.ombios.ep.entity.factory" /> -->
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1048576"/>
</bean>
</beans>
Upvotes: 5
Views: 11729
Reputation: 2910
You can use @ComponentScan
annotation in your main application epWeb
in order to register in Spring context classes which live in epEntity
project as Beans (Controllers, Services etc).
@ComponentScan({"com.example.service", "com.example.controller"})
Also, make sure that Service implementations are annotated with @Service
in epEntity
project.
Upvotes: 10
Reputation: 656
My sugestion is to create an spring-config.xml file in both projects for own spring beans configurations (you used the name mvc-dispatcher-servlet.xml) and on web.xml to map both files for contextConfigLocation, so both projects will be in same spring context.
Also you can use a specified pattern for spring config files and to use a regex for contextConfigLocation.
web.xml fragment:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:view-spring-config.xml
classpath:service-spring-config.xml
</param-value>
</context-param>
or
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:*-spring-config.xml
</param-value>
</context-param>
Upvotes: 0