可以看到LunaHangfireRunnerBase类的属性注入

接上一篇整合了一个命令行程序的框架我增加了一点功能

aop和定时任务

这次更新主要增加了审计日志还有集成了hangfire做定时任务

  1. 先来看审计日志部分,审计日志主要是使用了Windsor的动态代理功能.每一个添加了Audited特性的方法或者类,在方法被调用时会输出参数和值.使用时只要在需要审计的service上增加Audited特性即可

    [Audited]
    public class DemoService : LunaServiceBase, IDemoService
    {
        public string GetMessage(string name)
        {
            Logger.Info($"GetMessage {name}");
            return "测试";
        }
    
        public string GetDemo(DemoModel model)
        {
            var msg = $"{model.Name}: {model.Age}";
            return msg;
        }
    }
    

    同时在start上增加了一个可选参数.目前只有一个设置项,就是是否开启审计日志功能.如果不开启的话,标记了aduited的service也不会输出审计日志

  2. 定时任务的用法和之前的还是有一些区别.如要体现在runner上.之前的runner要继承LunaRunnerBase,如果使用hangfire的话要继承LunaHangfireRunnerBase.同时在run方法里增加定时任务
    public class Runner : LunaHangfireRunnerBase
    {
        public override void Run()
        {
            RecurringJob.AddOrUpdate<IJobService>(service => service.OutputLog(), Cron.Minutely);
        }
    }
    

    启动的方法还是和之前一样的使用starter即可.这里有一点需要注意一下.一定要在调用starter的run方法之前配置好hangfire

    GlobalConfiguration.Configuration.UseSqlServerStorage("default");
    

    这里推荐使用topshelf之类的框架把程序搞成服务

代码

和上一版对比的话,这次主要的变更在starter类构造函数中

private Starter(Type runnerType, StarterOption option)
{
    Container = new WindsorContainer();
    Container.Kernel.ComponentRegistered += (key, handler) =>
    {
        if (option.DisableAudit) return;

        if (handler.ComponentModel.Implementation.IsDefined(typeof(AuditedAttribute), true))
        {
            handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(AuditingInterceptor)));
        }
    };

    Container.Register(
        Classes.FromAssemblyInThisApplication(runnerType.Assembly)
            .IncludeNonPublicTypes()
            .BasedOn<ITransientDependency>()
            .WithServiceAllInterfaces()
            .If(type => !type.IsGenericTypeDefinition)
            .WithService.Self()
            .WithService.DefaultInterfaces()
            .LifestyleTransient()
    );

    Container.Register(
        Classes.FromAssemblyInThisApplication(runnerType.Assembly)
            .IncludeNonPublicTypes()
            .BasedOn<ISingletonDependency>()
            .If(type => !type.IsGenericTypeDefinition)
            .WithService.Self()
            .WithService.DefaultInterfaces()
            .LifestyleSingleton()
    );

    Container.Register(
        Classes.FromAssemblyInThisApplication(runnerType.Assembly)
            .IncludeNonPublicTypes()
            .BasedOn<IInterceptor>()
            .If(type => !type.IsGenericTypeDefinition)
            .WithService.Self()
            .LifestyleTransient()
    );

    Container.Register(
        Component.For<Starter>().Instance(this).LifestyleSingleton()
    );
}

新增注册了ComponentRegistered事件.在组件注册ioc完成时检测了组件是否声明了AuditedAttribute特性,如果有声明的话就会给组件增加一个拦截器实现审计日志.

另外就是把starter自己也注册进了ioc,这其实是为了替换hangfire的JobActivator时使用ioc容器.

public abstract class LunaHangfireRunnerBase : LunaRunnerBase
{
    private BackgroundJobServer _backgroundJobServer;
    public Starter Starter { get; set; }
    public override void Init()
    {
        base.Init();
        JobActivator.Current = new WindsorJobActivator(Starter.Container.Kernel);
        _backgroundJobServer = new BackgroundJobServer();
    }

    public override void Stop()
    {
        _backgroundJobServer.Dispose();
    }
}

可以看到LunaHangfireRunnerBase类的属性注入了starter.本来考虑构造注入的,但是使用构造注入的话,继承这个类的runner在代码上就会看起来不是很纯洁,所以放弃了

GitHub: https://github.com/lun3322/Luna.Service

这里有完整的代码和例子,没写单元测试大家凑合看吧.欢迎star

NuGet1: Install-Package Luna.Service

NuGet2: Install-Package Luna.Service.Nlog

NuGet2: Install-Package Luna.Service.Hangfire

原文地址:https://www.cnblogs.com/rtjherty848/p/8609860.html

时间: 2024-08-01 01:27:54

可以看到LunaHangfireRunnerBase类的属性注入的相关文章

Spring boot 工具类静态属性注入及多环境配置

由于需要访问MongoDB,但是本地开发环境不能直接连接MongoDB,需要通过SecureCRT使用127.0.0.2本地IP代理.但是程序部署到线上生产环境后,是可以直接访问MongoDB的,因此开发好程序后,总是要修改一下MongoDB服务器的IP才能提交代码,这样很是不方便. private static final String PUBCHAT_HOST = "127.0.0.2"; // private static final String PUBCHAT_HOST =

spring.net的简单使用(四)对象属性注入

创建了对象,如果是简单对象就到此为止,如果是复杂对象,则需要为它的属性赋值. 属性赋值有两种方法:属性注入和构造器注入. 一.属性注入 在object节点下使用property就是属性注入,如下: <object name="person" type="LINQDemo.person,LINQDemo"> <property name="name" value="ren"></property&g

Spring JavaBean属性值的注入方式( 属性注入, 特殊字符注入 &lt;![CDATA[ 带有特殊字符的值 ]]&gt; , 构造器注入 )

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.or

Spring boot将配置属性注入到bean类中

一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.com - foo.bar.com - jiaobuchong.com 1 2 3 4 5 6 下面我要将上面的配置属性注入到一个Java Bean类中,看码: import org.springframework.boot.context.properties.ConfigurationProper

net core天马行空系列: 一个接口多个实现类,利用mixin技术通过自定义服务名,实现精准属性注入

系列目录 1.net core天马行空系列:原生DI+AOP实现spring boot注解式编程 2.net core天马行空系列: 泛型仓储和声明式事物实现最优雅的crud操作 哈哈哈哈,大家好,我就是高产似母猪的三合.日常开发中,我们常会遇到这样的场景,一个接口,有多个实现类,在某个业务中,我们希望指定某个实现类,如今网络上常见的解决方案,就是注入一个委托或者利用工厂模式,这些方式虽然能实现功能,但使用起来还是不够优雅,如何才称得上优雅呢?自然是在添加服务的时候给服务指定名称,注入的时候根据

spring-boot实战【05】:Spring Boo多环境配置及配置属性注入到对象

项目工程结构: 配置文件application.properties文件 com.yucong.blog.name=yucong com.yucong.blog.title=Spring Boot Course com.yucong.blog.desc=${com.yucong.blog.name} is learing ${com.yucong.blog.title} # 随机字符串 com.yucong.blog.value=${random.value} # 随机int com.yucon

通过struts.xml配置为属性注入值_2015.01.04

01:web.xml配置: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="h

spring学习一——基本搭建,属性注入的两种方式

今天用spring 3.2.5搭建了基本的环境,spring出的太快了,前段时间才3.2.5,今儿个一瞧已经上了4的版本了,稍后给出spring的jar下载地址,毕竟现在官网上找不到了啊. 废话少说了,spring 3.2.5已经将所有的依赖包都放在了dist的lib下面,并且都有doc包和源码包,很是方便.先导入所需的jar包:core,context,beans,expression 四个jar包,除此之外,还需导入commons-logging. 下一步,新建xml文件,建议名称为 app

Spring之属性注入

时间:2017-1-31 23:38 --Bean的属性注入方式 有三种注入方式:    1)接口注入:        定义一个接口,定义setName(String name)方法,定义一个类,实现该接口,并提供private String name. 2)构造器注入:        定义一个类,声明一个private String name,通过构造器this.name = name;进行注入.        在配置文件中使用<constructor-arg>标签. 3)通过set方法注入