package com.javasm.aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.sql.Connection;
/**
* @author: zyb
* @className: CurrentThread
* @description:
* @date: 2021/9/20 16:44
* @since: 1.8
*/
@Component
public class CurrentThread {
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
@Autowired
private static Druids ds;
public static void setThreadLocal(Connection conn) {
threadLocal.set(conn);
}
public static Connection getThreadLocal() {
Connection connection = threadLocal.get();
if (connection != null) {
return connection;
}
return ds.getConn();
/*return null;*/
}
public static void removeThreadLocal() {
threadLocal.remove();
}
}
使用@Autowired时 ds对象为空控制台报错
java.lang.NullPointerException
package com.javasm.aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.sql.Connection;
@Component
public class CurrentThread {
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
@Resource
private static Druids ds;
public static void setThreadLocal(Connection conn) {
threadLocal.set(conn);
}
public static Connection getThreadLocal() {
Connection connection = threadLocal.get();
if (connection != null) {
return connection;
}
return ds.getConn();
/*return null;*/
}
public static void removeThreadLocal() {
threadLocal.remove();
}
}
把@Autowired换成@Resource后控制台报下面的错误
Error creating bean with name 'currentThread' defined in file [E:\idea_workSpace\0919_spring\out\production\0919_spring\com\javasm\aspect\CurrentThread.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: @Resource annotation is not supported on static fields
这是什么问题?