0

私の豆は:

@Component
public class KidsServerStartUp implements ServletContextListener
{
     UploadService uplService;

    @Autowired
    public void setUplService( UploadService uplService )
    {
        this.uplService = uplService;
    }
    public void contextInitialized(ServletContextEvent event) {
       System.out.println ( uplService );
    }
}

web.xml; 私は最初に春のフレームワークを呼び出して、すべての Bean を設定しています。次に、起動リスナーを設定します。

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>com.kids.util.KidsServerStartUp</listener-class>
    </listener>

uplService が null として出力されています!

4

1 に答える 1

3

あなたが探しているのは、この投稿のようなものだと思います。

ServletContextListenerスプリング コンテキストを使用しているため、クラスの作成には使用されませんListenerApplicationContextしかし、を使用してにアクセスできますServletContext

public class KidsServerStartUp implements ServletContextListener {
    public void contextInitialized(ServletContextEvent event) {
        final WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
        UploadService uplService = springContext.getBean(UploadService.class);
        System.out.println ( uplService );
    }
}
于 2012-07-03T04:19:49.203 に答える