创建名称为'ModelAccess'的bean时出错注入自动绑定的依赖项失败;无法自动连线栏位

时间:2018-08-09 07:06:46

标签: java spring hibernate spring-data-jpa

这是我项目的结构:

scr / main / java / com / company / configuration

scr / main / java / com / company / controller

scr / main / java / com / company / myProject

scr / main / java / com / company / exception

在配置中,我有3个类:ProjectInitializer,ProjectConfiguration和ProjectContextListener。

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.company"})
 @EnableJpaRepositories("com.company")
public class ProjectConfiguration {

}

================================================ =======================

public class ProjectInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { ProjectConfiguration.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.addListener(new ProjectContextListener());

    }
}

================================================ =====================

公共类ProjectContextListener实现ServletContextListener {

public void contextInitialized(ServletContextEvent servletContextEvent) {
    Security.addProvider(new BouncyCastleProvider());
    Security.addProvider(new FlexiCoreProvider());
    Security.addProvider(new FlexiECProvider());
    javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }
    });
}

public void contextDestroyed(ServletContextEvent servletContextEvent) {
    System.out.println("Shutting down!");
}

}

================================================ ======================

我想在myproject包中建立到oracle数据库的连接。它包含一些类,问题出在ModelAccess类上:

@Service
public class ModelAccess {
    @Autowired
    ProjectRepository jpaRepository;
    protected JpaRepository<ProjectEnt, Long> getJpaRepository() {
        return jpaRepository;
    }
}

================================================ ===========================

@Transactional
public interface ProjectRepository extends
        JpaRepository<ProfileEnt, Long>,IProjectRepository{


}

================================================ ===========================

@Repository
public interface IProjectRepository extends IGenericRepository<ProfileEnt> {
}

================================================ ==================

这是我在tomcat中部署项目时的堆栈跟踪:

创建名称为'ModelAccess'的bean时出错: 自动连接的依赖项注入失败;  嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段: com.company.myproject.impl.ProjectRepository com.company.myProject.impl.ModelAccess.jpaRepository;  嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException: 找不到依赖项类型为[com.company.myproject.impl.ProjectRepository]的合格Bean: 期望至少有1个bean符合此依赖项的自动装配条件。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(required = true)}

================================================ ============================ 这是我的pom文件的一部分:

 <properties>
        <springframework.version>4.2.0.RELEASE</springframework.version>
    </properties>
    <dependencies>
 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

  <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.11.11.RELEASE</version>
            <!--<version>2.0.8.RELEASE</version>-->
        </dependency>


        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.4</version>
        </dependency>

 <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.8.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.8.Final</version>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0</version>
        </dependency>

1 个答案:

答案 0 :(得分:0)

IProjectRepository不是JpaRepository的问题。而且,看起来您不需要它。要解决此问题,只需删除IProjectRepository并改用ProjectRepository