例子:
报错:加载不进来 原因:源码中的生成策略如下: 解决: 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(); } }例子:
报错:空指针。 原因:只有交给spring进行管理,她才能自动装配 解决:
在测试:仍报错 空指针 原因:new出来的对象,不是交给spring管理的,不能完成注入解决:通过容器去拿对象
例子3:
。。。。 未写完
例子:
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; } }
自动注入失败,【程序启动】会失败,跑出异常
解决:
原因:安类型找的话,她会找到两个,就会再去按名称去找,但名称也没有ImoocTemplateManagerService,QinyiTemplateManagerService 导致报错,不知道注入哪一个。 解决:
例子:
构造器 注入会存在循环依赖的问题 @Autowired,和setter不存在这个问题
例子:
具体说明:
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 同一个类中, 一个不标注事务的方法去调用标注了事务的方法, 事务会失效