gpt4 book ai didi

java - Spring MessageSource 进入服务

转载 作者:行者123 更新时间:2023-12-01 14:28:34 27 4
gpt4 key购买 nike

如果我将最后一个放在 Controller 之外但放在这样的服务内,我会遇到使用 MessageSource 读取属性的问题:

@Service
public class MessageHandler {

private static final Logger logger = LoggerFactory.getLogger(MessageHandler.class);

@Autowired
private static MessageSource messageSource;

public static void returnMessage(String key, Locale locale) {
logger.info(key);
logger.info(locale.toString());
logger.info(messageSource.getMessage(key, null, locale));
}

}

此服务位于:com.test.rest.utility

servlet-context.xml

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

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

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

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

<beans:bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basename" value="classpath:properties/welcome" />
<beans:property name="fallbackToSystemLocale" value="false" />
</beans:bean>

<beans:bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<beans:property name="defaultLocale" value="en" />
</beans:bean>

<interceptors>
<beans:bean
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang" />
</beans:bean>
</interceptors>


<beans:bean id="userService" class="com.test.rest.dao.UserDaoImpl" />

<context:component-scan base-package="com.test.rest.utility" />
<context:component-scan base-package="com.test.rest.controller" />



</beans:beans>

调用服务的 Controller :

@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody List<Project> home(Locale locale, Model model) {
String key = "message";
MessageHandler.returnMessage(key, locale);
}

当进入服务调用 messageSource.getMessage(key, null, locale) 时,应用程序返回 NullPointerException

谁能帮我解决这个问题?

最佳答案

你应该添加<context:component-scan base-package="package where services is located"/>扫描您的服务并将其配置为 spring bean,然后它将可用于其他 beans

关于java - Spring MessageSource 进入服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49796228/

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