Spring MVC的3.0 - 使用annonations服务层 [英] Spring MVC 3.0 - Service layer using annonations

查看:428
本文介绍了Spring MVC的3.0 - 使用annonations服务层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我用一个控制器类的Spring MVC应用程序,

  @Controller
公共类{myController的
@RequestMapping(方法= RequestMethod.POST,值=/*.htm)
公共myMethod的{@RequestBody最终MyRequestBean myRequest}}

我的输入是JSON的形式,我用杰克逊将其转换为Java对象。
现在,基于Java对象的财产,我要发送到相应的服务类。
如果是,则

  myRequest.value ==1 - > FirstService
myRequest.value ==2 - > SecondService

所有这些服务类将实施一个基本的接口。
我不想做这个实例中我的Java code。我怎样才能移动这一个配置文件。我怎样才能动态地注入基于请求值这些对象。请注意,我用的注解。
是新来Spring.Please提醒

编辑:包括我的解决方案结果
在我dispatcher.xml,

 `< UTIL:映射id =MYMAP>
<输入键=服务1值=com.service.MyService1/>
<输入键=服务2值=com.service.MyService1/>
< / UTIL:地图>`

在我的控制器,

@Resource私人地图MYMAP;

和在内部控制器的方法,结果,
                                                                                                                                                                    为MyService为myService =(则将MyService)的Class.forName((字符串)myMap.get(myRequest.getValue()))的newInstance()。的System.out.println(我的价值+ myService.doService());

如可以看到的,已经使用Java反射用于从我的XML返回的字符串得到实例。这是正确的方法是什么?


解决方案

  1. 创建创建所有可能的实例之一FactoryBean的一个

  2. 使用线程本地架 RequestAttributes 来获取请求

  3. @Scope的控制器(请求)

  4. 将接口注入服务

这应该很容易让控制器。但它是一个丑陋的做法。这里是另一个问题:


  1. 注入列表与LT; YourInterface>

  2. 在接口定义一个方法 getImplementationId()

  3. 在每个请求遍历表,并找出了返回正确的ID的implwmentation

这方面的一个变化将是两种:


  • prepopulate与键地图 - 关键的JSON未来和价值 - 特定implement执行

  • 包括在实现bean名jsod键和注入地图<弦乐,YourInterface>

然后,通过键查找

In my Spring MVC application with a single Controller class,

@Controller
public class MyController {
@RequestMapping(method = RequestMethod.POST, value = "/*.htm")
public myMethod{@RequestBody final MyRequestBean myRequest}

}

My input is in the form of JSON and I use Jackson for converting it to Java object. Now, based on the property in the Java object, I want to send to appropriate service class. If it is,

myRequest.value == "1" -> FirstService
myRequest.value == "2" -> SecondService

All these Service classes will be implementing a basic interface. I dont want to do this instantiation in my Java code. How can I move this to a configuration file. How can I inject these objects dynamically based on the request values. Please note that am using annotations. Am new to Spring.Please advise

EDIT: To include my solution
In my dispatcher.xml,

`<util:map id="myMap">
<entry key="service1" value="com.service.MyService1" /> 
<entry key="service2" value="com.service.MyService1" />
</util:map>` 

In my controller,

@Resource private Map myMap;

and in the method inside controller,
MyService myService = (MyService) Class.forName((String)myMap.get(myRequest.getValue())).newInstance(); System.out.println("My value" + myService.doService());

As can be seen, have used Java reflection for getting the instance from the string returned from my XML. Is this the correct way?

解决方案

  1. Create a FactoryBean that creates one of all possible instances
  2. Use the thread local holder RequestAttributes to obtain the request
  3. Make the controller of @Scope("request")
  4. Inject the service by interface

That should make it easy for the controller. But it is an ugly approach. Here is another one:

  1. Inject a List<YourInterface>
  2. Define a method in the interface getImplementationId()
  3. On each request iterate the list and pick the implwmentation that returns the proper id

A variation of this would be to either:

  • prepopulate a Map with key - the key coming from json, and value - a specific impl
  • Include the jsod key in the implementation bean name and inject a Map<String, YourInterface>

Then lookup by key

这篇关于Spring MVC的3.0 - 使用annonations服务层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆