【转载】play framework 2.0 实战(4)- spring

在Java开发中,spring可以说解决了不少问题。可以解决实体之间的问题。减少耦合度。在play framework 2.0中也可以应用上。对于spring 的模块加入首先我们需要在Build.scala中添加spring的包。

"play" % "spring_2.9.1" % "1.0-SNAPSHOT"

还要在application.conf中添加spring的模块支持:

spring.context = application-context.xml(这个是针对spring的配置文件,类之间的包含关系等。之后会说)

# Defaults to true

spring.namespace-aware = true

# Defaults to true

spring.add-play-properties = true

#spring.component-scan = beans

对于项目的结构我基本上是在app 新增services package(anyway 每个人在这里都有不同的使用。不喜欢的可以按照自己的方法)

 

//接口
public interface IGdouCatalogService {

 

    public ArrayList  GetCatalogList();

 

    public ArrayList GetCatalogByID(int catalogID);

}

 

 //实现

 

public class GdouCatalogService implements IGdouCatalogService {

 

 

    public ArrayList  GetCatalogList()

    {

        ArrayList jsonList= new ArrayList();

 

        List catalogList=GdouCatalog.GetCatalogList();

 

        for(GdouCatalog item :catalogList)

        {

            Map

 

            node.put("catalogID",item.catalogID);

            node.put("catalogName",item.name);

            node.put("intro",item.intro);

            node.put("website",item.website);

            node.put("img",item.img);

 

 

            ArrayList jsonMobileList= new ArrayList();

 

            for(GdouMobileItem mobile:item.itemList)

            {

                Map

 

                mobileNode.put("mobileID",mobile.mobileID);

                mobileNode.put("mobileName",mobile.name);

                mobileNode.put("downloadUrl",mobile.downloadUrl);

 

                jsonMobileList.add(mobileNode);

 

            }

 

            node.put("moibleList",jsonMobileList);

 

            jsonList.add(node);

        }

 

        return jsonList;

 

    }

 

    public ArrayList GetCatalogByID(int catalogID)

    {

        ArrayList jsonList= new ArrayList();

 

        List catalogList=GdouCatalog.GetCatalogByID(catalogID);

 

        for(GdouCatalog item :catalogList)

        {

            Map

 

            node.put("catalogID",item.catalogID);

            node.put("catalogName",item.name);

            node.put("intro",item.intro);

            node.put("website",item.website);

            node.put("img",item.img);

 

 

            ArrayList jsonMobileList= new ArrayList();

 

            for(GdouMobileItem mobile:item.itemList)

            {

                Map

 

                mobileNode.put("mobileID",mobile.mobileID);

                mobileNode.put("mobileName",mobile.name);

                mobileNode.put("downloadUrl",mobile.downloadUrl);

 

                jsonMobileList.add(mobileNode);

 

            }

 

            node.put("moibleList",jsonMobileList);

 

            jsonList.add(node);

        }

 

        return jsonList;

 

    }

 

}

通过从services的接口方法就可以通过controller进行调用。

 public class Api extends Controller{

 

    private  IGdouCatalogService   gdouCatalogService;

 //增加构造函数接收 Service ,这里个人觉得正好是依赖注入的关键。

    public  Api(IGdouCatalogService gdouCatalogService)

    {

        this.gdouCatalogService=gdouCatalogService;

    }

   //这里还有一点我把所有controller的方法去除掉静态,后面会解释

    @Transactional

    @BodyParser.Of(play.mvc.BodyParser.Json.class)

    public   Result GetCatalogList(){

        JsonNode json=request().body().asJson();

        ObjectNode result = Json.newObject();

 

        ArrayList jsonList= this.gdouCatalogService.GetCatalogList();    

 

 

        // return all as JSON

        return ok(Json.toJson(jsonList));

    }

 

 

    @Transactional

    @BodyParser.Of(play.mvc.BodyParser.Json.class)

    public  Result GetItemList(int catalogID){

        JsonNode json=request().body().asJson();

        ObjectNode result = Json.newObject();

 

        ArrayList jsonList= this.gdouCatalogService.GetCatalogByID(10000); 

 

 

        // return all as JSON

        return ok(Json.toJson(jsonList));

    }

 

}

 下面来看看 application-context.xml

 

这里注意需要诸如的类的关系,在api控制器中我们必须把构造方法与所带的函数关联上,有多少的函数就要绑定多少个bean。

这里我把controller作为spring的一部分所以我新增一个controllers/controllerFactory方法

public class ControllerFactory {

 

    public static Api getApiController() {

        return Spring.getBeanOfType(Api.class);

    }

 

    public static Application getAppController() {

        return Spring.getBeanOfType(Application.class);

    }

}

这个方法意味着我访问控制器时已经注入了相关的实例

这里需要修改route

 

# Home page

GET     /                                       controllers.ControllerFactory.getAppController.index()

 

# API Setting

 

GET     /Api/GetCatalogList                 controllers.ControllerFactory.getApiController.GetCatalogList() 

GET     /Api/GetItemList/:catalogID    controllers.ControllerFactory.getApiController.GetItemList(catalogID : Int)  

//看这里应该明白我为何需要把控制器的静态方法去掉了吧。

哈哈这样就可以把你的东西和spring牢牢绑定了。

推荐看看这个文档会对你有帮助的。

github.com/scott-phillips/Spring4Play2


转载于:https://my.oschina.net/wenivan/blog/77331

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值