简体   繁体   English

Spring @Value注释无法解析

[英]Spring @Value Annotation Not Resolving

I realize this question has been asked countless times, but none of the solutions appear to be working for my situation. 我意识到这个问题已经被问过无数次,但是没有一种解决方案适合我的情况。

I have a basic Spring-WS app I'm putting together with a single servlet context. 我有一个基本的Spring-WS应用程序,它与一个servlet上下文结合在一起。 I can see that both the @Component annotated class and the properties file are both getting picked up on startup as shown by the logs below. 我可以看到,@ Component注释类和属性文件都在启动时被选中,如下面的日志所示。 However, my @Value annotated String is coming back as null. 但是,我的@Value注释的String返回为null。

  1. Does anyone see anything wrong with the way I've set this up? 有人认为我的设置方式有问题吗? Please feel free to ask for any additional information I may have left out. 请随时询问我可能遗漏的任何其他信息。
  2. Going forward, what are the best methods I can use to investigate issues like this I encounter in the future? 展望未来,我可以使用哪些最佳方法来调查将来遇到的此类问题?

directmail-manager-servlet.xml: directmail经理-servlet.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"
  xmlns:sws="http://www.springframework.org/schema/web-services"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.xxx.direct.mailserver"/>
    <context:property-placeholder location="classpath:manager.properties"/>

    <sws:annotation-driven/>

    <sws:dynamic-wsdl id="manager" portTypeName="EcwDirect"
        locationUri="/mailerManagerService/" targetNamespace="http://obfuscated.com/direct/definitions">
        <sws:xsd location="/WEB-INF/mailManagerRequest.xsd" />
    </sws:dynamic-wsdl>

</beans>

web.xml: web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    version="3.0" 
    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">

    <display-name>xxxxxxxxx</display-name>

    <servlet>
        <servlet-name>directmail-manager</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        <init-param>
            <param-name>transformWsdlLocations</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>directmail-manager</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

manager.properties: manager.properties:

mailserver.url="localhost"

My annotated class: 我带注释的课:

package com.xxx.direct.mailserver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class TelnetMailServerUserController implements MailServerUserController {

    @Autowired
    @Value("${mailserver.url}")
    String mailserverUrl;

    @Override
    public void addUser(String username, String password) {

        System.out.println("server URL:" + mailserverUrl);
    }
}

class that uses this class: 使用此类的类:

@Service
public class MailManagerService {

    //TODO: craft and return response
    public void registerNewUser(List<Element> usersToAdd) {

        MailServerUserController userController = new TelnetMailServerUserController();

        //TODO: add users fo realz
        for(Element user : usersToAdd) {

            String emailAddress = user.getChildText("emailAddress", MailManagerEndpoint.NAMESPACE);
            String password = user.getChildText("password", MailManagerEndpoint.NAMESPACE);

            //TODO: log this:
            System.out.println("user " + emailAddress + " added");

            userController.addUser(emailAddress, password);

            //TODO: add to database
        }
    }
}

logs: 日志:

Apr 23, 2014 11:54:41 AM com.springsource.tcserver.security.PropertyDecoder <init>
INFO: tc Runtime property decoder using memory-based key
Apr 23, 2014 11:54:42 AM com.springsource.tcserver.security.PropertyDecoder <init>
INFO: tcServer Runtime property decoder has been initialized in 261 ms
Apr 23, 2014 11:54:42 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Apr 23, 2014 11:54:42 AM com.springsource.tcserver.serviceability.rmi.JmxSocketListener init
INFO: Started up JMX registry on 127.0.0.1:6969 in 128 ms
Apr 23, 2014 11:54:42 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 959 ms
Apr 23, 2014 11:54:42 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Apr 23, 2014 11:54:42 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: VMware vFabric tc Runtime 2.9.3.RELEASE/7.0.42.A.RELEASE
Apr 23, 2014 11:54:42 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor C:\springsource\vfabric-tc-server-developer-2.9.3.RELEASE\base-instance\conf\Catalina\localhost\ROOT.xml
Apr 23, 2014 11:54:42 AM org.apache.catalina.startup.SetContextPropertiesRule begin
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:mail-server-manager' did not find a matching property.
Apr 23, 2014 11:54:44 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Apr 23, 2014 11:54:44 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\springsource\vfabric-tc-server-developer-2.9.3.RELEASE\base-instance\webapps\manager
Apr 23, 2014 11:54:44 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Apr 23, 2014 11:54:44 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1319 ms
Apr 23, 2014 11:54:44 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'directmail-manager'
Apr 23, 2014 11:54:44 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'directmail-manager': initialization started
Apr 23, 2014 11:54:44 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'directmail-manager-servlet': startup date [Wed Apr 23 11:54:44 EDT 2014]; root of context hierarchy
Apr 23, 2014 11:54:44 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/directmail-manager-servlet.xml]
Apr 23, 2014 11:54:44 AM org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
INFO: Loading properties file from class path resource [manager.properties]
Apr 23, 2014 11:54:45 AM org.springframework.ws.soap.addressing.server.AbstractAddressingEndpointMapping afterPropertiesSet
INFO: Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
Apr 23, 2014 11:54:45 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7c0f6d9: defining beans [mailManagerEndpoint,mailManagerService,telnetMailServerUserController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping#0,org.springframework.ws.soap.server.endpoint.mapping.SoapActionAnnotationMethodEndpointMapping#0,org.springframework.ws.soap.addressing.server.AnnotationActionEndpointMapping#0,org.springframework.ws.server.endpoint.adapter.method.dom.DomPayloadMethodProcessor#0,org.springframework.ws.server.endpoint.adapter.method.SourcePayloadMethodProcessor#0,org.springframework.ws.server.endpoint.adapter.method.jaxb.XmlRootElementPayloadMethodProcessor#0,org.springframework.ws.server.endpoint.adapter.method.jaxb.JaxbElementPayloadMethodProcessor#0,org.springframework.ws.server.endpoint.adapter.method.dom.JDomPayloadMethodProcessor#0,org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter#0,org.springframework.ws.soap.server.endpoint.SoapFaultAnnotationExceptionResolver#0,org.springframework.ws.soap.server.endpoint.SimpleSoapExceptionResolver#0,org.springframework.xml.xsd.SimpleXsdSchema#0,manager,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Apr 23, 2014 11:54:45 AM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet
INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
Apr 23, 2014 11:54:45 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'directmail-manager': initialization completed in 1012 ms
user test added
server URL:null

You aren't using a Spring managed bean. 您没有使用Spring托管的bean。 You are creating an object yourself. 您正在自己创建对象。

MailServerUserController userController = new TelnetMailServerUserController();

In this case, Spring is not involved in processing the @Value annotated field (or anything else for that matter). 在这种情况下,Spring不参与处理@Value注释的字段(或其他任何与此相关的事情)。

Instead, get the bean from the context, inject it. 而是从上下文中获取bean,然后将其注入。

@Autowired
private MailServerUserController userController;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM