Skip to content

Solon hot reload beta

Controller

Adding or modifying Controller layer classes can be hot reloaded and Mapping annotations (such as @Mapping, @Get, @Post, etc.) information will be reparsed.

java
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;

@Controller
public class DemoController { 

    @Inject
    private DemoService demoService; 

    @Get
    @Mapping("/demo1") 
    public String demo1() { 
        return demoService.hello3211("solon1"); 
    } 

    @Get
    @Mapping("/demo2") 
    public String demo2() { 
        return "demo2"; 
    @Post
    @Mapping("/demo3") 
    public String demo3() { 
        return "demo3"; 
    }

}
  • The previous /demo2 is no longer accessible, but the newly added /demo3 can be accessed.
  • The newly added /demo1 can be accessed, and the used DemoService will also be injected into DemoController.

Component

Simple AOP implementation, using the @Around annotation to print logs before and after method execution.

java
import org.noear.solon.annotation.Around;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Around(TestAopInterceptor.class)
public @interface TestAop {
}

Concrete implementation class

java
import org.noear.solon.core.aspect.Interceptor;
import org.noear.solon.core.aspect.Invocation;

public class TestAopInterceptor implements Interceptor { 

    @Override 
    public Object doIntercept(Invocation inv) throws Throwable { 
        TestAop anno = inv.getMethodAnnotation(TestAop.class); 
        if (anno == null) { 
            anno = inv.getTargetAnnotation(TestAop.class); 
        } 
        if (anno == null) { 
            return inv.invoke(); 
        } 
        System.out.println("before"); 
        Object invoke = inv.invoke(); 
        System.out.println("after"); 
        return invoke; 
}
}

At this time we overload the DemoService class as follows

java
import io.github.future0923.debug.tools.test.solon.aop.TestAop;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

@Component
public class DemoService { 

    @Inject 
    private UserService userService; 

    @TestAop
    public String hello1(String name) {
        return userService.ab();
    }

    public String hello2(String name) { 
        return userService.ab(); 
    } 
    
    public String hello3(String name) { 
        return userService.ab(); 
    } 
}
  • After overloading, when hello1() is called, the @TestAop annotation will take effect to print before and after logs, and the userService.ab() method will be called.
  • The newly added hello2() method can be called.
  • The deleted hello3() method cannot be found.

Configuration

Added @Bean in @Configuration to create TestConfigBean

java
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;

@Configuration
public class TestConfig { 

    public static class TestConfigBean { 

        public String getName() { 
            return "test"; 
        } 
    } 

    @Bean
    public TestConfigBean testConfigBean() { 
        return new TestConfigBean(); 
    }
}

Inject into DemoService and use

java
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

@Component
public class DemoService { 

    @Inject
    private TestConfig.TestConfigBean testConfigBean; 

    public String hello(String name) {
        return null; 
        return testConfigBean.getName(); 
    }

}
  • The newly added TestConfigBean can be obtained in the container, and DemoService can use testConfigBean.