Java:基于注解的Spring使用【IOC容器】
第九章 关于Spring IOC容器的个人理解
9.1 Spring IOC容器 理解参考
Spring IOC容器 可以用Vuex进行参考。
- IOC容器,就是装各个对象的。
- Vuex,就是进行状态管理的。
获取装配对象,其实类似于前端的获取组件对象,
- 有个组件对象,通过组件对象调用组件的方法
- 获取装配对象,通过装配对象调用对象的方法。
9.2 本章主要内容
-
将对象装配到IOC容器中(
注解
)@Component
:装配普通组件到IOC容器@Repository
:装配持久化层组件到IOC容器@Service
:装配业务逻辑层组件到IOC容器@Controller
:装配控制层|表示层组件到IOC容器
-
装配对象中的属性(可以理解为:
初始化对象中属性的值
)@Autowired
:自动装配对象中属性【非字面量数值】@Qualifier
:配合@Autowired一起使用,将设置beanId名称装配到属性中@Value
:装配对象中属性【字面量数值】
-
组件扫描
- 默认使用情况
- 包含扫描
- 排除扫描
-
零配置注解开发
- 创建配置类
- 在class上面添加注解
@Configuration
:标识当前类是一个配置类,作用:代替XML配置文件@ComponentScan
:设置组件扫描当前包及其子包
- 使用
AnnotationConfigApplicationContext
容器对象
-
自己的bean,可以用注解
, -
第三方的bean,没法用注解,只能用xml配置
第十章 Spring中注解【非常重要】
10.1 使用注解将对象装配到IOC容器中
约定:约束>配置【注解>XML】>代码
位置:在类上面标识
注意:
- Spring本身不区分四个注解【四个注解本质是一样的@Component】,提供四个注解的目的只有一个:提高代码的可读性
- 只用注解装配对象,默认将类名首字母小写作为beanId
- 可以使用value属性,设置beanId;当注解中只使用一个value属性时,value关键字可省略
-
装配对象四个注解
- @Component:装配普通组件到IOC容器
- @Repository:装配持久化层组件到IOC容器
- @Service:装配业务逻辑层组件到IOC容器
- @Controller:装配控制层|表示层组件到IOC容器
-
使用注解步骤
-
导入相关jar包【已导入】
-
开启组件扫描
<!-- 开启组件扫描 base-package:设置扫描注解包名【当前包及其子包】 --> <context:component-scan base-package="com.atguigu"></context:component-scan>
-
使用注解标识组件
-
10.2 使用注解装配对象中属性【自动装配】
-
@Autowired注解
-
作用:自动装配对象中属性
-
装配原理:反射机制
-
装配方式
-
先按照byType进行匹配
-
匹配1个:匹配成功,正常使用
-
匹配0个:
-
默认【@Autowired(required=true)】报错
/*expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} */
-
@Autowired(required=false),不会报错
-
-
匹配多个
-
再按照byName进行唯一筛选
-
筛选成功【对象中属性名称==beanId】,正常使用
-
筛选失败【对象中属性名称!=beanId】,报如下错误:
//expected single matching bean but found 2: deptDao,deptDao2
-
-
-
-
-
@Autowired中required属性
- true:表示被标识的属性必须装配数值,如未装配,会报错。
- false:表示被标识的属性不必须装配数值,如未装配,不会报错。
-
-
@Qualifier注解
- 作用:配合@Autowired一起使用,将设置beanId名称装配到属性中
- 注意:不能单独使用,需要与@Autowired一起使用
-
@Value注解
- 作用:装配对象中属性【字面量数值】
第十一章 Spring中组件扫描
11.1 默认使用情况
<!-- 开启组件扫描
base-package:设置扫描注解包名【当前包及其子包】
-->
<context:component-scan base-package="com.atguigu"></context:component-scan>
11.2 包含扫描
- 注意:
- 使用包含扫描之前,必须设置use-default-filters=“false”【关闭当前包及其子包的扫描】
- type
- annotation:设置被扫描注解的全类名
- assignable:设置被扫描实现类的全类名
<context:component-scan base-package="com.atguigu" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:include-filter type="assignable" expression="com.atguigu.service.impl.DeptServiceImpl"/>
</context:component-scan>
11.3 排除扫描
<!-- 【排除扫描】 假设:环境中共有100包,不想扫描2/100-->
<context:component-scan base-package="com.atguigu">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<!-- <context:exclude-filter type="assignable" expression="com.atguigu.controller.DeptController"/>-->
</context:component-scan>
第十三章 Spring完全注解开发【0配置】
13.1 完全注解开发步骤
- 创建配置类
- 在class上面添加注解
@Configuration
:标识当前类是一个配置类,作用:代替XML配置文件@ComponentScan
:设置组件扫描当前包及其子包
- 使用
AnnotationConfigApplicationContext
容器对象
13.2 示例代码
// com/atguigu/spring/config/SpringConfig.java
/**
* @author Chunsheng Zhang 尚硅谷
* @create 2022/3/28 14:05
*/
@Configuration
@ComponentScan(basePackages = "com.atguigu.spring")
public class SpringConfig {
}
// src/test/java/Test0Xml.java
@Test
public void test0Xml(){
//创建容器对象
// ApplicationContext context =
// new ClassPathXmlApplicationContext("applicationContext.xml");
//使用AnnotationConfigApplicationContext容器对象
ApplicationContext context =
new AnnotationConfigApplicationContext(SpringConfig.class);
DeptDaoImpl deptDao = context.getBean("deptDao", DeptDaoImpl.class);
System.out.println("deptDao = " + deptDao);
}
第十四章 Spring集成Junit4
14.1 集成步骤
- 导入jar包
spring-test-5.3.1.jar
<!--spring集成junit4--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.1</version> <scope>test</scope> </dependency>
- 指定Spring的配置文件的路径
- 【
@ContextConfiguration
】
- 【
- 指定Spring环境下运行Junit4的运行器
@RunWith
14.2 示例代码
报了空指针,不知道啥原因
jdk改成1.8之后可以正常执行
/**
* @author Chunsheng Zhang 尚硅谷
* @create 2022/3/28 14:12
*/
@ContextConfiguration(locations = "classpath:applicationContext_annotation.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class TestSpringJunit4 {
@Autowired
private DeptService deptService;
@Test
public void testService(){
//创建容器对象
// ApplicationContext context =
// new ClassPathXmlApplicationContext("applicationContext.xml");
// DeptService deptService = context.getBean("deptService", DeptServiceImpl.class);
deptService.saveDept(new Dept());
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/84928.html