1 Spring注解
1.1 spring注解使用配置
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.zgd.learn.spring.demo02"/>
1.1 通用注解
1.1.1 @Component
- 类注解,被spring管理,beanId为anno,有下面两种写法
- @Component 不添加参数,默认为类名的首字母小写
- @Component(“anno”),id注入
- @Component(value = “anno”),id注入
1.1.2 @Controller
- 类注解,@Component的衍生注解,@Controller常用于注解Web的控制层,如Servlet
- @Controller 不添加参数,默认为类名的首字母小写
- @Controller(“anno”),id注入
- @Controller(value = “anno”),id注入
1.1.3 @Service
- 类注解,@Component的衍生注解,@Service常用于注解Service层
- @Service 不添加参数,默认为类名的首字母小写
- @Service(“anno”),id注入
- @Service(value = “anno”),id注入
1.1.4 @Repository
- 类注解,@Component的衍生注解,@Repository常用于注解Dao层
- @Repository,不添加参数,默认为类名的首字母小写
- @Repository(“anno”),id注入
- @Repository(value = “anno”),id注入
2.1 属性注解
1.2.1 @Resource
- 属性注解,@Resource用于对属性的注入
- @Resource 无添加参数,则按照类型自动注入
- @Resource(name = “anno”),id注入
- @Resource(type = Anno.class),类型注入
1.2.2 @Autowired
- 属性注解,@Autowired用于对属性的注入
- @Autowired,无参按类型注入
- 常和@Qualifier配合按照id注入
1.2.3 @Qualifier
- 属性注入,@Qualifier不可单独使用,可和@Autowired配合使用
@Qualifier(“anno”),id注入
@Qualifier(value=“anno”),id注入
1.3 spring测试注解
1.3.1 @RunWith(SpringJUnit4ClassRunner.class)
1.3.2 @ContextConfiguration(“classpath:spring.xml”)
- 类注解,表明使用spring.xml配置作为spring的配置文件
- @ContextConfiguration(locations ={ “classpath*:spring/*.xml” }),可使用多个配置文件
1.4 AOP注解
- 范例可参考:https://blog.csdn.net/Student108/article/details/108444179
1.4.1 AOP注解前提
<!-- 开启AOP注解 -->
<aop:aspectj-autoproxy/>
1.4.2 @Aspect切面注解
@Aspect
1.4.3 @Before前置通知注解
@Before(value = "execution(* com.zgd.learn.spring.demo03.OrderDao.save(..))")
1.4.4 @AfterReturing后置通知注解
- 后置通知注解,value为切点表达式,returning为返回值(非必须)
- 方法可以有参数:Object result
@AfterReturning(value = "execution(* com.zgd.learn.spring.demo03.OrderDao.delete(..))", returning = "result")
1.4.5 @Around环绕通知注解
- 环绕通知注解,value为切点表达式
- 方法需要参数:ProceedingJoinPoint joinPoint
- joinPoint.proceed()的前后添加环绕代码
@Around(value = "execution(* com.zgd.learn.spring.demo03.OrderDao.update(..))")
1.4.6 @AfterThrowing异常通知注解
- 异常通知注解,value为切点表达式,throwing为异常对象(非必须)
- 方法可以有参数:Throwable e
@AfterThrowing(value = "execution(* com.zgd.learn.spring.demo03.OrderDao.find(..))", throwing = "e")
1.4.7 @Before前置通知注解
@After(value = "execution(* com.zgd.learn.spring.demo03.OrderDao.find(..))")
1.4.8 @Pointcut切点注解
- 切点注解,不可以有参数,当不想在通知上写表达式可使用
- @Pointcut(value = “execution(* com.zgd.learn.spring.demo03.OrderDao.find(…))”)
@After(value = "AspactAnno.pointcut1()")
public void after()
{
System.out.println("=========最终通知=========");
}
@Pointcut(value = "execution(* com.zgd.learn.spring.demo03.OrderDao.find(..))")
public void pointcut1(){}
1.5 Transaction注解
1.5.1 Transaction注解配置前提
<!-- 配置事务管理器=============================== -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解事务================================ -->
<tx:annotation-driven transaction-manager="transactionManager"/>
1.5.2 @Transactional事务注解
- 类/方法注解,表明该类/方法支持事务
- isolation为事务隔离级别
- propagation 为事务传播方式
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)