开源中国 2018 年度最后一场技术盛会邀你来约~错过就要等明年啦!点此立即预约

ideal(github-harbby) / gadtryJavaApache-2.0

指数
0
Watch 8 Star 14 Fork 4
加入码云
与超过 300 万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
​ Gadtry 是一个构建于java8之上的工具库, 涵盖Ioc Aop exec graph等等工具库. 包含了日常开发中非常多工具类,当然它还在不断丰富中. 展开 收起

https://github.com/harbby/gadtry

ideal 最后提交于 update readme.md
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README.md

Gadtry Build Status

Gadtry A collection of java tool libraries. Contains: ioc. aop. exec. graph ...

Use

  • maven
<dependency>
  <groupId>com.github.harbby</groupId>
  <artifactId>gadtry</artifactId>
  <version>1.2.0</version>
</dependency>

Ioc

Create Factory:

IocFactory iocFactory = IocFactory.create(binder -> {
    binder.bind(Set.class).by(HashSet.class).withSingle();
    binder.bind(HashSet.class).withSingle();
    binder.bind(List.class).byCreator(ArrayList::new);  //Single object
    binder.bind(Object.class, new Object());
    binder.bind(Map.class).byCreator(HashMap::new).withSingle();  //Single object
    binder.bind(TestInject.class);
});

Set a1 = iocFactory.getInstance(Set.class);
Set a2 = iocFactory.getInstance(Set.class);
Assert.assertEquals(true, a1 == a2); // Single object

Class Inject

public class TestInject
{
    @Autowired
    private TestInject test;

    @Autowired
    public TestInject(HashMap set){
        System.out.println(set);
    }
}

Aop

Does not rely on ioc containers:

T proxy = AopFactory.proxy(Class<T>)
    .byInstance(instance)
    .returnType(void.class, Boolean.class)
    //.methodAnnotated(Setter.class)
    .around(proxyContext -> {
            String name = proxyContext.getInfo().getName();
            System.out.println(name);
            Object value = proxyContext.proceed();
            switch (name) {
                case "add":
                    Assert.assertEquals(true, value);  //Set or List
                    break;
                case "size":
                    Assert.assertTrue(value instanceof Integer);
                    break;
            }
    });

Dependent on ioc container:

        IocFactory iocFactory = GadTry.create(binder -> {
            binder.bind(Map.class).byCreator(HashMap::new).withSingle();
            binder.bind(HashSet.class).by(HashSet.class).withSingle();
        }).aop(binder -> {
            binder.bind("point1")
                    .withPackage("com.github.harbby")
                    //.subclassOf(Map.class)
                    //.classAnnotated(Service.class)
                    .classes(HashMap.class, HashSet.class)
                    .whereMethod(methodInfo -> methodInfo.getName().startsWith("add"))
                    .build()
                    .before((info) -> {
                        Assert.assertEquals("add", info.getName());
                        System.out.println("before1");
                    })
                    .after(() -> {
                        Assert.assertTrue(true);
                        System.out.println("after2");
                    });
        }).initialize();

        Set set = iocFactory.getInstance(HashSet.class);

Exec New Jvm

Throw the task to the child process

JVMLauncher<Integer> launcher = JVMLaunchers.<Integer>newJvm()
    .setCallable(() -> {
        // this is child process
        System.out.println("************ runing your task ***************");
        return 1;
    })
    .addUserjars(Collections.emptyList())
    .setXms("16m")
    .setXmx("16m")
    .setConsole((msg) -> System.out.println(msg))
    .build();

VmFuture<Integer> out = launcher.startAndGet();
Assert.assertEquals(out.get().get().intValue(), 1);

Useful mailing lists

  1. yezhixinghai@gmail.com - For discussions about code, design and features

Other

  • 加入QQ群 438625067

项目点评 ( 0 )

你可以在登录后,发表评论

搜索帮助