【问题标题】:Undefined beans with Spring MVC使用 Spring MVC 的未定义 bean
【发布时间】:2013-06-13 14:25:19
【问题描述】:

我无法理解 Spring MVC。 我有以下 web.xml :

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/root-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>RESTServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>RESTServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

在 root-context.xml 文件中,我声明了各种 bean,有些带有 xml,有些带有注释:

<?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">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <context:component-scan base-package="com.dynamease.**" />
    <context:annotation-config />

    <!-- Configures External Property Resolution -->
    <import resource="properties.xml" />

    <!-- Configures Shared Data Access Resources -->
    <import resource="data.xml" />

    <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
        <property name="url" value="ldap://192.168.1.10:10389" />
        <property name="base" value="dc=dynamease,dc=net" />
        <property name="userDn" value="uid=admin,ou=system" />
        <property name="password" value="secret" />
    </bean>
    <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
        <constructor-arg ref="contextSource" />
    </bean>
</beans>

在 servlet-context.xml 中,我定义了与 web 逻辑相关的 bean:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <interceptors>
        <beans:bean
            class="com.dynamease.entity.springsocialentities.UserInterceptor">
            <beans:constructor-arg ref="usersConnectionRepository" />
        </beans:bean>
    </interceptors>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <!-- Imports user-defined @Controller beans that process client requests -->
    <bean class="com.dynamease.web.rest.HomeController">
        <constructor-arg ref="facebook" />
    </bean>

    <!-- Allows users to sign-in with their provider accounts.  -->
    <bean class="org.springframework.social.connect.web.ProviderSignInController">
        <constructor-arg ref="connectionFactoryLocator" />
        <constructor-arg ref="usersConnectionRepository" />     
        <constructor-arg>
            <bean class="com.dynamease.entity.springsocialentities.SimpleSignInAdapter" />
        </constructor-arg>
    </bean>

    <mvc:view-controller path="/signin" />

    <mvc:view-controller path="/signout" />
</beans:beans>

问题是 root-context.xml 中定义的 bean 没有初始化(我得到空指针异常试图访问它们)。 例如,在 HomeController bean 中:

@Autowired
    private DynDirectoryServiceImpl dynDirectoryService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Model model) {
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        System.out.println(this.getClass().toString());
        System.out.println(dynDirectoryService.toString()); 
}

最后一行失败。

这里是 DynDirectoryServiceImpl 代码:

    @Component
    public class DynDirectoryServiceImpl implements DynDirectoryServiceInterface {

        @Autowired
        private LdapTemplate ldapTemplate;

        @Autowired
        private OdmManager odmManager;

        @Override
        public boolean verify(DynContact dynContact) {
            // TODO Auto-generated method stub
            return false;
        }
}

【问题讨论】:

  • 我没有看到在您的 root-context.xml 文件中创建了 DynDirectoryServiceImpl。是通过包裹扫描提取的吗?
  • 是的,在 root-context.xml 文件中。
  • @fxm 显示注释你的名字正确吗?
  • 代码本身是正确的,没有servlet系统也能完美运行。
  • @fxm .. 我认为问题出在此处。尝试在您的 xml 文件中将其更改为 base-package="com.dynamease.**base-package="com.dynamease

标签: java spring-mvc nullpointerexception


【解决方案1】:

尝试更改自动装配属性的类型:

@Autowired
private DynDirectoryServiceInterface

你也错过了

<context:annotation-config />

MVC 配置中的元素。

【讨论】:

  • 它似乎没有改变什么。
【解决方案2】:

首先,关于您的代码的一些想法: 您正在实现一个接口(这很好),但没有使用它(这很糟糕)。当您自动装配 DynDirectoryServiceImpl 时,您将绕过该接口。接口的目的是不允许其他实体直接查看您是如何实现代码的。

要使用接口自动装配,我会将接口命名为通用名称(即:“DynDirectoryService”),然后在 Impl 类中使用“@Component("DynDirectoryService")”。使用该类时,您会执行“@Autowire private DynDirectoryService variableName;”

执行此操作时需要注意的重要一点:您必须在实现和接口之间同步方法名称。如果您想在不对当前代码进行重大更改的情况下执行此操作,只需将其设为“@Component("DynDirectoryServiceInterface")”,然后在使用时执行“@Autowired DynDirectoryServiceInterface variableName;”

关于你的问题

System.out.println(dynDirectoryService.toString()); 

请注意,在您的 dynDirectoryServiceImpl 类中,您没有 .toString() 方法。你可能需要写一个。这应该是自动包含的方法之一,但我之前已经看到它没有包含在内,否则它只会吐出乱码,即正在使用的该类的实例。 (即:DynDirectoryServiceImpl@635446138792)。尝试将其添加到实现和接口中,看看它是否有帮助:

@Override
public String toString() {
    return "Hello, this is toString()";
}

另外,就像@SrinivasR 所说,您的组件扫描不应该是

<context:component-scan base-package="com.dynamease.**" />

除非你实际上在“com.dynamease”中有一个名为“**”的包,否则它应该是

<context:component-scan base-package="com.dynamease" />

组件扫描将自动神奇地查看“com.dynamease”中的所有子包。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-17
    • 2010-12-07
    • 1970-01-01
    • 2016-03-18
    • 2019-03-02
    • 2017-06-21
    • 1970-01-01
    • 2015-07-16
    相关资源
    最近更新 更多