Java实操可能出现的错误3 ---spring

    科技2025-08-01  9

    文章目录

    1spring bean 的默认名称生成策略2使用啦@Autowired注解,但是仍然出现啦空指针3不使用自动注入,你还会获取上下文吗?4你是不是经常存在多个可用bean异常4.1bean注入常见的两类异常4.1.1定义啦接口,但没有实现4.1.2 定义啦接口的多个实现类 5 spring出现啦循环依赖,怎么办?6

    1spring bean 的默认名称生成策略

    例子:

    报错:加载不进来 原因:源码中的生成策略如下: 解决: 1 尽量避免第一个字母和第二个字母是大写的情况 2 指定名称

    @Service("qYImooc") public class QYImooc { public void print() { System.out.println("QYImooc"); } }

    3通过类型去获取对象

    @SpringBootTest @RunWith(SpringRunner.class) public class TestQYImooc { @Test public void testDefaultBeanName() { QYImooc qyImooc = (QYImooc) ApplicationUtils.getBean("QYImooc"); QYImooc qyImooc_ = ApplicationUtils.getBean(QYImooc.class); qyImooc.print(); } }

    2使用啦@Autowired注解,但是仍然出现啦空指针

    例子:

    报错:空指针。 原因:只有交给spring进行管理,她才能自动装配 解决:

    在测试:仍报错 空指针 原因:new出来的对象,不是交给spring管理的,不能完成注入

    解决:通过容器去拿对象

    例子3:

    3不使用自动注入,你还会获取上下文吗?

    。。。。 未写完

    4你是不是经常存在多个可用bean异常

    例子:

    package com.imooc.spring.escape.multi_usable_bean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * <h1>配置多个 Redis 数据源</h1> * */ @SuppressWarnings("all") @Configuration public class RedisConfig { private final RedisConnectionFactory redisConnectionFactory; @Autowired public RedisConfig(RedisConnectionFactory redisConnectionFactory) { this.redisConnectionFactory = redisConnectionFactory; } @Primary @Bean(name = "qinyiRedisTemplate") public RedisTemplate<String, Object> getQinyiRedisTemplate( RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); RedisSerializer<String> stringSerializer = new StringRedisSerializer(); template.setConnectionFactory(factory); template.setKeySerializer(stringSerializer); template.setValueSerializer(stringSerializer); return template; } @Bean(name = "imoocRedisTemplate") public RedisTemplate<String, Object> getImoocRedisTemplate( RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); JdkSerializationRedisSerializer redisSerializer = new JdkSerializationRedisSerializer(); RedisSerializer<String> stringSerializer = new StringRedisSerializer(); template.setConnectionFactory(factory); template.setKeySerializer(stringSerializer); template.setValueSerializer(redisSerializer); return template; } }

    4.1bean注入常见的两类异常

    4.1.1定义啦接口,但没有实现

    自动注入失败,【程序启动】会失败,跑出异常

    解决:

    4.1.2 定义啦接口的多个实现类

    原因:安类型找的话,她会找到两个,就会再去按名称去找,但名称也没有ImoocTemplateManagerService,QinyiTemplateManagerService 导致报错,不知道注入哪一个。 解决:

    5 spring出现啦循环依赖,怎么办?

    例子:

    构造器 注入会存在循环依赖的问题 @Autowired,和setter不存在这个问题

    6

    例子:

    具体说明:

    1 主动捕获了异常, 导致事务不能回滚

    解决:事务进行啦回滚,数据库表中没有添加数据

    @Override @Transactional public void CatchExceptionCanNotRollback() { try { idAndNameDao.save(new IdAndName("qinyi")); throw new RuntimeException(); } catch (Exception ex) { ex.printStackTrace(); // 手动标记回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } }

    2 不是 unchecked 异常, 事务不能回滚

    3 unchecked 异常可以回滚

    非检查性异常,不用抛出异常或者try catch

    @Override @Transactional public void RuntimeExceptionCanRollback() { idAndNameDao.save(new IdAndName("qinyi")); throw new RuntimeException(); }

    eg: 登录接口中有添加任务的操作:先查询,后添加 当查询失败,都不会走到添加的地方 当添加失败,查询不要回滚 所以这个接口不需要事务注解

    4 指定 rollbackFor , 事务可以回滚

    当检查性异常,指定 rollbackFor , 事务可以回滚

    5 同一个类中, 一个不标注事务的方法去调用标注了事务的方法, 事务会失效

    Processed: 0.025, SQL: 9