gpt4 book ai didi

java - Spring @Value 注解无法解析

转载 作者:行者123 更新时间:2023-12-01 18:33:06 25 4
gpt4 key购买 nike

我意识到这个问题已被问过无数次,但似乎没有一个解决方案适合我的情况。

我有一个基本的 Spring-WS 应用程序,我将其与单个 servlet 上下文组合在一起。我可以看到 @Component 注解的类和属性文件都在启动时被拾取,如下面的日志所示。但是,我的 @Value 注释字符串返回为 null。

  1. 有人发现我的设置方式有什么问题吗?请随时询问我可能遗漏的任何其他信息。
  2. 展望 future ,我可以使用哪些最佳方法来调查将来遇到的此类问题?

directmail-manager-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:

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

ma​​nager.properties:

mailserver.url="localhost"

我的带注释的类(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);
}
}

使用该类的类:

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

日志:

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

最佳答案

您没有使用 Spring 托管 bean。您正在自己创建一个对象。

MailServerUserController userController = new TelnetMailServerUserController();

在这种情况下,Spring 不参与处理 @Value 带注释的字段(或任何其他相关字段)。

相反,从上下文中获取 bean,然后注入(inject)它。

@Autowired
private MailServerUserController userController;

关于java - Spring @Value 注解无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23249879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com