前言
什么是注解?
用一个词就可以描述注解,那就是元数据,即一种描述数据的数据。所以,可以说注解就是源代码的元数据。可以理解为一种标记。
为什么要引入注解?
简化开发
需要用到的注解
@Component
@Service
@Controller
@Reposity
上面四个注解都是用于标记Bean的实体类,用于spring容器能够在创建Bean时,能找到对应的实体类进行Bean的初始化,其中@component的作用范围最大
@Autowired 自动装配通过类型,名字
@Autowired顾名思义,就是自动装配,其作用是为了消除代码Java代码里面的getter/setter与bean属性中的property。当然,getter看个人需求,如果私有属性需要对外提供的话,应当予以保留。@Autowired默认按类型匹配的方式,在容器查找匹配的Bean,当有且仅有一个匹配的Bean时,Spring将其注入@Autowired标注的变量中。
如果不唯一通过@Qualifier获取
@Resource: 自动装配通过名字,类型
@Configuration 标记该类为配置类
@ComponentScan 扫描指定类或包
流程
1、导入依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.3</version>
</dependency>
2、创建实体类
package pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Teacher {
@Value("Jerry")//动态将值注入
private String name;
@Value("12")
private Integer age;
public Teacher() {
}
public Teacher(String name, Integer age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
@Component
public class Student {
@Value("tom")
private String name;
@Value("12")
private Integer age;
private Teacher teacher;
public Student() {
}
@Autowired
@Qualifier("teacher1")
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", teacher=" + teacher +
'}';
}
}
3、方式一
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="pojo"/>
<bean id="student" class="pojo.Student"/>
<bean id="teacher1" class="pojo.Teacher"/>
</beans>
测试
@Test
public void test5(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
Student student = context.getBean("student", Student.class);
System.out.println(student);
}
/**
output: Student{name='tom', age=12, teacher=Teacher{name='Jerry', age=12}}
**/
4、方式二
不使用配置文件
-
创建注解类
-
package config; import method.UserDAO; import method.UserDAOImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import pojo.Student; import pojo.Teacher; @Configuration @ComponentScan({"pojo","method"}) public class springConfig { @Bean public UserDAO getUserDAO(){ return new UserDAOImpl(); } @Bean public Student student(){ return new Student(); } @Bean public Teacher teacher1(){ return new Teacher(); } }
@Test
public void test4(){
BeanFactory context = new AnnotationConfigApplicationContext(springConfig.class);
Student student = context.getBean("student", Student.class);
System.out.println(student);
}
/**
output: Student{name='mike', age=12, teacher=Teacher{name='john', age=55}}
**/