Nodejs与spring实战,推广IOC REST使用
Nodejs与spring实战,推广IOC REST使用
为了证明 springnodejs 框架的确比老外写的好用,本人做几个demo来展示一下
- 服务端用 springnodejs
- 数据库用 mongo
- 前端UI jquery + Bootstrap ui + springnodejs ioc 例子暂时没有想到,就像当之前刚写这个框架一样,从无到有,也欢迎大家给点例子
springnodejsExample
作者 : solq
blog :
- http://www.cnblogs.com/solq/p/3574640.html
- http://www.jiaotuhao.com/
- http://www.springnodejs.com/
- http://example.springnodejs.com/
git : https://github.com/solq360/springnodejs https://github.com/solq360/springnodejsExample.git
QQ群:9547527
先来介绍框架文件 /core/AppContext.js
整个框架最核心的,所有的容器都经过 AppContext 扫描注册,而扫描配置,在/config/Appconfig.js 文件里 为什么分这么多目录呢,这根据不同的领域进行划分,以目录的形式进行区分管理,以后可能会介绍面向领域设计 现在大家只需要知道,开发一个业务,需要 config 资源,还有提供外部访问的服务类/控制类,最后给外部访问的公开类.三样
/config/Appconfig.js
- ./core 是框架的核心,一般不用写代码在这里
- ./config 配置资源
- ./module 业务模块
- ./ws 对外公开访问 webservice
var appConfig={
/**auto scan config**/
scan :{
'./core' : {
injectionType : 'core'
},
'./config' : {
injectionType : 'config'
},
'./module' : {
injectionType : 'service'
},
'./ws' : {
filter : '\\ws', //url 服务 过滤器
injectionType : 'controller',
//include : [],
exclude : ['./ws/test/test1.js']
}
}
};
app.js 很简单,就一个入口
var appContext = require("./core/AppContext.js");
appContext.runServer();
var httpServer = appContext.findContainer(“httpServer”);
httpServer.runServer();
- appContext.runServer 方法包括,文件扫描/注册,自动依赖注入,初化容器阶段
- appContext.findContainer(id) 通过ID就可以查找容器,ID以文件名注册
/core/HttpServer.js 内部已经做好一个 REST 的 HttpServer 应用 只要执行 就可以工作 如果你想加 socket 通信的话,是不是很简单?
接下来可以做什么呢? 我们先要熟悉IOC自动注入。 首先在 ./module/account/service/AccountService.js 建个JS文件
module.exports = {
user : 'solq',
postConstruct : function(){
console.log('account service postConstruct');
},
preDestroy : function(){
console.log('account service preDestroy');
}
};
- 建好后运行一下 node app.js 看看
- 然后在控制台 输入 close_server 看看
- 容器初始化的时候会执行 postConstruct 方法
- 服务器关闭时会执行 preDestroy 方法
- 顺便说下控制台命令在 ./core/console.js
接下来,AccountService.js 在其它地方怎么找到用上啊??
./module/test/service/TestService.js 我们再来建一个JS
module.exports = {
auto_accountService : null,
postConstruct : function(){
console.log('testService postConstruct');
console.log('auto_accountService',this.auto_accountService.user);
}
};
再运行一下 node app.js 看看 是不是自动注入你写的服务了? auto_(容器ID) 就可以注入
REST
./ws/test/test.js 新建JS
module.exports = {
'get:/test':{
controller : function(){
return {'key':'hello rest'};
}
}
};
./static/js/main.js 添加
$.ajax({
type : 'get',
dataType : 'json',
url : '/ws/test',
success : function(json){
alert(json.key);
}
});
然后 node app.js
传送参数
module.exports = {
'get:/test':{
controller : function(){
return {'key':'hello rest'};
}
} ,
'/test/{path1}/{path2}':{
controller : function(path_path1,path_path2,param_array_p1,param_int_p2,body_body){
console.log("path_p1 = " , path_path1);
console.log("path_p2 = " , path_path2);
console.log("param_array_p1 = " , param_array_p1);
console.log("param_int_p2 = " , param_int_p2);
console.log("body_body = " , body_body);
return body_body;
}
}
};
前端
var post_data = {p1:'xxx',p2:'555'};
$.ajax({
type : 'get',
dataType : 'json',
url : '/ws/test/a/b',
data : post_data,
success : function(json){
console.log(json);
}
});
- path_ 标识会将路径截取
- param_ 标识会将 请求参数截取
- body_ 标识会将请求参数变成 json
- array_ 数据类型转换 array
- int_ 数据类型转换 int
- req,res,不用说了吧?
相信你会爱上这风格
Nodejs与Spring实战,推广IOC REST使用
介绍
为了证明 springnodejs
框架的确比老外写的更方便使用,本文将通过几个Demo来展示其强大的功能。我们将使用以下技术栈:
- 服务端:使用
springnodejs
- 数据库:使用 MongoDB
- 前端UI:使用 jQuery 和 Bootstrap UI,并结合
springnodejs
的 IOC 注入
示例代码
首先,我们需要安装 springnodejs
框架。可以通过 npm 安装:
npm install springnodejs
接下来,我们创建一个简单的项目结构,以便更好地理解如何使用 springnodejs
。
项目结构
my-project/
├── app.js
├── core/
│ ├── AppContext.js
│ └── HttpServer.js
├── config/
│ └── AppConfig.js
├── module/
│ └── account/
│ └── service/
│ └── AccountService.js
└── ws/
└── test/
└── test.js
代码详解
1. AppContext.js
- 框架核心
AppContext.js
是整个框架的核心,负责文件扫描、注册以及自动依赖注入。
// core/AppContext.js
var appConfig = require('./AppConfig');
class AppContext {
constructor() {
this.containers = {};
}
runServer() {
// 文件扫描、注册、自动依赖注入、初始化容器
appConfig.scan.forEach((config, path) => {
// 执行扫描和注册逻辑
});
}
findContainer(id) {
return this.containers[id];
}
}
module.exports = new AppContext();
2. AppConfig.js
- 配置文件
AppConfig.js
定义了扫描配置,指定了哪些目录需要被扫描。
// config/AppConfig.js
module.exports = {
scan: {
'./core': {
injectionType: 'core'
},
'./config': {
injectionType: 'config'
},
'./module': {
injectionType: 'service'
},
'./ws': {
filter: '\\ws',
injectionType: 'controller',
exclude: ['./ws/test/test1.js']
}
}
};
3. AccountService.js
- 服务类
AccountService.js
是一个简单的服务类,包含初始化方法和销毁方法。
// module/account/service/AccountService.js
module.exports = {
user: 'solq',
postConstruct: function() {
console.log('account service postConstruct');
},
preDestroy: function() {
console.log('account service preDestroy');
}
};
4. TestService.js
- 测试服务类
TestService.js
通过自动注入的方式引用了 AccountService
。
// module/test/service/TestService.js
module.exports = {
auto_accountService: null,
postConstruct: function() {
console.log('testService postConstruct');
console.log('auto_accountService', this.auto_accountService.user);
}
};
5. test.js
- REST 控制器
test.js
是一个 REST 控制器,处理 HTTP 请求。
// ws/test/test.js
module.exports = {
'get:/test': {
controller: function() {
return { key: 'hello rest' };
}
},
'/test/{path1}/{path2}': {
controller: function(path_path1, path_path2, param_array_p1, param_int_p2, body_body) {
console.log("path_p1 = ", path_path1);
console.log("path_p2 = ", path_p2);
console.log("param_array_p1 = ", param_array_p1);
console.log("param_int_p2 = ", param_int_p2);
console.log("body_body = ", body_body);
return body_body;
}
}
};
6. 前端调用
前端通过 jQuery 发送 AJAX 请求。
// static/js/main.js
$.ajax({
type: 'get',
dataType: 'json',
url: '/ws/test',
success: function(json) {
alert(json.key);
}
});
总结
通过以上代码示例,我们可以看到 springnodejs
框架的简洁性和易用性。它不仅支持自动依赖注入,还提供了强大的 REST 支持。希望这些示例能帮助你更好地理解和使用 springnodejs
框架。
git : https://github.com/solq360/springnodejsExample.git
暂时把架子弄好,没有什么内容。前端JS也采用 ioc 自动依赖注入方式。
demo在哪?
相信大家都用过 express 缺点我就不说了
express好像只是个web框架吧,当然不会考虑IOC之类的,nodejs在后台方面好像没有什么优势,后台总有很多耗时的操作,node的单进程处理起来本身就很吃力。
以后JS 标准会支持多线程的。如果不考虑并发的话可用多进程处理
看你怎么用了,我是从PHP学起的,NODEJS 从某种意义来讲从前端跑向后端是种伟大的变革,就像 jquery 一样伟大。 相信nodejs的野心是很大的,以后到处都有它的足迹,ubuntu touch 出手机了,是JS的天下来了
在这个帖子中,作者展示了如何使用 springnodejs
框架来实现 IOC(控制反转)和 RESTful API。以下是一个简化的示例,以说明这些概念。
1. 定义服务
首先,定义一个简单的服务 AccountService
:
// ./module/account/service/AccountService.js
module.exports = {
user: 'solq',
postConstruct: function() {
console.log('account service postConstruct');
},
preDestroy: function() {
console.log('account service preDestroy');
}
};
2. 自动注入服务
然后,在另一个服务中自动注入 AccountService
:
// ./module/test/service/TestService.js
module.exports = {
auto_accountService: null,
postConstruct: function() {
console.log('testService postConstruct');
console.log('auto_accountService', this.auto_accountService.user);
}
};
3. 定义 REST 控制器
接下来,定义一个 REST 控制器来处理 HTTP 请求:
// ./ws/test/test.js
module.exports = {
'get:/test': {
controller: function() {
return { key: 'hello rest' };
}
},
'/test/{path1}/{path2}': {
controller: function(path1, path2, param_array_p1, param_int_p2, body) {
console.log("path1 =", path1);
console.log("path2 =", path2);
console.log("param_array_p1 =", param_array_p1);
console.log("param_int_p2 =", param_int_p2);
console.log("body =", body);
return body;
}
}
};
4. 启动应用
最后,启动应用并测试 REST API:
// app.js
var appContext = require("./core/AppContext.js");
appContext.runServer();
var httpServer = appContext.findContainer("httpServer");
httpServer.runServer();
通过以上步骤,你可以看到 springnodejs
框架如何简化服务的定义、注入以及 RESTful API 的创建。希望这个示例能帮助你理解 springnodejs
框架的工作方式。