Skip to main content

A python-made framework which is like Spring in Java.

Project description

pythonic-spring

用python实现spring特性, 已集成fastapi, 可以像springboot那样统一管理类的生成和调用, 相比较原先python通过在类声明外面整实例的情况, 防止了写代码时循环调用的风险, 也可以为java中比较常见的多态和继承在python中的实战化做基底

当前最新版本 0.0.1.3:

两个装饰器:

  • Service: 装饰class, 根据spring.json自动生成bean, 并通过BeanFactory注册
  • Autowired: 装饰__init__方法, 根据bean_modules和bean_ids自动注入注册的bean到类中

SpringApplication:构建即使用, 支持输入通配扫描代码并延迟import, 提供了bean_id和type两种找bean的方式

spring.json:扫描并反序列, 供BeanFactory读取

使用方法

安装pip包

pip install pythonic-spring

创建实例

from pythonicspring import SpringApplication
app = SpringApplication()

声明需要bean化的类, 使用类装饰器:Service

声明类内需要引用的bean, 在__init__方法上使用装饰器:Autowired

from pythonicspring import Service
from pythonicspring import Autowired
@Service
class ClassA:

    class_b: ClassB = None
    class_c: ClassC = None
    
    @Autowired(bean_modules={"class_b": ClassB}, bean_ids={"class_c": "ClassCCC"})
    def __init__(self):
        pass
        
    def foo(self):
        result = class_b.spam()
        pass

一个例子:

test/testApp.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pythonic-spring-0.0.1.4.tar.gz (8.4 kB view hashes)

Uploaded Source

Built Distribution

pythonic_spring-0.0.1.4-py3-none-any.whl (12.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page