Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能.所以,如果 Web 应…
一.spring常规方式. 在使用注释配置之前,先来回顾一下传统上是如何配置 Bean 并完成 Bean 之间依赖关系的建立.下面是 3 个类,它们分别是 Office.Car 和 Boss,这 3 个类需要在 Spring 容器中配置为 Bean: Office 仅有一个属性: 清单 1. Office.java package com.baobaotao; public class Office { private String officeNo =”001”; //省略 get/sette…
pring-mvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/X…
-------------------------注解扫面的bean的ID问题-------------------------- 0.前提需要明白注解扫描出来的bean的id默认是类名首字母小写,当然可以指定id: (1)只写注解不指定id 上面实际上是等价于xml中的下面配置: <bean id="userServiceImpl" class="cn.qlq.service.UserService"></bean> 测试:(程序中获取sp…
转载:http://www.cnblogs.com/think-in-java/p/5474740.html @Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入. 1.共同点 两者都可以写在字段和setter方法上.两者如果都写在字段上,那么就不需要再写setter方法. 2.不同点 (1)@Autowired @Autowire…
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入. 1.共同点 两者都可以写在字段和setter方法上.两者如果都写在字段上,那么就不需要再写setter方法. 2.不同点 (1)@Autowired @Autowired为Spring提供的注解,需要导入包org.springframework.beans.factory.an…
Spring 注解(一)Spring 注解编程模型 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 注解系列文章: Spring 注解(一)Spring 注解编程模型 Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils 一.术语 1.1 元注解 元注解是一种标注在别的注解之上的注解.如果一个注解可以标注在别的注解上,那么这个注解已然是元注解.…
声明Bean的注解: @Component : 组件,没有明确的角色 @Service : 在业务逻辑层(service层)使用 @Repository : 在数据访问层(dao层)使用. @Controller : 在展现层(MVC--SpringMVC)使用 注入Bean的注解: @Aautowired : Spring提供的注解. @Inject : JSR-330提供的注解 @Resource : JSR-250提供的注解 配置文件的注解: @Configuration : 声明当前类是…
1.配置文件形式: <context:component-scan base-package="com.atguigu" /> spring会扫描此包下的@Service @Repository  @Component @Autoware @Resource 等注解 2.注解形式 在配置文件注解类(@Configuration)上声明@ComponentScans,里面包含多个@ComponentScan, 或是只声明@ComponentScan package com.a…
Spring 框架中有很多可用的注解,其中有一类注解称模式注解(Stereotype Annotations),包括 @Component, @Service,@Controller,@Repository 等.只要在相应的类上标注这些注解,就能成为 Spring 中组件(Bean). 需要配置开启自动扫描.如在 XML 中配置 ` 或使用注解 @ComponentScan. 从最终的效果上来看,@Component, @Service,@Controller,@Repository 起到的作用…