解决Spring对静态变量无法注入问题(转)

问题
今天在学习的过程中想写一个连接和线程绑定的JDBCUtils工具类,但测试时发现一直报空指针异常,上网查了之后Spring并不支持对静态成员变量注入,所以光试用@Autowired肯定是不行的。可是我们编写工具类时肯定是要使用静态变量和方法的,我总结一下我用过可以实现对静态成员变量注入的方法。

@Component
public class JDBCUtils {
    @Autowired
    private static ComboPooledDataSource dataSource;

    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
    public static Connection getThreadConnection(){
        Connection conn = tl.get();
        if (conn == null){
            conn = getConnection();
            tl.set(conn);
        }
        return conn;

    }
    public static DataSource getDataSource(){
        return dataSource;
    }
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;

    }
    public static void removeThreadConnection(){
        tl.remove();
    }
}

set方法注入

注解方式

在类前加@Component注解,在set方法上加 @Autowired注解,这里注意两点
1.配置文件里已经配置了变量的相关参数
2.静态变量自动生成set方法时会有static修饰,要去掉,否则还是无法注入

@Component
public class JDBCUtils {

    private static ComboPooledDataSource dataSource;
    @Autowired
    public void setDataSource(ComboPooledDataSource dataSource) {
        JDBCUtils.dataSource = dataSource;
    }

xml方式

同样注意将set方法上的static去掉

public class JDBCUtils {
    private static   ComboPooledDataSource dataSource;

    public void setDataSource(ComboPooledDataSource dataSource) {
        this.dataSource = dataSource;
    }


    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
    public static Connection getThreadConnection(){
        Connection conn = tl.get();
        if (conn == null){
            conn = getConnection();
            tl.set(conn);
        }
        return conn;

    }
    public static DataSource getDataSource(){
        return dataSource;
    }
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;

    }
    public static void removeThreadConnection(){
        tl.remove();
    }
}
   <bean id="JDBCUtils" class="com.cc.utils.JDBCUtils">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

@PostConstruct注解方式注入
用@PostConstruct加在init方法上,在类初始化后执行该方法,对成员变量赋值。在这之前,我们要改造一下工具类,去掉我们想注入变量的static的修饰符,这样我们就可以用@Autowired实现对其注入。然后加一个静态的类自身的引用对象,当我们想要变量时通过这个引用对象来获取。

@Component
public class JDBCUtils {
    @Autowired
    private  ComboPooledDataSource dataSource;
    private static JDBCUtils jdbcUtils;
    @PostConstruct
    public void init(){
        jdbcUtils = this;
        this.dataSource = dataSource;
    }

    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
    public static Connection getThreadConnection(){
        Connection conn = tl.get();
        if (conn == null){
            conn = getConnection();
            tl.set(conn);
        }
        return conn;

    }
    public static DataSource getDataSource(){
        return jdbcUtils.dataSource;
    }
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = jdbcUtils.dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;

    }
    public static void removeThreadConnection(){
        tl.remove();
    }
}

当然这种用初始化方法也可以用xml配置,原理一样。

public class JDBCUtils {
    private  ComboPooledDataSource dataSource;

    public void setDataSource(ComboPooledDataSource dataSource) {
        this.dataSource = dataSource;
    }

    private static JDBCUtils jdbcUtils;
    public void init(){
        jdbcUtils = this;
        this.dataSource = dataSource;
    }

    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
    public static Connection getThreadConnection(){
        Connection conn = tl.get();
        if (conn == null){
            conn = getConnection();
            tl.set(conn);
        }
        return conn;

    }
    public static DataSource getDataSource(){
        return jdbcUtils.dataSource;
    }
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = jdbcUtils.dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;

    }
    public static void removeThreadConnection(){
        tl.remove();
    }
}
<bean id="JDBCUtils" class="com.cc.utils.JDBCUtils" init-method="init">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

参考链接:https://blog.csdn.net/chen1403876161/article/details/53644024

————————————————
版权声明:本文为CSDN博主「qq_42524262」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42524262/article/details/97898796

posted on 2019-11-20 13:49  木西-Muxy  阅读(2207)  评论(0编辑  收藏  举报

导航