Wiki

Clone wiki

fxq-commons / Core Annotations and IOC

Description

fxq-core for python is a declarative based IOC framework for helping to build large applications in python.

It facilitates dependency injection through decorators more commonly found in Spring

Getting Started

Create a class i.e. ComponentExample and decorate it with @Component.

This will trigger an instance of this component to be registered with the IOC container and it can be pulled out with the decorator method Autowired

Field injection is not supported with a decorator but can be done by calling the function Autowired

Constructor injection is supported

import logging

format = "[%(asctime)s] [%(levelname)s] [%(name)s] - %(message)s"
logging.basicConfig(format=format, level=logging.DEBUG)

from fxq.core.beans.factory.annotation import Autowired
from fxq.core.stereotype import Component


@Component
class ComponentExample:
    def __init__(self):
        self.id = "1"

    def __repr__(self):
        return "ComponentExample instance %s - ID:%s" % (hash(self), self.id)


if __name__ == '__main__':
    component_example = Autowired(type=ComponentExample)
    same_component = Autowired(name="component_example")
    same_component_again = Autowired(name="component_example", type=ComponentExample)
    logging.info(component_example)
    logging.info(same_component)
    logging.info(same_component_again)
Produces
[2019-05-19 19:17:07,789] [INFO] [BeanFactory] - Bean Factory Initialized
[2019-05-19 19:17:07,789] [INFO] [ApplicationContext] - Application Context Initialized
[2019-05-19 19:17:07,791] [DEBUG] [BeanFactory] - Registering Bean for type ComponentExample with name component_example
[2019-05-19 19:17:07,791] [DEBUG] [BeanFactory] - Matched component_example to be of class type ComponentExample
[2019-05-19 19:17:07,792] [DEBUG] [BeanFactory] - Found instance of type ComponentExample for requested bean component_example
[2019-05-19 19:17:07,792] [DEBUG] [BeanFactory] - Validated Bean component_example to be of type ComponentExample
[2019-05-19 19:17:07,792] [INFO] [root] - ComponentExample instance -9223372036577000402 - ID:1
[2019-05-19 19:17:07,792] [INFO] [root] - ComponentExample instance -9223372036577000402 - ID:1
[2019-05-19 19:17:07,792] [INFO] [root] - ComponentExample instance -9223372036577000402 - ID:1

Updated