1

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 が初期化されていないことです (それらにアクセスしようとすると、Null ポインター例外が発生します)。たとえば、 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;
        }
}
4

2 に答える 2

2

autowired プロパティのタイプを変更してみてください。

@Autowired
private DynDirectoryServiceInterface

あなたも欠けています

<context:annotation-config />

MVC 構成の要素。

于 2013-06-13T15:04:31.677 に答える