温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

@Autowired注入为null问题原因分析

发布时间:2020-10-09 10:39:37 来源:脚本之家 阅读:607 作者:吃鱼不挑刺 栏目:编程语言

问题说明

最近看到Spring事务,在学习过程中遇到一个很苦恼问题

搭建好Spring的启动环境后出现了一点小问题

在启动时候却出现[java.lang.NullPointerException]

不过因为当时一个小小的疏忽很low的问题 请往下看...

工程结构

@Autowired注入为null问题原因分析

代码片段

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

  <!-- Spring注解扫描 -->
  <context:component-scan base-package="com.*" />

  <!-- 1. 数据源对象: C3P0连接池 -->
  <bean id="dataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="org.h3.Driver"></property>
    <property name="jdbcUrl"
      value="jdbc:h3:tcp://192.168.190.1/~/test"></property>
    <property name="user" value="sa"></property>
    <property name="password" value="123"></property>
  </bean>

  <!-- 2. JdbcTemplate工具类实例 -->
  <bean id="jdbcTemplate"
    class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
  </bean>

  <!-- 3.配置事务 -->
  <bean id="dataSourceTransactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
  </bean>

</beans>

Test.java

public class Test {
  public static void main(String[] args) {
    ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(
        "spring.xml");
    ServiceIF service = (ServiceIF) classPathXmlApplicationContext.getBean("serviceImpl");
    service.add("小王", 23);
  }
}

TransactionUtil.java

@Component("transactionUtil")
public class TransactionUtil {

  /**
   * 初始化数据源
   */
  @Autowired
  private DataSourceTransactionManager dataSourceTransactionManager;

  /**
   * 开启事务
   * 
   * @return
   */
  public TransactionStatus begin() {
    TransactionStatus transaction = dataSourceTransactionManager.getTransaction(new DefaultTransactionDefinition());
    System.out.println(" 开启事务成功 ");
    return transaction;
  }

  /**
   * 提交事物
   * 
   * @param transaction
   */
  public void commit(TransactionStatus transaction) {
    dataSourceTransactionManager.commit(transaction);
    System.out.println(" 事物提交成功 ");
  }

  /**
   * 回滚事务
   * 
   * @param transaction
   */
  public void rollback(TransactionStatus transaction) {
    dataSourceTransactionManager.rollback(transaction);
    System.err.println(" 事物进行回滚 ");
  }
}

ServiceImpl.java

@Service("serviceImpl")
public class ServiceImpl implements ServiceIF {

  @Autowired
  TransactionUtil transactionUtil;

  private TransactionStatus transactionStatus = null;

  @Override
  public void add(String name, Integer age) {
    transactionStatus = transactionUtil.begin();
    try {
      new DaoImpl().add(name, age);
      transactionUtil.commit(transactionStatus);
    } catch (Exception e) {
      System.err.println("ERROR >>> 执行出现异常 即将进行回滚操作");
      transactionUtil.rollback(transactionStatus);
    }
  }
}

DaoImpl.java

public class DaoImpl implements DaoIF{

  /**
   * 注入jdbc模板类
   */
  @Autowired
  private JdbcTemplate jdbcTemplate;

  /**
   * 第一条插入语句
   */
  private final String SQL_INSERT_01 = "insert into user values (?,?)";

  /**
   * 添加sql执行
   * 
   * @param name
   * @param age
   */
  public void add(String name, Integer age) {
    jdbcTemplate.update(SQL_INSERT_01, name, age);
  }
}

运行结果

@Autowired注入为null问题原因分析

问题分析

@Autowired注入为null问题原因分析

解决思路

我在想 为什么会没有注入进来呢 我明明加了@Autowired注解

后来猜到可能是Spring.xml配置的问题

看完也没有问题

我就从Java Source一步一步看 发现....

@Autowired注入为null问题原因分析

我靠 我就猜测是不是如果用「new Object()」的方式创建实例后 其class中的Bean的注解会失效呢?

然后我尝试在ServiceImpl.java中以注解的方式把DaoIF的实例注入到ServiceImpl,

并在DaoImpl.java的类上面添加@Repository,

把ServiceImpl.java中new DaoImpl()替换成注入的daoImpl。

改修代码

ServiceImpl.java修改后

@Autowired注入为null问题原因分析

DaoImpl.java修改后

@Autowired注入为null问题原因分析

改修后调试

@Autowired注入为null问题原因分析

@Autowired注入为null问题原因分析

其实我懂得也不太多 Spring注入的流程那

首先他会把项目中target -> classes 目录下的「.class」文件进行解析

通过Spring.xml中的「context:component-scan」进行注解扫描

如果这个路径下的「.class」文件的类上面是否存在@Component声明的注解

如果被此类注解修饰,Spring会把所有被注解修饰的bean进行实例化操作 供给@Autowired进行注入

(在spring注解的源码中@Service和@Repository等等都继承了@Component注解)

结论

在使用Spring的Bean容器时 千万要确保

配置的注解扫描路径正确

Jar的依赖是否存在

是否在bean的上面加「@Service @Repository @Component … 」

要细心 遇到异常不要紧 慢慢分析!!!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI