springboot中有用的几个有用aware以及bean操作和数据源操作

本文参考了:

https://blog.csdn.net/derrantcm/article/details/76652951

https://blog.csdn.net/derrantcm/article/details/73456550

通过以上可以获得springboot的许多知识。

本文只是列出本人常用的两个aware.

闲话少叙,直接上代码

BeanFactoryAware  帮助获取各种bean

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;

@Component
public class BeanHelper implements BeanFactoryAware {
    
    private static BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    public static <T>T getBean(String id,Class<T> type){        
        return  beanFactory.getBean(id,type);                
    }
    
    
    public static <T>T getBean(Class<T> type){        
        return  beanFactory.getBean(type);        
        
    }
    
    public static <T>T getBean(String beanName){        
        return  (T) beanFactory.getBean(beanName);        
    }
    
}

 

ApplicationContextAware 帮助获取上线文的信息,也可以操作bean

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class DsControl implements ApplicationContextAware {
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//dataSource在springboot中是一个关键字 Object ds
=applicationContext.getBean("dataSource") ; System.out.println("当前的连接池是:"+ds.getClass().getName()); System.out.println("-----gooooo"); String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); for (String beanDefinitionName : beanDefinitionNames) { System.out.println(beanDefinitionName); } } }

 

转载于:https://www.cnblogs.com/lzfhope/p/9821749.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 3(假设这是一个未来的版本,目前Spring Boot的最新稳定版是2.x)的动态数据功能允许你在运行时切换或配置不同的数据库连接,这对于需要处理多个数据或者根据业务需求动态调整数据访问策略的应用非常有用。以下是一些关键点: 1. **DataSource bean**:在Spring Boot,你可以为每个数据创建一个`DataSource` bean,并通过`spring.datasource`的配置属性来配置它们,如URL、用户名和密码等。 2. **@ConfigurationProperties**:Spring Boot提供了`@ConfigurationProperties`注解来自动绑定外部配置文件的属性,这样你就可以轻松地从配置读取不同数据的配置信息。 3. **Profile-aware**:使用Spring Profile,你可以根据应用的运行环境(如开发、测试、生产)切换数据。例如,你可以有一个`dev-datasource.properties`和`prod-datasource.properties`,然后在启动应用时指定哪个配置文件生效。 4. **JdbcTemplate或JPA**:Spring Boot提供了`JdbcTemplate`和`JPA`库来与数据交互。在支持动态数据的情况下,你需要确保每次切换数据时,相关的`JdbcTemplate`或`EntityManagerFactory`也会被正确设置。 5. **AOP(面向切面编程)**:有时,你可能想要在切换数据的同时执行一些额外的操作,比如日志记录或者性能统计。这时可以使用AOP来编写通知,在数据切换时自动执行。 相关问题-- 1. 如何在Spring Boot 3启用动态数据? 2. 动态数据在高并发场景下有哪些优势? 3. Spring Boot 3如何管理多个数据的事务协调?

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值