基于注解方式实现对象创建和注入属性
基本概念
- 注解使用在类、方法、成员属性上面
- 使用注解目的:简化xml配置
创建对象
- 四个注解
- @Component
- @Service
- @Controller
- @Repository
- 四个注解功能是一样的,都可以创建bean实例
- 实际开发各有偏重
实现方式
- 引入依赖。除了5个基本jar包,见Spring5框架(一)https://ningqian.blog.youkuaiyun.com/article/details/116276783
还需要导入Spring的aop包,可以从官网下载
https://repo.spring.io/release/org/springframework/spring/
也可以从我的蓝奏云下载(5.3.6Version)
https://wwa.lanzous.com/iAHpxop0npc
选择文件夹libs——spring-aop-5.3.6.jar,将其导入idea工程中,导入方法同样在前文(一)中有讲 - 在xml配置文件中开启组件扫描
- 添加名称空间
- 添加扫描的包
<?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 http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启组件扫描
1. 如果扫描多个包,多个包使用逗号隔开
或扫描包上层目录
-->
<context:component-scan base-package="com.annotation.spring5"></context:component-scan>
</beans>
- 使用四个注解中的其中一个创建实例对象
//value属性相当远原先xml配置文件bean标签中的id属性
//value属性可以不写,默认其值为类名首字母小写
@Component(value = "userService")
public class UserService {
}
- 测试方法
@Test
public void testAnnotation1(){
ApplicationContext context = new ClassPathXmlApplicationContext("beanAnnotation1.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
}
- 组件扫描的补充配置
如果需要限定扫描的类,可以有如下两种限定方式:
- 第一种
<context:component-scan base-package="com.atguigu.springmvc" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
- 第二种
<context:component-scan base-package="com.atguigu.springmvc">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
基于注解方式注入属性
- 三个常用注入属性的注解
(1)@AutoWired:根据属性类型进行自动装配
(2)@Qualifier:根据属性名称进行注入
(3)@Resource:可以根据类型注入,也可以根据名称注入 - @AutoWired
- 将主类和成员类属性都添加创建实例对象注解
- 在成员类属性前添加注入属性注解
- 不需要显式添加set方法(已封装)
- 如下:
@Component(value = "service")
public class Service {
public void saySelf(){
System.out.println("我是服务类");
}
}
@Component(value = "userService")
public class UserService {
@Autowired
private Service myService;
public void saySelf(){
System.out.println("我是用户服务类");
}
public void sayService(){
myService.saySelf();
}
}
测试方法
@Test
public void testAmmotation2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beanAnnotation1.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.saySelf();
userService.sayService();
}
- @Qualifier
- 需要配合AutoWired使用
- 只需在2中的注解下添加如下的注解即可
@Autowired
@Qualifier(value = "service")//value为类Service的value
private Service myService;
- @Resource
- 首先需要导入Resource的依赖包
导入的方式有两种:
(1)在线下载:
在类的前面添加导包代码
import javax.annotation.Resource;
然后Resource是红色的,表明没有该包,将鼠标移到Resource上面,点击提示:下载javaEE的9个jar文件,点击下载,然后会自动下载,如果在文件目录中有lib文件夹,则会下载到lib文件夹下,如果没有,那我也不知道了,没测试过。这个不重要,只要能下载下来就好。
(2)添加本地jar包
我已经给你们打包好了,地址:
https://wwa.lanzous.com/ihKSvop9ena
添加方法在Spring5框架(一)中有,就不赘述了。
- 导入这9个jar包后与上面的两个注解添加方式相同
- 如下两种方式,分别表示按类型和名字匹配
//@Resource
@Resource(name="service")
- 注入基本类型的属性
@Value(value = "24")
private int age;
完全注解开发
- 创建配置类,替代xml配置文件
@Configuration
public class SpringConfig {
}
- 开启组件扫描
@Configuration
@ComponentScan(basePackages = "com.config.spring5")
public class SpringConfig {
}
- 处理测试方法不同,其他添加注释创建对象和输入属性的操作都相同
- 测试方法
Flower类
@Component(value = "flower")
public class Flower {
@Value(value = "rong")
private String name;
public void play(){
System.out.println(this.name+"玩得很开心");
}
}
Grass类
@Component(value = "grass")
public class Grass {
@Value(value = "ning")
private String name;
@Autowired
@Qualifier(value = "flower")
private Flower flower;
public void miss(){
System.out.println("我真的真的好想你");
}
public void seeYou(){
flower.play();
}
}
测试方法
@Test
public void testAnnatationConfig(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
Grass grass = context.getBean("grass", Grass.class);
grass.miss();
grass.seeYou();
}