一、Spring中的注解大概可以分为两大类:
1)spring的bean容器相关的注解,或者说bean工厂相关的注解;
2)springmvc相关的注解。
spring的bean容器相关的注解,先后有:@Required, @Autowired, @PostConstruct, @PreDestory,还有Spring3.0开始支持的JSR-330标准javax.inject.*中的注解(@Inject, @Named, @Qualifier, @Provider, @Scope, @Singleton).
springmvc相关的注解有:@Controller, @RequestMapping, @RequestParam, @ResponseBody等等。
二、Spring使用的注解大全和解释。
- @Controller 组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类,然后将web请求映射到注解了@RequestMapping的方法上。
- @Service 组合注解(组合了@Component注解),应用在service层(业务逻辑层)
- @Reponsitory 组合注解(组合了@Component注解),应用在dao层(数据访问层)
- @Component 表示一个带注释的类是一个“组件”,成为Spring管理的Bean。当使用基于注解的配置和类路径扫描时,这些类被视为自动检测的候选对象。同时@Component还是一个元注解。
- @Autowired Spring提供的工具(由Spring的依赖注入工具(BeanPostProcessor、BeanFactoryPostProcessor)自动注入。)
- @Resource JSR-250提供的注解
- @Configuration 声明当前类是一个配置类(相当于一个Spring配置的xml文件)
- @ComponentScan 自动扫描指定包下所有使用@Service,@Component,@Controller,@Repository的类并注册
- @Bean 注解在方法上,声明当前方法的返回值为一个Bean。返回的Bean对应的类中可以定义init()方法和destroy()方法,然后在@Bean(initMethod=”init”,destroyMethod=”destroy”)定义,在构造之后执行init,在销毁之前执行destroy。
- @Aspect 声明一个切面(就是说这是一个额外功能)
- @After 后置建言(advice),在原方法前执行。
- @Before 前置建言(advice),在原方法后执行。
- @Around 环绕建言(advice),在原方法执行前执行,在原方法执行后再执行(@Around可以实现其他两种advice)
- @Pointcut 声明切点,即定义拦截规则,确定有哪些方法会被切入
- @Transactional 声明事务(一般默认配置即可满足要求,当然也可以自定义)
- @Cacheable 声明数据缓存
- @EnableAspectJAutoProxy 开启Spring对AspectJ的支持
- @Value 值得注入。经常与Sping EL表达式语言一起使用,注入普通字符,系统属性,表达式运算结果,其他Bean的属性,文件内容,网址请求内容,配置文件属性值等等
- @PropertySource 指定文件地址。提供了一种方便的、声明性的机制,用于向Spring的环境添加PropertySource。与@configuration类一起使用。
- @Profile 表示当一个或多个指定的文件是活动的时,一个组件是有资格注册的。使用@Profile注解类或者方法,达到在不同情况下选择实例化不同的Bean。@Profile(“dev”)表示为dev时实例化。
- @RunWith 这个是Junit的注解,springboot集成了junit。一般在测试类里使用:@RunWith(SpringJUnit4ClassRunner.class) — SpringJUnit4ClassRunner在JUnit环境下提供Sprng TestContext Framework的功能
- @ContextConfiguration 用来加载配置ApplicationContext,其中classes属性用来加载配置类:@ContextConfiguration(classes = {TestConfig.class(自定义的一个配置类)})
- @WebAppConfiguration 一般用在测试上,注解在类上,用来声明加载的ApplicationContext是一个WebApplicationContext。他的属性指定的是Web资源的位置,默认为src/main/webapp,我们可以修改为:@WebAppConfiguration(“src/main/resources”)。
- @ImportResource 虽然Spring提倡零配置,但是还是提供了对xml文件的支持,这个注解就是用来加载xml配置的。例:@ImportResource({“classpath
三、创建工程
1、分包
-
man/java 目录下创建 :
com.itlaobing.aop.annotation.entity包
com.itlaobing.aop.annotation.dao 包
com.itlaobing.aop.annotation.service包
com.itlaobing.aop.annotation.controller包
com.itlaobing.aop.annotation.aspect包 -
man/resources/ 目录下创建:
com/itlaobing/aop/annotation包 -
test/java/ 目录下创建 com/itlaobing/aop/annotation包
2、【创建 Student、StudentDao、StudentService、StudentController、ServiceAspect】
1) Student
package com.itlaobing.aop.annotation.domain;
public class Student {
private Integer id; // object identifier filed
private String name;// student name
public Integer getId() {
return id;
}
public void setId( Integer id ) {
this.id = id;
}
public String getName() {
return name;
}
public void setName( String name ) {
this.name = name;
}
}
2 ) StudentDao
package com.itlaobing.aop.annotation.dao;
import com.itlaobing.aop.annotation.domain.Student;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
// <bean id="studentDao" class="com.itlaobing.aop.annotation.dao.StudentDao" scope="singleton" />
@Repository // 容器 、仓库 (注解是告诉Spring,让Spring创建一个名字叫“StudentDao”的StudentDaoImpl实例)
@Scope("singleton")
public class StudentDao {
public void persist( Student s ){
System.out.println("保存 { id : " + s.getId() + ", name : " + s.getName() + " }" );
}
}
3 ) StudentService
package com.itlaobing.aop.annotation.service;
import com.itlaobing.aop.annotation.dao.StudentDao;
import com.itlaobing.aop.annotation.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
// <bean id="studentService" class="com.itlaobing.aop.annotation.service.StudentService" scope="singleton" />
@Service("studentService")
public class StudentService {
@Autowired
private StudentDao studentDao ;
public void save( Student s){
System.out.println("[StudentService ] - [ save(student) ]");
studentDao.persist(s);
}
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao( StudentDao studentDao ) {
this.studentDao = studentDao;
}
}
4 ) StudentController
package com.itlaobing.aop.annotation.controller;
import com.itlaobing.aop.annotation.domain.Student;
import com.itlaobing.aop.annotation.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
// <bean id="studentController" class="com.itlaobing.aop.annotation.controller.StudentController" scope="singleton" />
@Controller //控制器
public class StudentController {
private StudentService studentService ;
public void signUp( Student s){
studentService.save(s);
}
public StudentService getStudentService() {
return studentService;
}
@Autowired
@Qualifier("studentService")//使用@Autowired自动注入,使用@Qualifier("speakNihao")确定注入的是具体的是什么,
public void setStudentService( StudentService studentService ) {
System.out.println( "[ StudentController ] - [ setStudentService ]" );
this.studentService = studentService;
}
}
5 ) ServiceAspect
package com.itlaobing.aop.annotation.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component//@Component是一个元注解,意思是可以注解其他类注解,
@Aspect // @Aspect:作用是把当前类标识为一个切面供容器读取
public class ServiceAspect {
@Pointcut( "execution(* com..service.*.save*(..))" )
private void servicePointcut(){
}
@Before( "servicePointcut()" )
public void before( JoinPoint joinPoint ) {
System.out.println( "【 " + joinPoint.getSignature().getName() + " 】方法即将执行" );
}
@Around( "servicePointcut()" )
public Object around( ProceedingJoinPoint proceedingJoinPoint ) throws Throwable {
String name = proceedingJoinPoint.getSignature().getName();
System.out.println( "开始为" + name + "计时" );
long begin = System.nanoTime();
Object result = proceedingJoinPoint.proceed();
long end = System.nanoTime();
System.out.print( "为" + name + "计时结束," );
System.out.println( "[ " + name + " ]执行耗时 [ " + ( end - begin ) + "ns, ]" );
return result ;
}
@AfterReturning( pointcut = "servicePointcut()" , returning = "xxx")
public void afterReturn( JoinPoint joinPoint , Object xxx ) {
System.out.println("【 " + joinPoint.getSignature().getName() + " 】方法执行后返回了 【 " + xxx +" 】");
}
@AfterThrowing( pointcut = "servicePointcut()" , throwing = "ex")
public void afterThrow( JoinPoint joinPoint , Throwable ex ) {
System.out.println("【 " + joinPoint.getSignature().getName() + " 】方法执行执行时抛出 【 " + ex +" 】");
}
@After( "servicePointcut()" )
public void after( JoinPoint joinPoint ) {
System.out.println( "【 " + joinPoint.getSignature().getName() + " 】方法执行结束" );
}
}
3、创建创建 Spring 配置文件】
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 启用注解
<context:annotation-config />
-->
<!-- 扫描 dao 包中所有的 所有的类 -->
<context:component-scan base-package="com.itlaobing.aop.annotation.dao" />
<!-- 扫描 service 包中所有的 所有的类 -->
<context:component-scan base-package="com.itlaobing.aop.annotation.service" />
<!-- 扫描 controller 包中所有的 所有的类 -->
<context:component-scan base-package="com.itlaobing.aop.annotation.controller" />
<!-- 扫描 aspect 包-->
<context:component-scan base-package="com.itlaobing.aop.annotation.aspect" />
<!-- 告知容器需要为 注解 提供 自动代理支持 -->
<aop:aspectj-autoproxy />
</bean>
<!-- 启用注解
<context:annotation-config />
-->
<!-- 扫描 dao 包中所有的 所有的类 -->
<context:component-scan
base-package="com.itlaobing.aop.annotation.dao"/>
<!-- 扫描 service 包中所有的 所有的类 -->
<context:component-scan
base-package="com.itlaobing.aop.annotation.service"/>
<!-- 扫描 controller 包中所有的 所有的类 -->
<context:component-scan
base-package="com.itlaobing.aop.annotation.controller"/>
</beans>
4、测试类
1 ) AnnotationBasedIoCTest
package com.itlaobing.aop.annotation;
import com.itlaobing.aop.annotation.controller.StudentController;
import com.itlaobing.aop.annotation.dao.StudentDao;
import com.itlaobing.aop.annotation.service.StudentService;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnotationBasedIoCTest {
public static void main(String[] args) {
AbstractApplicationContext container = new ClassPathXmlApplicationContext( "classpath*:com/**/annotation/ioc-annotation.xml" );
StudentDao studentDao1 = container.getBean( "studentDao" , StudentDao.class);
System.out.println( studentDao1 );
StudentDao studentDao2 = container.getBean( "studentDao" , StudentDao.class);
System.out.println( studentDao2 );
StudentService studentService = container.getBean( "studentService" , StudentService.class);
System.out.println( studentService );
System.out.println( studentService.getStudentDao() );
StudentController studentController = container.getBean( "studentController" , StudentController.class);
System.out.println( studentController );
System.out.println( studentController.getStudentService() );
container.close();
}
}
2 )AnnotationBasedAopTest
package com.itlaobing.aop.annotation;
import com.itlaobing.aop.annotation.controller.StudentController;
import com.itlaobing.aop.annotation.dao.StudentDao;
import com.itlaobing.aop.annotation.domain.Student;
import com.itlaobing.aop.annotation.service.StudentService;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnotationBasedAopTest {
public static void main(String[] args) {
AbstractApplicationContext container = new ClassPathXmlApplicationContext( "classpath*:com/**/annotation/aop-annotation.xml" );
StudentService studentService = container.getBean( "studentService" , StudentService.class);
System.out.println( "Class ==> " + studentService.getClass() );
Student s = new Student();
s.setId( 1001 );
s.setName( "奔波儿灞" );
studentService.save( s );
container.close();
}
}