【Java】Spring注入静态Bean的几种写法

 

单例模式在Spring注解上的一种拓展用法

 

写法一,先配置自身Bean,作为静态成员,然后目标Bean作为自身Bean的实例成员‘

Spring初始化自身Bean时自动装配数据源Bean,从而附属到静态成员上

使用@PostConstruct实现

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放在里面

当然原理是一样,只是通过实例方法或者构造器方法进行实现

方法二,直接在构造器入参装配完成资源分配

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方法上面编写

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();
    }

}

  

 

posted @ 2022-06-10 10:40  emdzz  阅读(1251)  评论(0编辑  收藏  举报