常用注解
注解 | 描述 |
---|---|
@Component | 泛指组件,类似在xml中配置bean |
@Configuration | 把类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean。 |
@Repository | 用于标注数据访问组件 |
@Service | 用于标注业务层组件,默认bean的id为类名且首字母小写 |
@Controller | 用于标注控制层组件 |
@RestController | spring4.0之后,@controller和@responsebody的结合 |
@Scope | 作用域 表示将Action的范围声明为原型,可以利用容器的scope=“prototype"来保证每一个请求有一个单独的Action来处理,避免struts中Action的线程安全问题。spring默认scope是单例模式(scope=“singleton”),这样只会创建一个Action对象,每次访问都是同一Action对象,数据不安全,struts2是要求每次次访问都对应不同的Action,scope=“prototype"可以保证当有请求的时候都创建一个Action对象。 |
@Lazy(true) | 延迟初始化 |
@RequestMapping | 用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。value:指定请求的实际地址;method:指定请求的method类型,GET、POST、PUT、DELETE等(Spring4.3中引进了{@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping},来帮助简化常用的HTTP方法的映射) |
@ResponseBody | 将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,效果等同于通过response对象输出指定格式的数据 效果等同于如下代码:@RequestMapping("/login”) public void login(User user, HttpServletResponse response){ response.getWriter.write(JSONObject.fromObject(user).toString()); } |
@PostConstruct | 初始化注解,用于指定初始化方法(用在方法上) |
@PreDestory | 用于指定销毁方法(用在方法上) |
@DependsOn | 定义Bean初始化及销毁时的顺序 |
@Primary | 自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常 |
@Autowired | 默认按类型装配,如果我们想使用按名称装配,可以结合@Qualifier注解一起使用 |
@Resource | 默认按名称装配,当找不到与名称匹配的bean才会按类型装配 |
@Async | 异步方法调用 |
@RequestParam | 接收参数,从request里面拿取值 |
@PathVariable | 接收参数,从一个URI模板里面来填充 |
@PathParam | 这个注解是和spring的pathVariable是一样的,也是基于模板的,但是这个是jboss包下面的一个实现,上面的是spring的一个实现,都要导包 |
@QueryParam | JAX-RS 提供的,和Spring的RequestParam作用一致 |
@RequestBody | 一般是post请求的时候才会使用这个请求(处理application/json、application/xml等格式的数据) |
属性注入
@Autowired
自动装配,可消除代码里的getter/setter与bean属性中的property
如果不使用注解自动注入,必须通过代码手动注入,如下:
...
<bean id="user" class="xxx.User" />
<property name="student" ref="student" />
</bean>
<bean id="student" class="xxx.Student" />
...
@Test
public void test(){
//读取配置文件
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=(User) ctx.getBean("user");
}
而使用注解@Autowired大大减少了代码量,提高可读性。
使用前配置文件要添加扫描器
...
<context:component-scan base-package="xxx.vo" />
<bean id="user" class="xxx.User" />
<bean id="student" class="xxx.Student" />
...
class User{
@Autowired
private Student student;
...
}
注:当xml配置了user的属性时,Spring会按照xml优先的原则去User.java中寻找这两个属性的getter/setter,导致的结果就是初始化bean报错
<context:component-scan base-package="xxx.vo" />
<bean id="user" class="xxx.User">
<property name="student" ref="student" />
</bean>
<bean id="student" class="xxx.Student" />
class User{
@Autowired
private Student student;
//无getter/setter
}
注:当xml未配置了student的bean时会抛出找不到bean的异常,可将@Autowired注解的required设置为false,默认student为null
<context:component-scan base-package="xxx.vo" />
<bean id="user" class="xxx.User" />
class User{
@Autowired(required=false)
private Student student;
//无getter/setter
}
Qualifier
指定注入Bean的名称
@Resource
@Resource注解与@Autowired注解作用相似
class User{
@Resource(name="student")
private Student student;
//@Resource(type="Student.class")
//private Student student;
...
}
@Resource的装配顺序: (1)、@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配 (2)、指定了name或者type则根据指定的类型去匹配bean,任何一个不匹配都将报错
@Autowired和@Resource两个注解的区别: (1)、@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配 (2)、@Autowired是Spring的注解,@Resource是J2EE的注解
@Value
基本数值的填充 :@Value("")
常用于获取配置文件参数值 ${配置文件中参数名}
直接new对象是无法获取参数值的,必须通过springIOC
@Value注入static属性的两种方式
//1.通过执行set方法进行参数注入
private static String userName;
@Value("${git.userName}")
public void setUserName(String userName) {
GitUtils.userName = userName;
}
//2.通过中间变量赋值
private static String userName;
@Value("${git.userName}")
private String userNameTmp;
@PostConstruct
public void init() {
userName = userNameTmp;
}
接收参数
@RequestParam
处理Content-Type为 application/x-www-form-urlencoded编码的内容
http://localhost:8080/springmvc/hello/101?param1=10¶m2=20
根据上面的这个URL,你可以用这样的方式来进行获取
public String getDetails(
@RequestParam(value="param1", required=true) String param1,
@RequestParam(value="param2", required=false) String param2){
...
}
@RequestParam 支持下面四种参数 defaultValue 如果本次请求没有携带这个参数,或者参数为空,那么就会启用默认值 name 绑定本次参数的名称,要跟URL上面的一样 required 这个参数是不是必须的 value 跟name一样的作用,是name属性的一个别名
@PathVariable
这个注解能够识别URL里面的一个模板,我们看下面的一个URL http://localhost:8080/springmvc/hello/101?param1=10¶m2=20 上面的一个url你可以这样写:
public String getDetails(
@RequestMapping("/hello/{id}")
public String getDetails(@PathVariable(value="id") String id,
@RequestParam(value="param1", required=true) String param1,
@RequestParam(value="param2", required=false) String param2){
...
}
@RequestBody
@RequestBody用于post请求,处理 application/json、application/xml等格式的数据
@ModelAttribute
@ModelAttribute注解类型将参数绑定到Model对象,处理 multipart/form-data 格式的数据