English 中文(简体)
春天:使用不同的服务,取决于该方法是否由时间表启动。
原标题:Spring: Use different service depending on if the method is triggered from a schedule or not

1。 我有“春布申请”和“界面”,实施一些缺省方法,并界定一种方法<代码>getGraphClient(:

interface GraphService {
    fun getGraphClient(): GraphServiceClient<Request>

    fun getAllUsers(): List<User> {
        // 。。。 using getGraphClient()
    }

    // 。。。
}

有两个服务,一个是“级联”栏,一个是“级联”栏,另一个是“级应用服务”栏。

@Service
class GraphUserService() {

    override fun getGraphClient(): GraphServiceClient<Request> {
        // build graph client for user requests
    }

}

@Service
class GraphApplicationService() {

    override fun getGraphClient(): GraphServiceClient<Request> {
        // build graph client for application requests
    }

}

现在,我有一个方法<代码>refreshUsers(>,载于Adminservice,通过一个时间表自动和定期启动:

@Service
class ScheduleService(
    private val adminService: AdminService
) {

    @Scheduled(cron = "@monthly")
    fun executeMonthlySchedule() {
        adminService。refreshUsers()
    }

}

Additionally, the method refreshUsers() can be triggered proactive by an user。

My aim: refreshUsers() should use the getAllUsers() method from the GraphUserService if its triggered by an user and the getAllUsers() method from the GraphApplicationService if its triggered by the automatic schedule。

How can I reach this? I thought of conditional beans but didn t come to a result until now。

Thanks in advance。

问题回答

There are two important issues to solve. The language I used is Java, but I don t think Kotlin will be any different.

  • 采用同一接口的豆类动态依赖注射

    • This problem can be solved by using dynamic dependency injection using Map.
  • www.un.org/Depts/DGACM/index_spanish.htm 如何区分用户要求和时间表?

    • A simple way that comes to mind is to plant a Flag in the user request, but @Scheduled annotated methods cannot have parameters.
  • 然后?

    • Wouldn’t it be possible to distinguish it by importing the path metadata that called the method?
    • If it is a request from the user, we will usually call a submethod in the Controller class.
    • For scheduling, no method is called in the Controller.
  • 我的解决办法是使用斯特克特雷斯。

  • 核查方法

    • Implemented a method to distinguish between methods of beans that implement the same interface.
    • In some cases, dependency injection will be done dynamically, so if a different output appears, verification is successful.
    @Service
    @RequiredArgsConstructor
    public class ScheduleService {
    
        private final AdminService adminService;
    
        @Scheduled(cron = "0/1 * * * * ?")
        public void executeMonthlySchedule() {
            adminService.refreshUsers();
        }
    }
    
    @Service
    public class GraphUserService implements GraphService {
    
        @Override
        public void getAllUsers() {
            System.out.println("GraphUserService.getAllUsers");
        }
    }
    
    @Service
    public class GraphApplicationService implements GraphService {
        @Override
        public void getAllUsers() {
            System.out.println("GraphApplicationService.getAllUsers");
        }
    }
    
    @Slf4j
    @Service
    @RequiredArgsConstructor
    public class AdminService {
    
        private final Map<String, GraphService> graphServiceMap;
    
        public void refreshUsers() {
            StackTraceElement[] ste = new Throwable().getStackTrace();
            String methodCallClassName = ste[2].getClassName();
            log.info("methodCallClassName={}", methodCallClassName);
            //if method call class name contain upper class name -> DI graphUserService
            GraphService graphService;
            if (methodCallClassName.contains("Controller")) {
                graphService = graphServiceMap.get("graphUserService");
            } else {
                graphService = graphServiceMap.get("graphApplicationService");
            }
            graphService.getAllUsers();
        }
    }
    
    /*
    methodCallClassName=example.controller.AdminController
    GraphUserService.getAllUsers
    */
    
    /*
    methodCallClassName=jdk.internal.reflect.DirectMethodHandleAccessor
    GraphApplicationService.getAllUsers
    */
    
  • 通过使用地图解决了依赖物的动态注入问题。

    • There is also a way to use annotations, but I prefer this method because it is quite intuitive.
  • 检查StackTraceElement的内容。

    • I was able to check the path of the parent class that was the caller in ste[2].
    • Depending on the coding, the index of this part may be at 0 or 1.
    • There is a risk in using magic numbers, but the problem can be solved.
  • 检查了AdminController的道路,以核实它用的是图表。 在这种情况下,用户服务。

  • @Sbed电话也按预期运作。

可能有更好的想法,但我希望我的想法是有益的。





相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签