开始时间:2022-01-08
课程地址
注解的使用
通过spring的注解完成java对象的创建,属性。代替xml文件
实现步骤:
1.加入依赖
2.创建类,在类中加入注解
3.创建spring的配置文件
声明组件扫描器的标签,指名注解在你的项目的中的位置。
4.使用注解创建对象, 创建容器ApplicationContext
注解复习
@Component注解方便创建对象
spring中和@Component功能一致,创建对象的注解还有:
1.@Repository(用在持久层类的上面) : 放在dao的实现类上面,
表示创建dao对象,dao对象是能访问数据库的。
2.@Service(用在业务层类的上面):放在service的实现类上面,
创建service对象,service对象是做业务处理,可以有事务等功能的。
3.@Controller(用在控制器的上面):放在控制器(处理器)类的上面,创建控制器对象的,
控制器对象,能够接受用户提交的参数,显示请求的处理结果。
以上三个注解的使用语法和@Component一样的。 都能创建对象,但是这三个注解还有额外的功能。
@Repository,@Service,@Controller是给项目的对象分层的。
component注解
1.加载依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
2.写类,类上加注解
package BUPT.ba01;
import org.springframework.stereotype.Component;
//创建对象,类似于bean,value对应bean的id
@Component(value = "myStudent")
public class Student {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
3.写spring config
<?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">
<!--声明组件扫描器,组件指的是Java对象
base-package:指定注解在项目中的包名
component-scan工作方式:spring会扫描遍历base-package指定的包
扫描包和子包的所有注解,按照注解创建类
-->
<context:component-scan base-package="BUPT.ba01"/>
</beans>
最后测试,和之前的测试基本一致
package BUPT;
import static org.junit.Assert.assertTrue;
import BUPT.ba01.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Unit test for simple App.
*/
public class AppTest {
@Test
public void test02() {
//使用spring容器创建的对象
//1.指定spring配置文件的名称
String config = "applicationContext.xml";
//2.创建表示spring容器的对象, ApplicationContext
// ApplicationContext就是表示Spring容器,通过容器获取对象了
// ClassPathXmlApplicationContext:表示从类路径中加载spring的配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
Student mystudent = (Student) ac.getBean("myStudent");
System.out.println(mystudent);
}
}
指定包的不同方式
<!--第一种方式:使用多次组件扫描器,指定不同的包-->
<context:component-scan base-package="BUPT.ba01"/>
<context:component-scan base-package="BUPT.ba02"/>
<!--第二种方式:使用分隔符(;或,)分隔多个包名-->
<context:component-scan base-package="BUPT.ba01;BUPT.ba02" />
<!--第三种方式:指定父包-->
<context:component-scan base-package="BUPT" />
简单类型属性注入@Value(掌握)
Student类里面这样写
@Value("JDH")
private String name;
@Value("23")
private int age;
就可以在Test里面发现赋好值了
引用类型赋值
byType 自动注入@Autowired(掌握)
Student类多一个School的引用类型
通过Autowired实现自动注入
package BUPT.ba03;
@Component("myStudent")
public class Student {
@Value("WY")
private String name;
@Value("20")
private int age;
@Autowired
public School school;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSchool(School school) {
System.out.println("school:" + school);
this.school = school;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", school=" + school +
'}';
}
}
在School类里面写注解
package BUPT.ba03;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("mySchool")
public class School {
@Value("haidian")
private String address;
@Value("BUPT")
private String name;
public void setAddress(String address) {
this.address = address;
}
public void setName(String name) {
this.name = name;
}
```java
@Override
public String toString() {
return "School{" +
"address='" + address + '\'' +
", name='" + name + '\'' +
'}';
}
}
测试类就可以用了
byName 自动注入@Autowired(掌握)
需要在引用属性上联合使用注解@Autowired 与@Qualifier。 @Qualifier 的 value 属性用
于指定要匹配的 Bean 的 id 值。
其他方式与上面一致
@Autowired
@Qualifier("mySchool")
public School school;
@Autowired 还有一个属性 required,默认值为 true,表示当匹配失败后,会终止程序运
行。若将其值设置为 false,则匹配失败,将被忽略,未匹配的属性值为 null。
JDK 注解@Resource 自动注入(掌握)
@Resource 注解既可以按名称匹配Bean,
也可以按类型匹配 Bean。
== 默认是按名称注入。==
使用该注解,要求 JDK 必须是 6 及以上版本。
@Resource 可在属性上,也可在 set 方法上
@Resource 注解若不带任何参数, 采用默认按名称的方式注入,按名称不能注入 bean,
则会按照类型进行 Bean 的匹配注入。
@Resource 注解指定其 name 属性,则 name 的值即为按照名称进行匹配的 Bean 的 id
注解与 XML 的对比
- 注解优点是:
⚫ 方便
⚫ 直观
⚫ 高效(代码少,没有配置文件的书写那么复杂)。
其弊端也显而易见:以硬编码的方式写入到 Java 代码中,修改是需要重新编译代码的。 - XML 方式优点是:
⚫ 配置和代码是分离的
⚫ 在 xml 中做修改,无需编译代码,只需重启服务器即可将新的配置加载。
xml 的缺点是:编写麻烦,效率低,大型项目过于复杂。
结束时间:2022-01-09