使用maven构建jar,包含spring服务列表并将其添加到war中
我将Spring与Maven和Tomcat一起使用。我有一个SpringMVC应用程序,它有一个公共前端WAR和一个共享服务JAR。我的JAR包含由spring annotation使用maven构建jar,包含spring服务列表并将其添加到war中,spring,maven,service,jar,Spring,Maven,Service,Jar,我将Spring与Maven和Tomcat一起使用。我有一个SpringMVC应用程序,它有一个公共前端WAR和一个共享服务JAR。我的JAR包含由spring annotation@service注释的服务列表,我在这个JAR中配置我的应用程序上下文,如下所示 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta
@service
注释的服务列表,我在这个JAR中配置我的应用程序上下文,如下所示
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:sws="http://www.springframework.org/schema/web-services"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services
http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.bergit.jpa" />
<!-- Import file which containt parameter need it to configure the DB and hibernate -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="META-INF/config/jdbc.properties" />
<!-- Database connection settings -->
<bean id="dataSource-seconde"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<!-- Configuration for Hibernate/JPA -->
<bean id="entityManagerFactory-seconde"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- <property name="dataSource" ref="dataSource-seconde" /> -->
<property name="persistenceUnitManager" ref="pum"/>
<property name="persistenceUnitName" value="testspring-jpa-seconde" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="${jdbc.dialect}" />
</bean>
</property>
</bean>
<bean id="pum"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>META-INF/persistence.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager-seconde"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory-seconde" />
<property name="dataSource" ref="dataSource-seconde" />
</bean>
</beans>
我用Junit测试了我的Jar,它还可以。
现在我将这个jar添加到我的war中(添加到war的pom文件中)。
我为我的第一个视图编写了控制器类,在这里我注入了我的第一个服务,我想在这个controllerUserJARService
中使用它,如下所示:
@Service("userJARService")
public class UserJARServiceImpl implements UserJARService {...}
@Controller
@RequestMapping(AccessClassToWebService.CONTROLLER_BASE_PATH)
public class AccessClassToWebService {
@Autowired
UserJARService userJARService;
}
现在,我在war的pom中添加了这个jar依赖项。但我得到了错误的咆哮
原因:org.springframework.beans.factory.BeanCreationException:无法自动连线字段:com.bergit.jpa.service.UserJARService….您在哪里部署了war Tomcat、Wildfly、Weblogic?我将war部署到Tomcat您的
基本包
是错误的。它是一个包,不是模式或类引用com.bergit.jpa.service
和com.bergit.jpa.repository
或仅com.bergit.jpa
就足够了。另外,您应该编程到接口UserJARService
,而不是实际的实现。@M.Deinum:是的,我这样更改了,但我有相同的错误没有匹配类型[com.bergit.jpa.service.UserJARService]的bean您更改了什么?