一、注释配置相对于 XML 配置具有很多的优势:
1、他可以充分利用java反射机制获取类结构信息。这些信息可以有效减少配置的工作,如使用JPA注释配置ORM映射时,我们就不需要指定的PO的属性名、类信息,如果关系表字段和PO属性名,类型都一致,甚至无需编写任务属性的映射信息,因为这些信息都可以通过java反射机制获取。
2、注释和java代码位于一个文件中,而XML配置采用独立的配置文件,大多数配置信息在程序开发完成后都不会调整,如果配置信息和java代码放在一起,有助于增强程序的内聚性。而采用独立的XML配置文件,程序员在编写一个功能的时候,往往需要在程序文件和配置文件中不停切换,而这种思维上的不连贯会降低开发效率。
3、在很多情况下,注释配置比XML配置更受欢迎,因此可以使用注释进行Bean定义和依赖注入的内容。
4、用注解来向spring容器注册bean。需要在applicationContext.xml中注册<context:component-scan base-package=”pagkage1[,pagkage2,…,pagkageN]”/>。
二、案例
1、创建实体类
Student类
package cn.happy.entity;
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;
import javax.annotation.Resource;
//使用component,默认使用类名首字母小写作为spring的对象
@Component("student") //此处括号中的内容也可以省略
public class Student {
@Value("lucy")
private String name;
@Value("18")
private int age;
//@Resource(name="car") //JDK的注解。括号中的可以省略 注入时两种:先baName,不写时,默认会在内存中找car同名的对象。
//另一种注解:
@Autowired
@Qualifier("car")
private Car car; //当为car2时,会按byType找,只有一个同类型的bean才能注入成功
//当有一个littleCar去继承Car时,会报错。
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", car=" + car +
'}';
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void getData(){
System.out.println(name);
}
}
Car类
package cn.happy.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("car")
public class Car {
@Value("兰博基尼")
private String brand;
@Value("紫色")
private String color;
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", color='" + color + '\'' +
'}';
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
2、编写xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="cn.happy.entity"></context:component-scan>
</beans>
3、编写测试类
package cn.happy.test01;
import cn.happy.entity.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ZJtest {
@Test
public void testZJ(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = (Student)ctx.getBean("student");
System.out.println(stu);
}
}