【问题标题】:Autowired Bean not of correct type when building with Codehaus & EnversHibernateToolTask使用 Codehaus 和 EnversHibernateToolTask​​ 构建时自动装配的 Bean 类型不正确
【发布时间】:2012-11-15 12:25:28
【问题描述】:

仅当我使用我的 QA Profile 构建时才会出现此问题,例如mvn clean install -Pqa

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'reportingService' must be of type [com.testProjects.project.backoffice.service.BackofficeReportingService], but was actually of type [$Proxy78]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:360)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:442)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:549)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
    ... 54 more
Tests run: 563, Failures: 1, Errors: 0, Skipped: 562, Time elapsed: 19.656 sec <<< FAILURE!

我的 POM 的相关部分:

<profile>
    <id>qa</id>
    <properties>
        <tomcat.url>http://etc:8081/manager/text</tomcat.url>
        <war.name>project-backoffice</war.name>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.1.1</version>
                <configuration>
                    <deployer>
                        <deployables>
                            <deployable>
                                <properties>
                                    <context>${war.name}</context>
                                </properties>
                            </deployable>
                        </deployables>
                    </deployer>
                    <wait>true</wait>
                    <container>
                        <containerId>tomcat7x</containerId>
                        <type>remote</type>
                    </container>
                    <configuration>
                        <type>runtime</type>
                        <properties>
                            <cargo.tomcat.manager.url>${tomcat.url}</cargo.tomcat.manager.url>
                            <cargo.remote.username>admin</cargo.remote.username>
                            <cargo.remote.password>admin</cargo.remote.password>
                        </properties>
                    </configuration>
                </configuration>
                <executions>
                    <execution>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>redeploy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

以及构建部分:

<artifactId>maven-antrun-plugin </artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <phase>install</phase>
            <configuration>
                <tasks>
                    <property name="compile_classpath" refid="maven.compile.classpath" />
                    <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.EnversHibernateToolTask" classpath="${compile_classpath}" />
                    <hibernatetool destdir="${project.build.directory}">
                        <annotationconfiguration configurationfile="${project.build.outputDirectory}/backoffice.hibernate.cfg.xml" propertyfile="${project.build.directory}/test-classes/hibernatetools.properties" />
                        <hbm2ddl create="true" drop="true" export="false" format="true" outputfilename="project-schema.ddl" />
                    </hibernatetool>
                    <hibernatetool destdir="${project.build.directory}">
                        <annotationconfiguration configurationfile="${project.build.outputDirectory}/backoffice.hibernate.docs.cfg.xml" propertyfile="${project.build.directory}/test-classes/hibernatetools.properties" />
                        <hbm2ddl create="true" drop="true" export="false" format="true" outputfilename="document-schema.ddl" />
                    </hibernatetool>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>

有问题的控制器:

@Controller
@RequestMapping("/reporting/")
public class ReportingController {

    private final static Logger logger = LoggerFactory.getLogger(ReportingController.class);

    @Resource
    private BackofficeReportingService reportingService;

相关服务:

@Transactional(propagation=Propagation.NEVER)
@Service
public class BackofficeReportingService {

尝试在上下文中添加定义,还尝试设置 CGLIB 代理,但没有效果。

<bean id="reportingService" class="com.project.backoffice.service.BackofficeReportingService"/>

非常感谢任何帮助,这使我的构建“不稳定”,我很想知道发生了什么。

【问题讨论】:

  • BackofficeReportingService 是否实现了任何接口?如果没有,则创建一个并仅通过该接口引用此类。将 CGLIB 添加到您的 CLASSPATH 并启用 CGLIB 代理应该也有帮助。
  • 不是,但我会尝试并报告。谢谢。

标签: spring hibernate maven dependency-injection


【解决方案1】:

这要归功于 Tomasz Nurkiewicz。 为 reportingService 创建一个接口解决了这个问题。

我不清楚为什么会出现这个问题,为什么几个月内没有变化,或者为什么大多数其他服务在没有接口的情况下注入良好,但我非常感谢 Tomasz!用绿色球体代替黄色球体的 Huzzah!

【讨论】:

    猜你喜欢
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 2020-01-31
    相关资源
    最近更新 更多