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

网友投稿 467 2023-01-18


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

问题说明

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

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

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

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

工程结构

代码片段

spring.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

class="com.mchange.v2.c3p0.ComboPooledDataSource">

value="jdbc:h2:tcp://192.168.190.1/~/test">http://

class="org.springframework.jdbc.core.JdbcTemplate">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

class="com.mchange.v2.c3p0.ComboPooledDataSource">

value="jdbc:h2:tcp://192.168.190.1/~/test">http://

class="com.mchange.v2.c3p0.ComboPooledDataSource">

value="jdbc:h2:tcp://192.168.190.1/~/test">http://

value="jdbc:h2:tcp://192.168.190.1/~/test">http://

class="org.springframework.jdbc.core.JdbcTemplate">

class="org.springframework.jdbc.core.JdbcTemplate">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

Test.java

public class Test {

public static void main(http://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 Ycvedbegin() {

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注解

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

看完也没有问题

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

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

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

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

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

改修代码

ServiceImpl.java修改后

DaoImpl.java修改后

改修后调试

其实我懂得也不太多 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 … 」

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java实现百度云文字识别接口代码
下一篇:详解SpringBoot开发使用@ImportResource注解影响拦截器
相关文章

 发表评论

暂时没有评论,来抢沙发吧~