Magician-Containers 1.0.0 is released, adding a new member to the Magician family

In the process of using Magician to develop projects recently, I found some problems. Sometimes I want to monitor certain methods, or start a timed task for polling, which is a little more troublesome, because Magician has no corresponding solution for the time being, but I don't want to add functions to the existing Magician, because this will make the project bigger and bigger, and eventually become a big fat man, losing flexibility. When developers use it, no matter what functions are needed or not Being introduced into the project in one go is unacceptable to me. And it also went against the original intention of Magician, so in the end we decided to release it in the form of a new composition. Give the user the right to use or not.

What Magician-Containers bring

  1. Bean management (no IOC, just to bind functions on beans and solve some troubles)
  2. AOP
  3. timed task

Bean management

Just add an annotation to the class. It cannot be used on the controller, and not all classes need to be turned into a bean. The developer can decide at will.

We recommend turning it into a bean only when you need to use AOP or scheduled tasks in this class.

@MagicianBean
public class DemoBean {

}

AOP

Write the logic of AOP

public class DemoAop implements BaseAop {

    /**
     * 方法执行前
     * @param args 方法的参数
     */
    public void startMethod(Object[] args) {

    }

    /**
     * 方法执行后
     * @param args 方法的参数
     * @param result 方法的返回数据
     */
    public void endMethod(Object[] args, Object result) {

    }

    /**
     * 方法出异常后
     * @param e 方法的异常信息
     */
    public void exp(Throwable e) {

    }
}

Hang on to the method that needs to be monitored

@MagicianBean
public class DemoBean {

    @MagicianAop(className = DemoAop.class)
    public void demoAopMethod() {

    }
}

timed task

Currently only polling at intervals is supported, and cron expressions are not yet supported

@MagicianBean
public class DemoBean {

    // loop: 轮询频率,单位:毫秒
    @MagicianTimer(loop=1000)
    public void demoTimerMethod() {

    }
}

get bean object

You cannot assign values ​​directly when defining member variables. The following example is a writing method we recommend. For details, please refer to the documentation on the official website.

@MagicianBean
public class DemoBean {

    private DemoBean demoBean;

    public void demoMethod() {
        demoBean = BeanUtil.get(DemoBean.class);
    }
}

Load resources at startup

HttpServer httpServer = Magician
        .createHttp()
        .scan("com.test"); // Scanning range (package name)

// 在scan方法执行后,才可以加载bean,顺序一定要注意
MagicianContainers.load();

httpServer.bind(8080);

Visit the official website to learn more: https://magician-io.com

Guess you like

Origin www.oschina.net/news/202758/magician-containers-1-0-0-released