Introduce EzyFox Bean

Updated at 1699803762000

EzyFox Bean Introduction

EzyFox Bean is a library for bean managment and dependency injection. It can manage the both singleton objects and prototype objects.

1. Structure of EzyFox Bean

  1. EzyBeanContext: a composite object, manage all properties, singletons and prototypes
  2. Properties: EzyBeanContext will keep all properties and inject to singletons and prototypes
  3. EzySingletonFactory: manage all singletons
  4. EzyPrototypeFactory: manage all prototypes

EzyFox Bean also has many annotations and you can find out theme here.

2. Install EzyFox Bean

You can add to your dependency like this:


The latest version can be found in the Maven Central repository.

3. Properties

Currently, EzyFox Bean support .properties and yaml file. By default, it will read files in your classpath:

  2. application.yaml
  3. application.yml

Let's say you have a like this:



You can create a data class and EzyFox Bean will bind the properties to it automatically. Example:

    @EzyPropertiesBean(prefix = "book")
    public class BookSetting {
        private String namePattern;
        private long minPrice;

4. Singleton management

Bean initialization

EzyFox Bean will scan which packages that you provide and by default it will scan com.tvd12.ezyfox.boot package. It find which classes are annotated with @EzySingleton, @EzyConfigurationBefore, @EzyConfiguration, @EzyConfigurationAfter to create singleton objects. Example, you have BookRepository class like bellow, EzyFox Bean will scan and create singleton for it.

    public class BookRepository {
        public void save(Book book) {
            System.out.println("saved book: " + book);

Bean binding

EzyFox Bean support binding vi constructor and @EzyAutoBind annotation, but we recommend you use constructor binding because you will don't need use setter method and make your class is immutable. Example:

    public class BookService {
        private final BookSetting bookSetting;
        private final BookRepository bookRepository;
        public void saveBook(Book book) {
            if (!book.getName().matches(bookSetting.getNamePattern())) {
                throw new IllegalArgumentException("invalid book name");

5. Prototype management

EzyFox Bean will create a prototype supplier for a class that's annotated by @EzyPrototype annotation, example:

    public class BookPriceCalculator {
        private Book book;
        private int discount;
        public long calculate() {
            return book.getPrice() - discount;

4. Example

Now combine properties, singletons and prototypes we can create a EzyBeanContext and get bean (singleton and prototype) to use:

    public final class EzyFoxBeanExample {
        public static void main(String[] args) {
            final EzyBeanContext beanContext = EzyBeanContext.builder()
            final BookController bookController =
            final Book book = new Book(1L, "EzyFox in action", 2000);
            final BookPriceCalculator bookPriceCalculator =
            final long bookPrice = bookPriceCalculator.calculate();
            System.out.println("book price: " + bookPrice);