开发者

Dependency Injection for annotation based Controllers in spring mvc [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its curr开发者_高级运维ent form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜