1、概念
注解(Annotation),也叫元数据(Metadata),是Java5的新特性,JDK5引入了Metadata很容易的就能够调用Annotations。注解与类、接口、枚举在同一个层次,并可以应用于包、类型、构造方法、方法、成员变量、参数、本地变量的声明中,用来对这些元素进行说明注释。
注解是以‘@注解名’在代码中存在的。
2、作用
1.生成文档。这是最常见的,也是java 最早提供的注解。常用的有@see @param @return 等;
2.跟踪代码依赖性,实现替代配置文件功能。比较常见的是spring 2.5 开始的基于注解配置。作用就是减少配置。现在的框架基本都使用了这种配置来减少配置文件的数量;
3.在编译时进行格式检查。如@Override放在方法前,如果你这个方法并不是覆盖了超类方法,则编译时就能检查出;
3、使用
1、@Component
1、在类前边添加@Component
如果没有name值默认是累的首字母小写
2、@Component(name=“users”)
添加nama值自定义名字
2、@Repository @Service @Controller
这三个注解是 @Component 注解的衍生注解,功能一样。
@Repository :dao层
@Service:service层
@Controller:web层
3、@Resource
@Resource 注解,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Resource 的使用来消除 set ,get方法。
在User类中没有给Person set ,get方法。
public class Users {
private int uid;
private String uname;
@Resource(name ="person")
private Person person;
@Override
public String toString() {
return "Users{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", person=" + person +
'}';
}
}
@Component("person")
public class Person {
private int pid;
private String pname;
@Override
public String toString() {
return "Person{" +
"pid=" + pid +
", pname='" + pname + '\'' +
'}';
}
运行结果
Users{uid=0, uname='null', person=Person{pid=0, pname='李垚'}}
4、@Autowired
功能和注解 @Resource 一样,可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。只不过注解@Resource 是按照名称来进行装配,而@Autowired 则是按照类型来进行装配。
public interface PersonDao {
public void savePerson();
}
@Component("personDaoImplOne")
public class PersonDaoImplOne implements PersonDao{
@Override
public void savePerson() {
System.out.println("save Person One");
}
}
@Service("personService")
public class PersonService{
@Autowired
private PersonDao personDao;
@Autowired
private UserDao usersDaoImplOne; //1、写实现类注解
@Autowired(required=false)
@Qualifier("UsersDaoImpTwo")//2、@Autowired 和 @Qualifier("名称") 配合使用
private UserDao userDao;
/*三者运行其一*/
public void savePerson() {
this.personDao.savePerson();
}
}
注意:
在使用@Autowired时,首先在容器中查询对应类型的bean
如果查询结果刚好为一个,就将该bean装配给@Autowired指定的数据
如果查询的结果不止一个,那么@Autowired会根据名称来查找。
如果查询的结果为空,那么会抛出异常。解决方法时,使用required=false
4、配置
1:在 applicationContext.xml 中引入命名空间
这里我们简单讲解一下这里引入的命名空间,简单来说就是用来约束xml文件格式的。第一个 xmlns:context ,这表示标签格式应该是 <context:标签名。添加下边是三个:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
/*添加1*/ xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
/*添加2*/ http://www.springframework.org/schema/context
/*添加3*/ http://www.springframework.org/schema/context/spring-context.xsd">
2:在 applicationContext.xml 文件中引入注解扫描器
<context:component-scan base-package="com.zhihui.zhujie"/>
整个配置文件:
<?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"
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">
<context:component-scan base-package="com.zhihui.zhujie"/>
</beans>
3:在 Person 类中添加注解@Component
在类前添加@Component(“名称”)
@Component("users")
public class Users {
4:测试
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Users u=(Users) ac.getBean("users");
System.out.println(u);