2

这个很棘手 - 至少对我来说。组件扫描似乎不起作用

这是 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 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>

我的服务:

@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);
    }

}

和 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-scan 放入 applicationContext.xml (不是 applicatinContext2.xml 我的服务 bean 被检测到。)。如果有其他信息我应该提供告诉。

4

4 回答 4

2

如果我没记错的话,组件扫描中的基本包只需要一个包名。然后将包含所有底层子包。

因此正确的设置应该是

<context:component-scan base-package="ne.projl" />
于 2013-07-11T02:58:37.543 回答
1

可能有很多原因,一个常见的原因是你@Component在错误的包下创建了你的类。

由于您的上下文扫描配置集,该类必须在此包下

<context:component-scan base-package="ne.projl.*" />
于 2013-07-11T00:14:14.713 回答
0

发布异常(如果有)将为这里的每个人提供对问题的更多见解。在我看来,这是一个能见度问题。如果您引用的是在您的 applicationContext2 中扫描的组件,在您通过 contextConfigLocation 加载的组件之一中,显然它将不可见。. 检查此链接以获取有关应用程序上下文可见性的更多信息 。我无法对问题添加评论..所以我已经发布了我的观察结果作为答案.. 抱歉

于 2013-07-11T08:47:54.760 回答
0

我不确定,但您的 web.xml 没有这样说

<!--            WEB-INF/applicationContext2.xml -->

不是注释掉了吗?

您是否在 applicationContext.xml 文件中导入 applicationContext2 资源?

于 2013-07-11T11:30:08.900 回答