我正在使用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
方面的任何见解。