为什么spring没有检测到我的@组件?
这是一个棘手的问题-至少对我来说。组件扫描似乎不起作用 这是web.xml文件为什么spring没有检测到我的@组件?,spring,spring-mvc,Spring,Spring Mvc,这是一个棘手的问题-至少对我来说。组件扫描似乎不起作用 这是web.xml文件 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Geomajas GWT face example applicatio
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Geomajas GWT face example application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:org/geomajas/spring/geomajasContext.xml
classpath:org/geomajas/plugin/rasterizing/DefaultRasterizedPipelines.xml
WEB-INF/applicationContext.xml
<!-- WEB-INF/applicationContext2.xml -->
<!-- WEB-INF/layer*.xml -->
<!-- WEB-INF/map*.xml -->
WEB-INF/layerOsm.xml
WEB-INF/mapOsm.xml
<!-- WEB-INF/applicationContext2.xml -->
</param-value>
</context-param>
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>org.geomajas.servlet.CacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.geomajas.servlet.PrepareScanningContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>GeomajasServiceServlet</servlet-name>
<servlet-class>org.geomajas.gwt.server.GeomajasServiceImpl</servlet-class>
</servlet>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/geomajasWebContext.xml</param-value>
<description>Spring Web-MVC specific (additional) context files.</description>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<!-- SpringGwt remote service servlet -->
<servlet>
<servlet-name>springGwtRemoteServiceServlet</servlet-name>
<servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class>
<init-param>
<param-name>contexConfigLocation</param-name>
<param-value>WEB-INF/applicationContext2.xml</param-value>
<description>j</description>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>GeomajasServiceServlet</servlet-name>
<url-pattern>/showcase/geomajasService</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/d/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springGwtRemoteServiceServlet</servlet-name>
<url-pattern>/showcase/springGwtServices/test</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Geomajas GWT人脸示例应用程序
上下文配置位置
类路径:org/geomajas/spring/geomajasContext.xml
类路径:org/geomajas/plugin/rasterising/DefaultRasterizedPipelines.xml
WEB-INF/applicationContext.xml
WEB-INF/layerOsm.xml
WEB-INF/mapOsm.xml
缓存过滤器
org.geomajas.servlet.CacheFilter
缓存过滤器
*
org.geomajas.servlet.PrepareScanningContextListener
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
GeomajasServiceServlet
org.geomajas.gwt.server.GeomajasServiceImpl
调度员
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
classpath*:META-INF/geomajasWebContext.xml
SpringWebMVC特定(附加)上下文文件。
3.
springGwtRemoteServiceServlet
org.spring4gwt.server.SpringGwtRemoteServiceServlet
contexConfigLocation
WEB-INF/applicationContext2.xml
J
GeomajasServiceServlet
/showcase/geomajasService
调度员
/d/*
springGwtRemoteServiceServlet
/showcase/SpringWtService/test
index.html
我的服务:
@Service("test")
public class ProjServiceImpl extends RemoteServiceServlet implements ProjService {
private static final Log LOG = LogFactory.getLog(ProjServiceImpl.class);
@Autowired
PoiCategDAO poiCategDAO;
public String greetServer(String input) throws IllegalArgumentException {
// Verify that the input is valid.
if (!FieldVerifier.isValidName(input)) {
// If the input is not valid, throw an IllegalArgumentException back to
// the client.
throw new IllegalArgumentException(
"Name must be at least 4 characters long");
}
//RequestContextUtils.getWebApplicationContext(request);
// String serverInfo = getServletContext().getServerInfo();
// String userAgent = getThreadLocalRequest().getHeader("User-Agent");
return "Hello, " + input + "!<br><br>I am running .<br><br>It looks like you are using:<br>";
}
@Override
//@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void testdao(Integer id) throws IllegalArgumentException {
// TODO Auto-generated method stub
PoiCateg X=poiCategDAO.findById(id);
PoiCateg z=poiCategDAO.findById(id);
}
}
@服务(“测试”)
公共类ProjServiceImpl扩展RemoteServiceServlet实现ProjService{
私有静态最终日志日志=LogFactory.getLog(ProjServiceImpl.class);
@自动连线
波卡特道波卡特道;
公共字符串greetServer(字符串输入)抛出IllegalArgumentException{
//验证输入是否有效。
如果(!FieldVerifier.isValidName(输入)){
//如果输入无效,则将IllegalArgumentException抛出回
//客户。
抛出新的IllegalArgumentException(
“名称长度必须至少为4个字符”);
}
//RequestContextIls.getWebApplicationContext(请求);
//字符串serverInfo=getServletContext().getServerInfo();
//字符串userAgent=getThreadLocalRequest().getHeader(“用户代理”);
返回“Hello,”+input+“!
我正在运行。
看起来您正在使用:
”;
}
@凌驾
//@事务性(传播=propagation.REQUIRED,rollboor=Exception.class)
public void testdao(整数id)引发IllegalArgumentException{
//TODO自动生成的方法存根
PoiCateg X=poiCategDAO.findById(id);
PoiCateg z=poiCategDAO.findById(id);
}
}
和applicationContext2.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="ne.projl.*" />
<!-- <context:component-scan base-package="FULLY QUALIFIED" /> -->
<bean class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="MyPUnit" />
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
请注意,如果我将context:component扫描放在applicationContext.xml中(不是applicationContext 2.xml,则检测到我的服务bean)。如果我还需要提供其他信息,请告诉我。可能有很多原因,一个常见的原因是您在错误的包下创建了
@组件
类
作为上下文扫描配置集,该类必须位于该包下
<context:component-scan base-package="ne.projl.*" />
如果我没记错的话,组件扫描中的基本包只需要包名。然后将包括所有基础子包 因此,应选择正确的设置
<context:component-scan base-package="ne.projl" />
发布异常(如果有)将为这里的每个人提供更多关于该问题的见解。在我看来,这是一个可见性问题。
如果您正在引用通过contextConfigLocation加载的某个组件中的applicationContext2中扫描的组件,则该组件显然不可见。
.
. 我无法对这个问题发表评论,所以我把我的观察结果作为答案发表了出来。。对不起,我不确定,但是你的web.xml不是这样说的吗
<!-- WEB-INF/applicationContext2.xml -->
那不是评论掉了吗?
您正在applicationContext.xml文件中导入applicationContext2的资源吗?ne.projl是我的基本软件包。刚刚检查了拼写错误,我所有的类都在ne.projl.someothername下。我不明白你最后一句话的意思:你引用的源代码是applicationContext2.xml,它包含组件扫描。您说过applicationContext.xml包含组件扫描,但不包含applicationContext2.xml。更糟糕的是,web.xml引用的是application.xml。你能先简化并澄清你的问题吗?你用什么方法来确定你的bean是否被扫描?等等,你的最后一句话是否真的是:如果我在applicationContext.xml(不是applicationContext2.xml)中放入context:component scan,我的服务bean被检测到了??如果是这样的话,这是显而易见的:applicationContext2.xml没有在Spring中使用。您尚未将其包含在web.xml的contextConfigLocation1中。您的应用程序上下文xml真的加载了吗?2.你的服务真的在ne.projl下吗?3.它应该不带“*”,但不能不带“*”。4.您是否尝试过只使用一个主类、一个服务类创建一个最简单的项目,并在主方法中加载应用程序上下文?在这里引用大量不相关的信息之前,确保你真的知道如何使用它