【Java】Spring注入静态Bean的几种写法
单例模式在Spring注解上的一种拓展用法
写法一,先配置自身Bean,作为静态成员,然后目标Bean作为自身Bean的实例成员‘
Spring初始化自身Bean时自动装配数据源Bean,从而附属到静态成员上
使用@PostConstruct实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package cn.ymcd.aisw.jobService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.sql.DataSource; import java.sql.*; /** * @Description Job统一sql工具类 * @Author jianglun * @Date 2022/5/26 0026 10:50 * @Version 1.0 */ @Component public class JDBCUtils { @Autowired private DataSource dataSource; public static JDBCUtils jdbcUtils; @PostConstruct private void init(){ jdbcUtils = this ; jdbcUtils.dataSource = this .dataSource; } // 定期过期商家 public static void invalidMerchant(){ Connection connection = null ; PreparedStatement statement = null ; try { connection = jdbcUtils.dataSource.getConnection(); statement = connection.prepareStatement( "update aisw_merchant set STATUS = 0, MERCHANT_STATUS = 2 where MERCHANT_VALID_DATE < ?" ); statement.setDate( 1 , new Date(System.currentTimeMillis())); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭连接 try { if (statement != null ){ statement.close(); } if (connection != null ){ connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } |
我觉得这样要多浪费一个自身Bean放在里面
当然原理是一样,只是通过实例方法或者构造器方法进行实现
方法二,直接在构造器入参装配完成资源分配
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package cn.ymcd.aisw.common; import cn.ymcd.wss.util.log.YmcdLogger; import lombok.SneakyThrows; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.sql.Connection; /** * @Description Job统一sql工具类 * @Author jianglun * @Date 2022/5/26 0026 10:50 * @Version 1.0 */ @Component public class JdbcUtil { private static final YmcdLogger LOGGER = new YmcdLogger(JdbcUtil. class ); private static DataSource dataSource; @Autowired private JdbcUtil(DataSource dataSource) { JdbcUtil.dataSource = dataSource; } @SneakyThrows public static Connection getConnection() { LOGGER.info( "Static Variable Test ... {}" , JdbcUtil.dataSource.toString()); return JdbcUtil.dataSource.getConnection(); } } |
也可以在Set方法上面编写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package cn.ymcd.aisw.common; import cn.ymcd.wss.util.log.YmcdLogger; import lombok.SneakyThrows; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.sql.Connection; /** * @Description Job统一sql工具类 * @Author jianglun * @Date 2022/5/26 0026 10:50 * @Version 1.0 */ @Component public class JdbcUtil { private static final YmcdLogger LOGGER = new YmcdLogger(JdbcUtil. class ); private static DataSource dataSource; @Autowired private void setDataSource(DataSource dataSource) { JdbcUtil.dataSource = dataSource; } @SneakyThrows public static Connection getConnection() { LOGGER.info( "Static Variable Test ... {}" , JdbcUtil.dataSource.toString()); return JdbcUtil.dataSource.getConnection(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
2020-06-10 【JavaWeb】24 使用反射让Servlet实现Controller功能