Spring必须指定至少一个基本包的异常 [英] Spring At least one base package must be specified exception

查看:265
本文介绍了Spring必须指定至少一个基本包的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译项目时,它会给出此错误并停止编译

When I'm compiling my project it gives this error and stop compiling


org.springframework.beans.factory.BeanDefinitionStoreException:
从ServletContext资源
中解析XML文档的意外异常[/WEB-INF/application-context.xml];嵌套异常为
java.lang.IllegalArgumentException:必须至少指定一个基本包$ p $ b

为什么

推荐答案

根据Spring Doc

As per Spring Doc

Spring提供了自动检测定型类并向ApplicationContext注册相应BeanDefinition的功能。为了自动检测这些类并注册相应的bean,需要在XML中包含以下元素,其中 basePackage将是两个类的公共父包(或者可以指定一个逗号分隔的列表,其中包括每个类的父包)

Spring provides the capability of automatically detecting 'stereotyped' classes and registering corresponding BeanDefinitions with the ApplicationContext. To autodetect these classes and register the corresponding beans requires the inclusion of the following element in XML where 'basePackage' would be a common parent package for the two classes (or alternatively a comma-separated list could be specified that included the parent package of each class).

因此,您的应用程序上下文应如下所示:

Hence your application-context should look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

     <context:component-scan base-package="org.example"/>

</beans>

使包可以通过 component-scan ,您应该根据需要使用相应的注释对Java类进行注释。像 @Controllers @Service 等。
查看完整详细信息此处

to enable your package to get scanned via component-scan you should annotate your java class with respective annotation as per the requirement. Like @Controllers or @Service etc. See the full details here.

这篇关于Spring必须指定至少一个基本包的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆