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("...");
    }
 }

スプリング構成は、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