spring小技巧

1.自动注入配置文件中的属性;

    @Component
    @PropertySource("classpath:ordercreate.properties")

    @ConfigurationProperties(prefix="query")

    @PropertySource:指定读取文件

    @ConfigurationProperties:指定读取特定前缀的值,prefix 设定的就是前缀名称。

                                                例:query.promotion_url=http://xxxxx

2.手动获取springBean

@Component
public class SpringBeanUtil implements ApplicationContextAware {


private static ApplicationContext applicationContext;
@Resource
ConsulDiscoveryProperties consulDiscoveryProperties;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringBeanUtil.applicationContext == null) {
        SpringBeanUtil.applicationContext = applicationContext;
        }
    }


    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }


    //通过name获取 Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }


    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }


    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }

}

3.关于spring在异步线程中无法通过(AopContext.currentProxy()获取当前bean;

    例:ThreadPool.getPool1().execute(new Runnable() {
@Override
public void run() {
((xx)AopContext.currentProxy()).xxx()
}

});

        xx:当前类的名称

    可以通过上面的手动获取bean,拿到其余的bean进行处理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一些 Spring Boot 小技巧: 1. 使用 @Value 注解注入属性值:可以使用 @Value 注解注入属性值到一个变量中,例如:@Value("${my.property}") private String myProperty; 2. 配置多个 profile:可以在 application.yml 或 application.properties 中定义多个 profile,例如:spring.profiles.active=dev, test, prod。 3. 使用 Spring Boot Actuator:Spring Boot Actuator 可以让你监控应用的运行情况,如 health,metrics,info 等。 4. 自定义异常处理:可以使用 @ControllerAdvice 和 @ExceptionHandler 注解来定义自己的异常处理方法,例如: ```java @ControllerAdvice public class MyExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Something went wrong!"); } } ``` 5. 使用 @Scheduled 注解:可以使用 @Scheduled 注解来定时执行方法,例如: ```java @Scheduled(fixedDelay = 5000) public void myTask() { // do something } ``` 6. 使用 Spring Boot DevTools:Spring Boot DevTools 可以让你快速进行应用开发和调试,例如自动重启应用,自动重新加载静态资源等。 7. 使用 Spring Boot Test:Spring Boot Test 可以让你编写单元测试和集成测试,例如: ```java @RunWith(SpringRunner.class) @SpringBootTest public class MyTest { @Autowired private MyService myService; @Test public void testMyService() { assertNotNull(myService); } } ``` 这些技巧可以帮助你更好地使用 Spring Boot,提高开发效率和应用质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值