0.3.29 • Published 3 months ago

bpframework v0.3.29

Weekly downloads
112
License
MIT
Repository
github
Last release
3 months ago

This project is still in development

Setup.

use cli to create a project.

npm i bpframework-cli -g

create a project.

bpframework init

Example.

see directory ./examples

Feature.

featuresupports
configbootstrap.ymlSpringCloudConfig
discoverynacos
scheduling@Scheduled
api routers@RestController

Configure.

The appropriate configuration is required to enable the corresponding feature: config

namedescription
FindMicroserviceConfigure定义自定义的服务发现处理方法
FeignClientConfigure定义FeignClient的默认headers等信息
RestControllerConfigure定义RestController的默认headers等信息

@FindMicroserviceConfigure

By default, nacos is used to find micro-service; You can customize it by @FindMicroserviceConfigure.

@Service()
class Configure {
  @FindMicroserviceConfigure
  async onFindMicroservice(serviceName: string, excludeHost: string): Promise<ServiceInfo> {
    return {
      ip,
      port,
      serviceName,
      metadata,
    }
  }
}

@RestControllerConfigure

定义RestController的默认headers等信息, 使用如下方式.

@Service()
class Configure {
  @RestControllerConfigure
  onConfigure(): bp.RestControllerConfigureInfo {
    return {
      defaultHeaders: {'content-type': 'application/json;charset=utf-8'},
    }
  }
}

@FeignClientConfigure

定义FeignClient的默认headers等信息, 使用如下方式.

@Service()
class Configure {
  @FeignClientConfigure
  onConfigure(): bp.FeignClientConfigureInfo {
    return {
      defaultHeaders: {'content-type': 'application/json;charset=utf-8'},
      /**
       * 对每次请求后接收的消息进行过滤.
       */
      filterResponseCallback: (data: FeignClientFilterResponseData) => {
        
      },
      /**
       * Processing the data of the request.
       */
      filterRequestCallback: (data: FeignClientFilterRequestData, feignData: FeignDataType) => {

      }
    }
  }
}

@Value

使用 @Value 注解设置初始值或获取配置值.

@Service()
class Demo {
  @Value("Miss A")
  teacher1Name: string; // will set to 'Miss A'

  @Value("${teacherName2}")
  teacher2Name: string; // will set to config value "teacherName2"

  @Value("${teacherName3:defaultName}")
  teacher3Name: string; // will set to 'defaultName' if config value "teacherName3" isn't existed.
}

Middleware.

see https://github.com/bpcloud/middleware.git

Event Listener.

namedescription
ContextRefreshedEventListener本地配置加载完成, 系统service对象初始化完成
RefreshRemoteEventListener远程配置动态刷新事件
InstanceRegisteredEventListener实例注册到注册中心后的事件

@ContextRefreshedEventListener

本地配置加载完成, 系统service对象初始化完成.

@Service()
class ApplicationEvent {
  @ContextRefreshedEventListener
  async onContextRefreshed(ev:ContextRefreshedEvent):void {

  }
}

@RefreshRemoteEventListener

远程配置动态刷新事件.

@Service()
class ApplicationEvent {
  @RefreshRemoteEventListener
  async onRefreshRemote(ev:RefreshRemoteEvent):void {

  }
}

@InstanceRegisteredEventListener

实例注册到注册中心后的事件.

@Service()
class ApplicationEvent {
  @InstanceRegisteredEventListener
  async onInstanceRegistered(ev:InstanceRegisteredEvent):void {
    
  }
}

Scheduling

@Scheduled

使用此注解可以开启一个定时任务.

@Service()
class Demo {
  @Scheduled({cron:'* * * * * *'})
  async onTick(): Promise<false|void> {
    return false; // 返回false则表明停止此task.
  }
}
  • Start task: 当类实例被创建后, task即按照时间间隔运行
  • Stop task: 当@Scheduled修饰的方法明确返回false时, task将停止

Bean

@Service

可以使用此注解实例化对象

/**
 * 加载所有的bean, 并进行实例化等操作.
 */
export function finishBeans(): Promise<void>;

/**
* @desc 获得已装配完的指定类型的service.
*/
export function getServiceInstances(key: any): ServiceInstanceType;

/**
 * 无需等待执行 finishBeans().
 * 
 * @returns {ClassDecorator}
 */
export function ImmediatelyService(name: string): ClassDecorator;
export function ImmediatelyService(cfg?: { singleton?: boolean, name?: string }): ClassDecorator;

/**
 * @desc 表明指定的类为Service类.
 * 
 * 定义为Service的类, 在源文件被引用后, 单例bean将会自动在全局创建一个实例.
 * 
 * @description 
 *  `Service` 与 `Bean` 都是延迟注入类型; 需要在 `finishBeans()` 方法调用之后才能够生效.
 *  需实现立即生效类型使用 `ImmediatelyService`
 * 
 * @param cfg.singleton 是否为单例; (默认单例)
 * @param cfg.name      使用名称注入; 如不使用名称,则使用类型注入.
 *
 * @returns {ClassDecorator}
 */
export function Service(name: string): ClassDecorator;
export function Service(cfg?: { singleton?: boolean, name?: string }): ClassDecorator;

示例:

/**
 * 在app初始化完成后将自动实例化.
 */
@Service()
class Example {
  constructor() {}
}

/**
 * 立即自动实例化.
 */
@ImmediatelyService()
class Example {
  constructor() {}
}

@Bean

/**
 * @desc 表明指定的属性为Bean.
 * 
 * <Bean修饰的方法不允许带参数, 并且返回的类型作为注入对象的类型.>
 * 定义为Bean, 在源文件被引用后, 单例bean将会自动在全局创建一个实例.
 * 
 * @description 
 *  `Service` 与 `Bean` 都是延迟注入类型; 需要在 `finishBeans()` 方法调用之后才能够生效.
 *  需实现立即生效类型使用 `ImmediatelyService`
 * 
 * @param cfg.singleton 是否为单例; (默认单例)
 * @param cfg.name      使用名称注入; 如不使用名称,则使用方法名注入.
 * 
 * @example
 * 
 * ﹫Service()
 * class {
 *       ﹫Bean() 
 *       foo(): Object { 
 *           return {};
 *       }
 * 
 *       ﹫Autowired('foo')
 *       private obj: Object;
 * }
 * @returns {PropertyDecorator}
 */
export function Bean(name: string): MethodDecorator;
export function Bean(cfg?: { singleton?: boolean, name?: string }): MethodDecorator;

@Autowired

/**
 * @desc 表明指定的属性可以自动装载指定的Service实例.
 * 
 * @example
 *  ﹫Autowired(ClassA)
 *  obj: ClassA;  // will to auto create object.
 * 
 * @returns {PropertyDecorator}
 */
export function Autowired(type: Function|string): PropertyDecorator;

@Value

/**
 * @desc 表明指定的属性可以自动装载指定的值.
 * @description 无需添加 RefreshScope 注解; 在配置刷新时会自动变更值.
 * @example
 *   ﹫Service()
 *   class Demo {
 *     ﹫Value("Miss A")
 *     teacher1Name: string; // will set to 'Miss A'
 * 
 *     ﹫Value("${teacherName2}")
 *     teacher2Name: string; // will set to config value "teacherName2"
 * 
 *     ﹫Value("${teacherName3:defaultName}")
 *     teacher3Name: string; // will set to 'defaultName' if config value "teacherName3" isn't existed.
 *   }
 * 
 * @returns {PropertyDecorator}
 */

export function Value(value: any): PropertyDecorator;
0.3.29

3 months ago

0.3.28

3 months ago

0.3.27

3 months ago

0.3.26

4 months ago

0.3.25

4 months ago

0.3.24

5 months ago

0.3.23

3 years ago

0.3.22

3 years ago

0.3.20

3 years ago

0.3.21

3 years ago

0.3.19

3 years ago

0.3.18

3 years ago

0.3.17

3 years ago

0.3.16

3 years ago

0.3.15

3 years ago

0.3.14

3 years ago

0.3.13

3 years ago

0.3.9

4 years ago

0.3.12

4 years ago

0.3.11

4 years ago

0.3.10

4 years ago

0.3.8

4 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.0

4 years ago

0.3.1

4 years ago

0.2.30

4 years ago

0.2.29

4 years ago

0.2.27

4 years ago

0.2.26

4 years ago

0.2.28

4 years ago

0.2.25

4 years ago

0.2.24

4 years ago

0.2.23

4 years ago

0.2.22

4 years ago

0.2.21

4 years ago

0.2.20

4 years ago

0.2.19

4 years ago

0.2.18

4 years ago

0.2.17

4 years ago

0.2.16

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.1.14

4 years ago

0.1.15

4 years ago

0.1.16

4 years ago

0.1.13

4 years ago

0.1.10

4 years ago

0.1.11

4 years ago

0.1.12

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.3

4 years ago

0.1.5

4 years ago

0.1.0

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.18

5 years ago

0.0.15

5 years ago

0.0.16

5 years ago

0.0.17

5 years ago

0.0.14

5 years ago

0.0.12

5 years ago

0.0.13

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.5

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago