English 中文(简体)
Spring MVC 2. 不按请求使用的目标进行注射
原标题:Spring MVC Dependency Injection not working in object called from within RequestMappingController

在我试图做以下工作时,我在普伦·姆博克有一个叫马普车控制员班,这个班子即时叫外派,我无法在这个班子内使用依赖注射,例如,如果我试图使用这种注射。

@Resource(name = "savedsearchesService")
        private SavedSearchesService savedsearchesService;

我获得一个无名例外。 我不得不通过<代码>SavedSearches Service Savesearchesservice,从我的控制器法到其他外部方法,以便使其得以工作。

I m wondering if anyone can point out why it is this way as I am curious and if there is something I am missing as far as how to do this properly. Thanks

@Controller
@RequestMapping("")
public class MainController {

    @Resource(name = "savedsearchesService")
    private SavedSearchesService savedsearchesService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String getPersons(Model model, HttpServletRequest request)
            throws IOException {

        HttpSession session = request.getSession();


        SomeExternalClass someExternalClass = new SomeExternalClass ();
        someExternalClass.Main();
    }

}

外部类别的例子如下:

public class SomeExternalClass {

    @Resource(name = "savedsearchesService")
    private SavedSearchesService savedsearchesService;

    public void Main () {
        savedsearchesService.get();

    }
}
最佳回答

问题在于:

SomeExternalClass someExternalClass = new SomeExternalClass();

取决于春季依赖注射的所有班级都必须是的代用品管理<>strong>,如果你用new关键词当值班,则情况并非如此。

有一些替代方案。 提一下:

  • Inject SomeExternalClass into You controller.

  • 创立一个含有即时逻辑的春季-生态工厂(例如apptext.getBean()

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...