Spring常用的注解
一.@Component注解:
首先配置好Spring的各种配置文件。接下来我们先创建一个实体类进行具体的演示。
package com.superliu.bean;
public class Man {
private int id;
private String name;
private String hobby;
public Man() {
}
public Man(int id, String name, String hobby) {
this.id = id;
this.name = name;
this.hobby = hobby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "Man{" +
"id=" + id +
", name='" + name + '\'' +
", hobby='" + hobby + '\'' +
'}';
}
}
添加Component注解:
在实体类中添加注解。
1.默认(类名小写开头):
2.给对象命名(Strongman):
扫描注解类:
在applicationContext.xml文件中添加扫描注解类,目的是告诉Spring框架哪些类上面有注解,需要Spring框架处理。
在测试类中打印:
打印创建的类。
1.默认对象名的打印:
2.命名后的对象打印:
运行:
在测试类中运行。
二.@Value注解:
1.在实体类中添加Value注解,将对象进行赋值。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("Strangman") //给类创建对象,对象名为Strangman
public class Man {
@Value("111")
private int id;
@Value("麻生希")
private String name;
@Value("直播")
private String hobby;
public Man() {
}
public Man(int id, String name, String hobby) {
this.id = id;
this.name = name;
this.hobby = hobby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "Man{" +
"id=" + id +
", name='" + name + '\'' +
", hobby='" + hobby + '\'' +
'}';
}
}
2.测试运行:
三.@Autowired注解(匹配一个对象):
1.创建一个接口类(Mygirls)和两个实现类(ChineseGirl和EnglishGirl):
接口类:
实现类:(需要加上Component注解且只能加其中一个)
2.在实体类中添加Autowired注解(匹配一个对象):
import com.superliu.girls.Mygirls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("Strangman") //给类创建对象,对象名为Strangman
public class Man {
@Value("111")
private int id;
@Value("刘峰林")
private String name;
@Value("直播")
private String hobby;
//注入值
@Autowired //自动装配(根基类型自动装配一个):Spring框架一旦启动,自动找一个类型相匹配的对象注入进来!
private Mygirls girl;
public Mygirls getGirl() {
return girl;
}
public void setGirl(Mygirls girl) {
this.girl = girl;
}
public Man() {
}
public Man(int id, String name, String hobby) {
this.id = id;
this.name = name;
this.hobby = hobby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "Man{" +
"id=" + id +
", name='" + name + '\'' +
", hobby='" + hobby + '\'' +
'}';
}
}
3.测试运行:
四.@Qualifier注解(与@Autowired注解配合使用):
当@Autowired注解有多个对象能被匹配的时候,@Qualifier指定某一个对象匹配,所有@Qualifier注解不能单独使用。
1.在实体类中添加@Qualifier注解:
import com.superliu.girls.Mygirls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("Strangman") //给类创建对象,对象名为Strangman
public class Man {
@Value("111")
private int id;
@Value("刘峰林")
private String name;
@Value("直播")
private String hobby;
//注入值
@Autowired //自动装配(根基类型自动装配一个):Spring框架一旦启动,自动找一个类型相匹配的对象注入进来!
@Qualifier("chineseGirl")//需要配合@Autowired使用,不能单独使用。
private Mygirls girl;
public Mygirls getGirl() {
return girl;
}
public void setGirl(Mygirls girl) {
this.girl = girl;
}
public Man() {
}
public Man(int id, String name, String hobby) {
this.id = id;
this.name = name;
this.hobby = hobby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "Man{" +
"id=" + id +
", name='" + name + '\'' +
", hobby='" + hobby + '\'' +
'}';
}
}
2. 指定某一个对象与@Autowired匹配
五.@Resource注解:
1.在实体类中添加@Qualifier注解:
标以上运行会报错,因为Resource注解默认是根据类型自动装配,如果匹配多个对象,会出现异常。
2.争对多个对象添加@Qualifier注解:
3.运行:
六.@Scope注解:
1.@Scope(“singleton”)---------------单例
2.@Scope(“prototype”)---------------多例
七.初始化和销毁的注解:
1.@PostConstruct--------------初始化调用
2.@PreDestroy-------------------销毁调用
转载:https://blog.csdn.net/Baichi_00/article/details/101175296
查看评论