Dependency Injection for annotation based Controllers in spring mvc [closed]
How to set properties into annotated controllers in Spring?
It sounds like you want to avoid the @Autowired annotation, but you do want to use annotation-style SpringMVC controllers. There's no reason that you have to use both. You can instantiate the bean in XML like any other. I'll give you an example below. I'd encourage you, though, to consider using @Autowired at least for controllers, if not for other services. It makes their code easier to create and to read, and as long as you only use it from controllers, it shouldn't lead to any awkward, confusing interdependencies.
@Controller
public class MyController {
private String field;
public void setField(String field) {
this.field = field;
}
@RequestMapping("/Wooo")
public String handler(ModelMap model) {
model.addAttribute("thefield",field);
return "fieldViewer";
}
}
And then in the XML:
<bean class="com.mything.MyController">
<property name="field">waffles</property>
</bean>
<mvc:annotation-driven/>
Use the @Autowired
and @Qualifier
annotations inside the Controller
; same as any other class that you need to autowire using annotations. Be sure to put the component scan into your context XML.
精彩评论