English 中文(简体)
在MVC申请中,是否会发生依赖注射?
原标题:In an MVC application, where will the dependency injection take place?
最佳回答

依赖性注射的整个思想是而不是 。 在注射的情况下,这些依赖者应当只是“隐蔽”,而无需任何要求(控制转移)。 在使用<代码>ApplicationContext#getBean(String)时,你仍然要求依赖(a la Service Locator)并且现在要求将控制转换为而不是(即使这允许你轻易改变执行)。

因此,你应使用固定式或建筑式注射器制作<代码>MyController的星座和注射器。

public class MyController {

    private MyService myService;

    public MyController(MyService aService) { // constructor based injection
        this.myService = aService;
    }

    public void setMyService(MySerice aService) { // setter based injection
        this.myService = aService;
    }

    @Autowired
    public void setMyService(MyService aService) { // autowired by Spring
        this.myService = aService;
    }

    @RequestMapping("/blah")
    public String someAction()
    {
        // do something here
        myService.foo();

        return "someView";
    }
}

并且把春天混为一谈。

问题回答

As a compliment to Pascal s post, Martin Fowler: Inversion of Control Containers and the Dependency Injection pattern is a must read.

正如本塔所说,你应使用<代码>@Autowire通知来做到这一点。 除此之外,春天还支持基于名称和基于类型公约的注射。 我建议你读一下。





相关问题
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 ...

热门标签