Spring MessageSource进入服务

时间:2018-04-12 12:13:08

标签: java spring-mvc

我在使用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>

我打电话给服务的控制器:

@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

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您无法自动归档静态归档。所以你可以:

  • 创建一个界面:MessageService
  • 将MessageService实现到MessageHandler
  • 使用以下方法注入服务:

    ...
                    ObjectHandle publicKey = null;
                    ObjectHandle privateKey = null;
    
                    List<ObjectAttribute> publicKeyAttributes = new List<ObjectAttribute>();
                    publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
                    publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "label"));
                    session.FindObjectsInit(publicKeyAttributes);
                    List<ObjectHandle> oObjCollection = session.FindObjects(1);
    
                    List<ObjectHandle> foundPublicKeys = session.FindAllObjects(publicKeyAttributes);
    
                    List<ObjectAttribute> objectAttributess = session.GetAttributeValue(foundPublicKeys[0], new List<CKA>() { CKA.CKA_ID, CKA.CKA_LABEL, CKA.CKA_VALUE });
                    byte[] ckaIdd = objectAttributess[0].GetValueAsByteArray();
                    string ckaLabel = objectAttributess[1].GetValueAsString();
                    byte[] ckaValue = objectAttributess[2].GetValueAsByteArray();
                    var _rawData = ckaValue ?? throw new ArgumentNullException(nameof(ckaValue));
                    var _parsedCertificate = new X509Certificate2(_rawData);
    
                    Cer c = new Cer();
    
                    var Key = c.GetRSAPublicKey(_parsedCertificate);
    
                    XmlDocument doc = new XmlDocument();
                    doc.Load(@"C:\Users\MyUser\Desktop\SampleFile.xml");
    
                    SignedXml signedXml = new SignedXml(doc);
    
                    signedXml.SigningKey = Key;
    
                    Reference reference = new Reference();
                    reference.Uri = "";
    
                    XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
                    reference.AddTransform(env);
    
                    signedXml.AddReference(reference);
    
                    signedXml.ComputeSignature(); // Error occurs in this line
    
    }}