spring没有从maven依赖中读取服务存储库

时间:2017-07-11 11:23:25

标签: java spring maven

我正在使用两个maven项目,一个是Web应用程序,另一个是maven jar。 jar的依赖关系被添加到Web应用程序中,但在访问Web应用程序中的jar时

这是网络应用的代码

@Controller
public class IndexController {
    @Autowired
        private ServiceIntf intf;

        @RequestMapping("/")
        public String displayMessage(Model model) {
            String message = intf.message();
            System.out.println("\n\n\n\n" + message + "\n\n\n");
            model.addAttribute("message", message != null ? message : "null Value");
            return "index";
        }
    }

和服务存储库,来自其他项目

@Service
public class ServiceClass implements ServiceIntf {

    Logger logger = Logger.getGlobal();

    public String message() {
        return "hello";
    }

} 

jsp文件

<body>
    <h1>Configured Context</h1>
    ABC
    <h1>${message}</h1>
    DEF
</body>

用于Web应用程序的pom文件

    <dependencies>
            <dependency>
<!-- project dependency-->
                <groupId>test-core</groupId>
                <artifactId>test-core</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.1.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.1.0.RELEASE</version>
            </dependency>
        </dependencies>

结果我正在打印已配置的上下文 ABC XYZ 但不会显示消息。请看看并帮助我。如有需要,请告知我们。

编辑:这是我得到的错误日志形式eclipse错误日志

The POM for test-core:test-core:jar:0.0.1-SNAPSHOT is missing, no dependency information available

编辑2:重新安装.m2后,我收到以下错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.api.service.intf; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.api.service.ServiceIntf] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

网络配置文件

public class DispatcherServletConfiguration extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

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

}

@EnableWebMvc
@Configuration
@ComponentScan("com.api")
public class ServletConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

注意:请原谅我的英语不好

1 个答案:

答案 0 :(得分:1)

BeanCreationException的根本原因是名称为intf的bean不存在,因此请进行以下任何一项更改。确保在spring xml文件中添加了<context:component-scan base-package="your.base.package" />

<强> Fix1:

更改

@Service
public class ServiceClass implements ServiceIntf {

@Service("intf")
public class ServiceClass implements ServiceIntf {

OR

<强> FIX2

更改

@Autowired
private ServiceIntf intf;

@Autowired
private ServiceIntf serviceIntf;