spring容器对于Bean的建立和对象属性的依赖注入提供了注解的支持,让咱们在开发中可以更加便捷的实现对象的建立和对象属性的依赖注入。
一,对于Bean的建立spring容器提供了如下四个注解的支持:
一、@Component
二、@Repository dao层实现类的注解
三、@Service service层实现类的注解
四、@Controller controller层实现类的注解
以上四个注解在普通使用中是等效的,但在web项目中为了区分三层架构中不一样层之间Bean的建立,为了不注解使用的混乱,使用后三个注解进行区分。
二,对于Bean属性的依赖注入分为两类,一类是对于属性是String类型或者基本数据类型Spring容器提供了@Value这个注解,另外一类是对于属性是对象的提供了@Autowired和@Resource这两个注解。
其中,@Autowired这个注解是spring框架自带的注解,而@Resource(javax.annotation.Resource)这个注解是javax扩展包中注解规范的一种,而spring对这一注解提供了支持。
下面咱们经过实验来讲明注解对于bean建立和bean属性依赖注入的实现。
首先要在配置文件中配置注解扫描的驱动。
java
<context:annotation-config/>
<context:component-scan base-package="com.opensource"/>
这里提一句,若是配置了注解扫描包的范围,也就是第二行,那么<context:annotation-config/>能够不用配置,由于配置扫描包的范围后,注解的驱动也就有了。
实验一,bean的建立,由于spring容器对于bean建立的四个注解是等效,这里咱们使用@Component这个注解程序员
Student类:
@Component public class Student { public Student(){ System.out.println("spring容器调用Student类的无参构造器"); }
测试类:
public class MyTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("spring-bean.xml"); } }
实验结果:

实验二:bean属性为String类型及基本数据类型的的依赖注入web
student类:spring
@Component(value = "student") public class Student { @Value("张三") private String name; @Value("23") private int age; public String getName() { return name; } public int getAge() { return age; } }
在这里 @Component(value = "student") value指定的是bean的id,另外对于注解方式实现的依赖注入,bean的属性无需再提供setter方法。架构
测试类:框架
public class MyTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("spring-bean.xml"); Student student = (Student)ac.getBean("student"); System.out.println(student.getName()); System.out.println(student.getAge()); } }
实验结果:测试
实验三:bean属性为java对象的依赖注入。spa
对于bean属性为java对象的依赖注入,分为两种一种是byName类型的,一种是byType类型的。而使用的注解分别是@Autowired和@Resource,对于byType类型的二者的使用没有区别,可是对于byName类型的@Autowired要使用联合注解@Qualifier,因此说使用@Resource较为简单。这里咱们在研究对于bean属性为java对象的依赖注入时就使用@Resource这个注解了。code
实验3.1:按照byName类型对bean属性为java对象的依赖注入。component
上个实验中的Student类保持不变,再提供一个Teacher类。
@Component("teacher") public class Teacher { @Resource(name = "student") private Student student; public Student getStudent() { return student; } }
测试类:
public class MyTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("spring-bean.xml"); Teacher teacher = (Teacher)ac.getBean("teacher"); System.out.println(teacher.getStudent().getName()); System.out.println(teacher.getStudent().getAge()); } }
实验结果:
实验3.2:按照byType类型对bean属性为java对象的依赖注入。
对于这种方式的依赖注入,只需分别修改Student类和Teacher类。
@Component public class Student { @Value("张三") private String name; @Value("23") @Qualifier private int age; public String getName() { return name; } public int getAge() { return age; } }
@Component("teacher") public class Teacher { @Resource private Student student; public Student getStudent() { return student; } }
固然在这里Student类的id也是能够保留的,这里这么作是为了说明问题。实验结果同上。
最后说一点,咱们做为程序员,研究问题仍是要仔细深刻一点的。当你对原理了解的有够透彻,开发起来也就驾轻就熟了,不少开发中的问题和疑惑也就迎刃而解了,并且在面对其余问题的时候也可作到举一反三。固然在开发中没有太多的时间让你去研究原理,开发中要以实现功能为前提,可等项目上线的后,你有大把的时间或者空余的时间,你大可去刨根问底,深刻的去研究一项技术,为以为这对一名程序员的成长是很重要的事情。