DaoUtil已经加了@Component,但是自动注入还是null
这时候就需要写一个工具类来手动注入(工具借鉴这位大佬)
package jd.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class BeanUtil implements ApplicationContextAware {
protected static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
if (applicationContext == null) {
applicationContext = arg0;
}
}
public static Object getBean(String name) {
//name表示其他要注入的注解name名
return applicationContext.getBean(name);
}
public static T getBean(Class clazz) {
return applicationContext.getBean(clazz);
}
}
注入方式由1变成2



