Spring注解
声明Bean的注解:
@Component:组件,没有明确的角色
@Service:在业务逻辑层(Service)使用
@Repository:在数据访问层(Dao)使用
@Controller:在展现层(MVC–SpringMvc)使用
注入Bean的注解:
@Aautowired:Spring提供的注解
@Inject : JSR-330提供的注解
@Resource : JSR-250提供的注解
@Primary 注入时优先使用此注解下的实现类,对应org.springframework.beans.factory.NoUniqueBeanDefinitionException异常
配置文件的注解:
@Configuration : 声明当前类是个配置类,相当于一个Spring配置的xml文件.
@ComponentScan (cn.test.demo): 自动扫描包名下所有使用 @Component @Service @Repository @Controller 的类,并注册为Bean
@WiselyConfiguration : 组合注解 可以替代 @Configuration和@ComponentScan
@Bean : 注解在方法上,声明当前方法的返回值为一个Bean.
AOP切面编程注解:
@Aspect : 声明这是一个切面
@After @Before. @Around 定义切面,可以直接将拦截规则(切入点 PointCut)作为参数
@PointCut : 专门定义拦截规则 然后在 @After @Before. @Around 中调用
@Transcational : 事务处理
@Cacheable : 数据缓存
@EnableAaspectJAutoProxy : 开启Spring 对 这个切面(Aspect )的支持
SpringMVC 常用注解:
@Controller : 注解在类上 声明这个类是springmvc里的Controller,将其声明为一个spring的Bean.
@RequestMapping :可以注解在类上和方法上,映射WEB请求(访问路径和参数)
@ResponseBody : 支持将返回值放入response体内,而不是返回一个页面(返回的是一个组数据)
@RequestBody : 允许request的参数在request体中,而不是直接连接在地址后面,次注解放置在参数前
@PathVariable : 用来接收路径参数 如/test/001,001为参数,次注解放置在参数前
@RestController : @Controller + @ResponseBody 组合注解
@ControllerAdvice : 通过@ControllerAdvice可以将对已控制器的全局配置放置在同一个位置
@ExceptionHandler : 用于全局处理控制器的异常
@ExceptionHandier(value=Exception.class) –>通过value属性可过滤拦截器条件,拦截所有的异常
@InitBinder : 用来设置WebDataBinder , WebDataBinder用来自动绑定前台请求参数到Model中.
@RunWith : 运行器
@RunWith(JUnit4.class)就是指用JUnit4来运行
@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境
@RunWith(Suite.class)的话就是一套测试集合,
@WebAppConfiguration(“src/main/resources”) : 注解在类上,用来声明加载的ApplicationContex 是一个WebApplicationContext ,它的属性指定的是Web资源的位置,默认为 src/main/webapp ,自定义修改为 resource
@Before : 在 xxx 前初始化