Magician-Containers容器管理模块

联合创作 · 2023-09-27 16:08

Magician-Containers 是 Magician 的官方组件,一个容器管理模块,可以对项目中的bean进行统一管理,它带来了两个扩展:AOP 和定时任务。


文档


https://magician-io.com


示例


导入依赖



<!-- This is the jar package build by this project -->
<dependency>
<groupId>com.magician.containers</groupId>
<artifactId>Magician-Containers</artifactId>
<version>1.0.0</version>
</dependency>

<!-- This is Magician -->
<dependency>
<groupId>com.github.yuyenews</groupId>
<artifactId>Magician</artifactId>
<version>2.0.5</version>
</dependency>

<!-- This is the log package, which supports any package that can be bridged with slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.12</version>
</dependency>

标记Bean


不可以用在Controller上



@MagicianBean
public class DemoBean {

}

Aop


编写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) {

}
}

挂到需要监听的方法上



@MagicianBean
public class DemoBean {

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

}
}

定时任务



@MagicianBean
public class DemoBean {

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

}
}

获取Bean对象



@MagicianBean
public class DemoBean {

private DemoBean demoBean;

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

启动时加载资源



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

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

httpServer.bind(8080);

 

浏览 23
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报