传统的Spring作法是使用.xml文件来对bean进行注入或者是配置aop、事务。咱们先看一个不使用注解的Spring示例,在这个示例的基础上,改为注解版本的,这样也能看出使用与不使用注解之间的区别,先定义一个老师:php
public class Teacher{
private String teacherName = "TW";
public String toString() {
return "TeacherName:" + teacherName;
}
}
复制代码
再定义一个学生:java
public class Student{
private String studentName = "SL";
public String toString() {
return "StudentName:" + studentName;
}
}
复制代码
而后定义一个学校:spring
public class School{
private Teacher teacher;
private Student student;
public void setTeacher(Teacher teacher){
this.teacher = teacher;
}
public void setStudent(Student student){
this.student = student;
}
public Teacher getTeacher(){
return teacher;
}
public Student getStudent(){
return student;
}
public String toString(){
return teacher + "\n" + student;
}
}
复制代码
spring的配置文件这么写:this
<?xml version="1.0" encoding="UTF-8"?>
<bean id="school" class="com.zxt.bean.School" >
<property name="teacher" ref="teacher" />
<property name="student" ref="student" />
</bean>
<bean id="teacher" class="com.zxt.uu.Teacher" />
<bean id="student" class="com.zxt.uu.Student" />
复制代码
这是最初始的.xml配置,很显然这么作有两个缺点:spa
为了解决这两个问题,Spring引入了注解,经过@注解名
的方式,让注解与Java Bean紧密结合,既大大减小了配置文件的体积,又增长了Java Bean的可读性与内聚性。code
顾名思义,就是自动装配。其做用是替代Java代码里面的getter/setter与bean属性中的property。若是私有属性须要对外提供的话,getter应当予以保留。引入@Autowired注解,先看一下spring配置文件怎么写:component
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.2.xsd">
9
10 <context:component-scan base-package="com.zxt" />
11
12 <bean id="school" class="com.zxt.bean.School" />
13 <bean id="teacher" class="com.zxt.uu.Teacher" />
14 <bean id="student" class="com.zxt.uu.Student" />
15
16 </beans>
复制代码
注意第10行,为了实现bean的自动载入,必须配置spring的扫描器。xml
在base-package指明一个包:对象
<context:component-scan base-package=“com.zxt”/>
复制代码
代表com.zxt包及其子包中,若是某个类的头上带有特定的注解@Component
、@Repository
、@Service
或@Controller
,就会将这个对象做为Bean注入进spring容器。事务
看到第12行,原来school里面应当注入两个属性teacher、student,如今不须要注入了。再看下,School.java也很简练,把getter/setter均可以去掉:
public class School{
@Autowired
private Teacher teacher;
@Autowired
private Student student;
public String toString(){
return teacher + "\n" + student;
}
}
复制代码
这里@Autowired
注解的意思就是,当Spring发现@Autowired
注解时,将自动在代码上下文中找到与其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方去。
@Resource注解做用与@Autowired很是类似。先看一下@Resource,直接写School.java了:
public class School{
@Resource(name = "teacher")
private Teacher teacher;
@Resource(type = Student.class)
private Student student;
public String toString(){
return teacher + "\n" + student;
}
}
复制代码
这是详细一些的用法,说一下@Resource的装配顺序:
使用@Service,能够更加简化.xml文件配置。
由于spring的配置文件里面还有12行~14行三个bean,应用spring配置文件里面一个自动扫描的标签,能够把这三个bean也给去掉,加强Java代码的内聚性并进一步减小配置文件。先看一下配置文件:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.2.xsd">
9
10 <context:component-scan base-package="com.zxt" />
11 </beans>
复制代码
配置文件看起来特别清爽。School.java,Teacher.java和Student.java分别作以下修改:
@Service
public class School{
@Autowired
private Teacher teacher;
@Autowired
private Student student;
public String toString(){
return teacher + "\n" + student;
}
}
复制代码
@Service
public class Teacher{
private String teacherName = "TW";
public String toString() {
return "TeacherName:" + teacherName;
}
}
复制代码
@Service
public class Student{
private String studentName = "SL";
public String toString() {
return "StudentName:" + studentName;
}
}
复制代码
这样,School.java在Spring容器中存在的形式就是"school",便可以经过ApplicationContext的getBean("school")
方法来获得School.java。
@Service注解,其实作了两件事情: