简体   繁体   English

无法通过@ImportResource加载Spring根上下文

[英]Spring root context is not loaded via @ImportResource

I have a small issue, i'm trying to combine java-based config with xml-based-config, i have a small issue, is that the configuration of my root-context is xml based, and i'm trying to scan it using @ImportResource annotation but spring during start can't read the configuration file which leads to beanCreationException, could not autowire fields. 我有一个小问题,我正在尝试将基于java的配置与xml-based-config结合起来,我有一个小问题,就是我的根上下文的配置是基于xml的,我正在尝试对其进行扫描使用@ImportResource批注,但是spring在启动过程中无法读取导致beanCreationException的配置文件,无法自动装配字段。

here are my files 这是我的文件

RootConfig.java RootConfig.java

    @Configuration
    @ImportResource({ "classpath*: applicationContext.xml" })
    public class RootContextConfiguration {
        public RootContextConfiguration() {
            super();
        }
    }

WebAppInitilizer.java WebAppInitilizer.java

    public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

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

org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ma.xxx.net.repos.UserRepository ma.xxx.net.service.impl.UserServiceImpl.userRepository; org.springframework.beans.factory.BeanCreationException:无法自动连线字段:私有ma.xxx.net.repos.UserRepository ma.xxx.net.service.impl.UserServiceImpl.userRepository;

I presume it couldn't inject bean because it couldn't find it. 我认为它无法注入bean,因为找不到它。

This is spring-data-managed Repository. 这是Spring数据管理的存储库。

    @Repository
    public interface UserRepository extends JpaRepository<User, Long>{

    }

This is what you asked for 这就是你要的

    @Service("userService")
    public class UserServiceImpl implements IUserService {

        @Autowired
        private UserRepository userRepository;

        @Override
        public User getUser() {
            return userRepository.findOne(1L);
        }
    }

Hope you guys can help me out with this. 希望你们能帮助我。

您需要添加到您的配置@EnableJpaRepositories并指向该存储库所在的软件包

@EnableJpaRepositories(basePackages = "your.repository.package")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM