0

我正在使用 3.2.3.RELEASE 开发一个 Spring 项目,并且无法让依赖注入与非 Spring 托管对象一起工作。具体使用如下对象使用@Configurable.

@Configurable
public class DomainObject
{
    @Autowired
    private MessageService messageService;

    private String name;

    public void testDependencies()
    {
        if(messageService != null)
            System.out.println("...");
        else
            throw new RuntimeException("...");
    }
 }

spring 配置是纯注释驱动的,在 web.xml 之外没有 xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <display-name>Test AOP Configuration</display-name>
 <servlet>
       <servlet-name>SpringDispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextClass</param-name>
                <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
                </param-value>
            </init-param>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>com.test.configuration</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
       </servlet>
       <servlet-mapping>
            <servlet-name>SpringDispatcher</servlet-name>
            <url-pattern>/</url-pattern>
       </servlet-mapping>
</web-app>

MvcConfiguration.class

@Configuration
@ComponentScan(basePackages = {"com.test"})
@EnableWebMvc
@EnableSpringConfigured
@EnableAspectJAutoProxy
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
public class MvcConfiguration extends WebMvcConfigurerAdapter
{
    @Bean
   public ViewResolver getViewResolver()
   {
       InternalResourceViewResolver resolver = new InternalResourceViewResolver();
       resolver.setPrefix("/WEB-INF/views/");
       resolver.setSuffix(".jsp");
       return resolver;
   }

   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry)
   {
       registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
   }

   ...remaining bean definitions

一切似乎都在工作,直到我尝试在Controller. 只要控制器映射不包含控制器映射DomainObject@RequestBody参数,它就会正确自动连接。

这个例子完美地工作,一切都反序列化并且消息服务按我的预期注入。

@Controller
public class HomeController
{
   @RequestMapping(value = "/test")
   public ModelAndView test(@RequestParam String id, @RequestBody String json) throws IOException
   {
      System.out.println(StringSupport.repeatString("*", 120, ""));
      System.out.println("id = " + id);
      System.out.println(StringSupport.repeatString("*", 120, ""));
      try
      {
         JsonUtility.readValue(json, DomainObject.class).testDependencies();
      }
      catch (Exception exception)
      {
         exception.printStackTrace();
      }
      System.out.println(StringSupport.repeatString("*", 120, ""));
      try
      {
         new DomainObject().testDependencies();
      }
      catch (Exception exception)
      {
         exception.printStackTrace();
      }
      System.out.println(StringSupport.repeatString("*", 120, ""));
      return new ModelAndView("home");
   }
}

然而,这是我最初的意图,传入的对象或使用 new 创建的对象都没有注入其依赖项。

   @RequestMapping(value = "/test")
   public ModelAndView test(@RequestParam String id, @RequestBody DomainObject domainObject) throws IOException
   {
      System.out.println(StringSupport.repeatString("*", 120, ""));
      System.out.println("id = " + id);
      System.out.println(StringSupport.repeatString("*", 120, ""));
      try
      {
         domainObject.testDependencies();
      }
      catch (Exception exception)
      {
         exception.printStackTrace();
      }
      System.out.println(StringSupport.repeatString("*", 120, ""));
      try
      {
         new DomainObject().testDependencies();
      }
      catch (Exception exception)
      {
         exception.printStackTrace();
      }
      System.out.println(StringSupport.repeatString("*", 120, ""));
      return new ModelAndView("home");
   }
}

任何对@RequestBody反序列化过程或@Configurable方面的深入了解都将不胜感激。

4

0 回答 0