当前位置: 首页 > news >正文

SSM框架,Spring-ioc的学习(下)

拓展:在xml文件中读取外部配置文件

例:若要导入外部配置文件jdbc.properties

<?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:property-placeholder location="classpath:jdbc.properties,classpath:其他配置文件,……" /><!-- 配置数据源 --><bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="${atguigu.url}"/><property name="driverClassName" value="${atguigu.driver}"/><property name="username" value="${atguigu.username}"/><property name="password" value="${atguigu.password}"/></bean><!-- 配置 JdbcTemplate --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><!-- 装配数据源 --><property name="dataSource" ref="druidDataSource"/></bean>
</beans>

使用注解的方式进行配置bean

关于注解

注解与xml配置文件一样,注解本身不能运行,注解本身只做一个标记,注解的功能是框架检测到注解标记的位置,然后针对这个位置按照注解标记的功能来执行具体的操作。即一切的操作都是Java代码执行的,xml和注解只是告诉框架中的代码如何执行

关于扫描

Spring为了知道那些地方有那些注解,就需要通过扫描的方式,来进行检测。然后根据扫描的方式进行后续操作。

组件的注解配置

Spring的以下注解,将其标注在Java类上,将它们定义成Spring的Bean

注解说明
@Component该注解用于描述 Spring 中的 Bean,它是一个泛化的概念,仅仅表示容器中的一个组件(Bean),并且可以作用在应用的任何层次,例如 Service 层、Dao 层等。 使用时只需将该注解标注在相应类上即可。
@Repository该注解用于将数据访问层(Dao 层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
@Service该注解通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
@Controller该注解通常作用在控制层(如SpringMVC 的 Controller),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。

在类上使用以上注解后,相应的bean的id值默认为首字母小写后的类名,class就是此类的路径

@Component和三个注解(@Repository、@Service、@Controller)对于组件管理来说没有区别,只是起了三个新的名字,以便于区别组件

配置文件确定扫描范围

配置自动扫描的包

使用 context:component-scan 标签,在其base-package属性中写上要自动扫描的包,例:

<?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>"><!-- 配置自动扫描的包 --><!-- 1.包要精准,提高性能!2.会扫描指定的包和子包内容3.多个包可以使用,分割 例如: com.atguigu.controller,com.atguigu.service等--><context:component-scan base-package="com.atguigu.components"/></beans>
  • 自动扫描包时也会扫描其所有的子包
  • 可以指定多个包,多个包之间使用逗号分割

排除自动扫描的范围

将context:component-scan变为双标签,在其中使用context:exclude-filter标签进行排除自动扫描的范围,然后指定其type属性和exrpession属性

  • type属性:指定排除的方式,annotation表示根据注解方式排除
  • expression属性:指定排除规则的表达式,对于注解来说,写入指定的类的路径名即可

例:

<!-- 情况三:指定不扫描的组件 -->
<context:component-scan base-package="com.atguigu.components"><!-- context:exclude-filter标签:指定排除规则 --><!-- type属性:指定根据什么来进行排除,annotation取值表示根据注解来排除 --><!-- expression属性:指定排除规则的表达式,对于注解来说指定全类名即可 --><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

锁定自动扫描的范围

要指定包中的某个注解,只扫描它,就要锁定其范围

将context:component-scan变为双标签,在context:component-scan标签中加上use-default-filters属性,将属性值为false。让其指定包的所有注解先不生效。

在context:component-scan双标签中使用context:include-filter标签进行锁定自动扫描的范围,然后指定其type属性和exrpession属性

  • type属性:指定锁定的方式,annotation表示根据注解方式锁定
  • expression属性:指定锁定规则的表达式,对于注解来说,写入指定的类的路径名即可

关于组件bean的id值

  • 若不指定,则默认id值为将首字母小写后的类名
  • 可以在注解后加上括号,在其中指定其value属性,value属性的值就是组件的id值
  • 若注解后的括号中只对value属性进行赋值,则可以省略value=,直接写value值的字符串
@Controller(value = "tianDog")
public class SoldierController {
}
@Service("smallDog")
public class SoldierService {
}

周期方法和作用域的注解

周期方法注解

周期方法的要求同样是:权限修饰符为public,返回值类型为void,无形参列表

  • 在初始化方法上加上注解:@PostConstruct
  • 在销毁方法上写上注解:@PreDestory
public class BeanOne {//周期方法要求: 方法命名随意,但是要求方法必须是 public void 无形参列表@PostConstruct  //注解制指定初始化方法public void init() {// 初始化逻辑}
}
public class BeanTwo {@PreDestroy //注解指定销毁方法public void cleanup() {// 释放资源逻辑}
}

注:销毁方法要正常关闭ioc才会执行,即将获取到ioc使用close方法关闭,单例的bean在ioc容器close时会自动销毁并调用销毁方法,多例的bean在ioc容器close时不会自动销毁不会调用销毁方法

作用域配置注解

作用域知识点见Spring-ioc的学习(上)

在类上加上Scope注解,在Scope注解的括号内指定其scopeName属性

  • 单例为scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON
  • scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE

di注入的注解

自动装配注解:@Autowired

用法:直接在成员变量上标记此注解,也可以在构造器、set方法上标记,以注入组件对象

此注解的功能是:先在ioc容器中查找符合类型的组件对象,再将查找到的组件对象赋值给当前属性,以完成组件注入的功能

例:

@Service("smallDog")
public class SoldierService {
//在成员变量上标记@Autowiredprivate SoldierDao soldierDao;public void getMessage() {soldierDao.getMessage();}
}
@Controller(value = "tianDog")
public class SoldierController {private SoldierService soldierService;
//在构造器上标注@Autowiredpublic SoldierController(SoldierService soldierService) {this.soldierService = soldierService;}……
}
@Controller(value = "tianDog")
public class SoldierController {private SoldierService soldierService;//在set方法上标注@Autowiredpublic void setSoldierService(SoldierService soldierService) {this.soldierService = soldierService;}……
}

注:参与自动装配的组件都需要在ioc容器中

该注解执行步骤:

  • 首先根据所需要的组件类型到 IOC 容器中查找
    • 能够找到唯一的 bean:直接执行装配
    • 如果完全找不到匹配这个类型的 bean:装配失败
    • 和所需类型匹配的 bean 不止一个
      • 没有 @Qualifier 注解:根据 @Autowired 标记位置成员变量的变量名作为 bean 的 id 进行匹配
        • 能够找到:执行装配
        • 找不到:装配失败
      • 使用 @Qualifier 注解:根据 @Qualifier 注解中指定的名称作为 bean 的id进行匹配
        • 能够找到:执行装配
        • 找不到:装配失败

佛系装配(不推荐使用):给 @Autowired 注解设置 required = false 属性,即给对应的要注入的变量赋值为null值

关于@Qualifier的使用

在相应的要注入的变量上标记@Autowired注解的同时,加上@Qualifier注解。在@Qualitier注解内写上value属性,value属性的值就是要找的组件的id的值。

例:

@Controller(value = "tianDog")
public class SoldierController {@Autowired@Qualifier(value = "maomiService222")//会根据id值为maomiService222的组件注入// 根据面向接口编程思想,使用接口类型引入Service组件private ISoldierService soldierService;

注:@Qualifier注解不能单独使用,要配合Autowired注解使用

还可以使用@Resource(name=”……”)注解,就是@Qualifier和Autowired注解的组合体,但是项目要导入jsr-250依赖,依赖导入如下:

<dependency><groupId>jakarta.annotation</groupId><artifactId>jakarta.annotation-api</artifactId><version>2.1.1</version>
</dependency>

注入基本类型数据

注入基本类型数据的步骤是,在要注入的变量上方写Value注解,直接在括号中写上其要注入的值即可(用字符串形式)

@Value("liergou")
private String name;
@Value("19")
private String age;

使用注解配置成员变量的优点是可以使用${……}的方式来注入properties配置文件的信息

此处导入properties文件的方式和文章开头的步骤一样:

<!-- 导入外部属性文件 --><context:property-placeholder location="classpath:jdbc.properties,classpath:其他配置文件,……" />
@Component
public class CommonComponent {/*** 情况1: ${key} 取外部配置key对应的值!* 情况2: ${key:defaultValue} 没有key,可以给与默认值*/@Value("${catalog:hahaha}")private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}

在${ }中的配置文件中的变量后加上 : ,意思是若变量不存在,就默认为:后面的值

FactoryBean的注解配置

FactoryBean类内部的代码还是照常,只要在此类上方加上@Component、@Controller、@Service、@Repository注解即可

@Component
public class JavaBeanFactoryBean implements FactoryBean<JavaBean> {@Overridepublic JavaBean getObject() throws Exception {JavaBean javaBean = new JavaBean();return javaBean;}@Overridepublic Class<?> getObjectType() {return JavaBean.class;}
}

配置类方式管理Bean

Spring 完全注解配置(Fully Annotation-based Configuration)是指通过 Java配置类 代码来配置 Spring 应用程序,使用注解来替代原本在 XML 配置文件中的配置。相对于 XML 配置,完全注解配置具有更强的类型安全性和更好的可读性。

步骤

  1. 在配置类上添加注解@Configuration,代表此类是配置类
  2. 使用@ComponentScan注解,完成包扫描注解配置,在其中的value属性或basepackages属性中指定包路径,如果有多个包路径,使用{ }括号将所有包路径括起来,包路径之间用逗号隔开
  3. 使用@PropertySource注解,引用外部的配置文件,在value属性中指定配置文件的路径即可,可以有多个,格式为value=”classpath: …… , classpath: …… , ……”
  4. 声明第三方依赖的Bean组件

例:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;//标注当前类是配置类,替代application.xml    
@Configuration
//使用注解读取外部配置,替代 <context:property-placeholder标签
@PropertySource("classpath:application.properties,classpath:jdbc.properties")
//使用@ComponentScan注解,可以配置扫描包,替代<context:component-scan标签
@ComponentScan(basePackages = {"com.ergou.components","com.ergou.service"})
public class MyConfiguration {}

创建ioc容器:

// AnnotationConfigApplicationContext 根据配置类创建 IOC 容器对象
ApplicationContext iocContainerAnnotation =
new AnnotationConfigApplicationContext(MyConfiguration.class);

// AnnotationConfigApplicationContext-IOC容器对象
ApplicationContext iocContainerAnnotation =
new AnnotationConfigApplicationContext();
//外部设置配置类
iocContainerAnnotation.register(MyConfiguration.class);
//刷新后方可生效!!
iocContainerAnnotation.refresh();

@Bean注解的使用

步骤:

  1. 方法的返回值类型为目标组件的的类型或其类型的接口、父类的类型
  2. 方法名为bean组件的id值
  3. 在方法体中自定义实例化过程即可
  4. 在方法上加上@Bean注解

例:

//标注当前类是配置类,替代application.xml
@Configuration
//使用注解读取外部配置,替代 <context:property-placeholder标签
@PropertySource("classpath:jdbc.properties")
//使用@ComponentScan注解,可以配置扫描包,替代<context:component-scan标签
@ComponentScan(basePackages = {"com.ergou.ioc_01"})
public class JavaConfiguration {@Value("${ergou.url}")private String url;@Value("${ergou.driver}")private String driver;@Value("${ergou.username}")private String username;@Value("${ergou.password}")private String password;
/***方法的返回值类型为目标组件的的类型或其类型的接口、父类的类型
*方法名为bean组件的id值
*在方法体中自定义实例化过程即可
*在方法上加上@Bean注解
* @return*/
@Beanpublic DruidDataSource dataSource(){DruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setUrl(url);druidDataSource.setDriverClassName(driver);druidDataSource.setUsername(username);druidDataSource.setPassword(password);return druidDataSource;}
}

关于bean的id值

默认为当前方法的方法名

手动指定id值:在@Bean注解中加上value属性(或name属性),在其中指定id值即可

@Configuration
public class AppConfig {@Bean("myThing") //指定名称public Thing thing() {return new Thing();}
}

关于周期方法

在配置类中写相应的初始化方法和销毁方法,然后在@Bean注解中,initMethod属性中写初始化方法的方法名,在destoryMethod属性中写销毁方法的方法名。

例:

public class BeanOne {public void init() {// initialization logic}
}public class BeanTwo {public void cleanup() {// destruction logic}
}@Configuration
public class AppConfig {@Bean(initMethod = "init")public BeanOne beanOne() {return new BeanOne();}@Bean(destroyMethod = "cleanup")public BeanTwo beanTwo() {return new BeanTwo();}
}

关于作用域

在对应的bean的方法上加上Scope注解,在Scope注解的括号内指定其scopeName属性

  • 单例为scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON
  • scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE
@Bean@Scope(scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON)public DruidDataSource dataSource(){DruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setUrl(url);druidDataSource.setDriverClassName(driver);druidDataSource.setUsername(username);druidDataSource.setPassword(password);return druidDataSource;}

调用其他的组件

  • 如果要调用的组件也是@Bean注解的方法配置的bean组件,则可以直接调用其bean对应的@Bean注解后的方法
@Bean
public JdbcTemplate jdbcTemplate(){JdbcTemplate jdbcTemplate = new JdbcTemplate();//调用了上面的DataSource类型的组件jdbcTemplate.setDataSource(dataSource());return jdbcTemplate;
}
  • 在@Bean注解的方法的形参列表中写上一个想要调用的的组件类型的变量,方法体中调用时调用的就是相应类型的组件。(可以有多个)
@Bean
//形参为DataSource类型的变量,变量名为dataSource,意思是方法体中调用dataSource变量,调用的就是DataSource类型的组件
public JdbcTemplate jdbcTemplate(DataSource dataSource){JdbcTemplate jdbcTemplate = new JdbcTemplate();
//调用dataSource组件
jdbcTemplate.setDataSource(dataSource);return jdbcTemplate;
}

如果对应类型有多个组件,则选择组件id值与变量名一致的组件(要保证要有一个组件的id值与变量名一致)

@Import注解的使用

@Import注解用来从另一个配置类中加载@bean定义的bean组件,在@Import注解后的括号中写相应类的class实例即可

@Configuration
public class ConfigA {@Beanpublic A a() {return new A();}
}
@Configuration
@Import(ConfigA.class)
public class ConfigB {@Beanpublic B b() {return new B();}
}

也可以传入多个外部的配置类,用{}括起来,例:

@Configuration
@Import({JavaConfiguration.class,JavaConfigurationB.class})
public class JavaConfigurationA {
}

整合Spring5-Test5搭建测试环境

  1. 整合测试环境作用

    好处1:不需要自己创建IOC容器对象了

    好处2:任何需要的bean都可以在测试类中直接享受自动装配

  2. 导入相关依赖

    <!--junit5测试-->
    <dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.3.1</version>
    </dependency>
    <dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>6.0.6</version><scope>test</scope>
    </dependency>
    
  3. 整合测试注解使用

    //@SpringJUnitConfig(locations = {"classpath:spring-context.xml"})  //指定配置文件xml
    @SpringJUnitConfig(value = {BeanConfig.class})  //指定配置类
    public class Junit5IntegrationTest {@Autowiredprivate User user;@Testpublic void testJunit5() {System.out.println(user);}
    }
    

相关文章:

  • 力扣热题100_双指针_15_三数之和
  • React18原理: React核心对象之ReactElement对象和Fiber对象
  • Paper - CombFold: predicting structures of large protein assemblies 论文简读
  • 函数 栈帧
  • Python 修改window桌面背景图片
  • 算法训练营day28(补), 贪心算法2
  • node.js后端+小程序前端+mongoDB(增删改查)
  • 【机器学习基础】决策树(Decision Tree)
  • Qt网络编程-TCP与UDP
  • 寒假学习记录15:Node(网络)
  • 代码随想录day27 Java版
  • 可变参数(c/c++)
  • 【MySQL】表的增删改查(进阶)
  • 每日五道java面试题之java基础篇(五)
  • C++多重继承
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • 【402天】跃迁之路——程序员高效学习方法论探索系列(实验阶段159-2018.03.14)...
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • angular2开源库收集
  • Angularjs之国际化
  • CSS 专业技巧
  • es6--symbol
  • JAVA_NIO系列——Channel和Buffer详解
  • JS 面试题总结
  • Map集合、散列表、红黑树介绍
  • maya建模与骨骼动画快速实现人工鱼
  • PaddlePaddle-GitHub的正确打开姿势
  • Promise初体验
  • Python - 闭包Closure
  • react-core-image-upload 一款轻量级图片上传裁剪插件
  • Redis的resp协议
  • RxJS: 简单入门
  • select2 取值 遍历 设置默认值
  • SQL 难点解决:记录的引用
  • tab.js分享及浏览器兼容性问题汇总
  • vue的全局变量和全局拦截请求器
  • 每天10道Java面试题,跟我走,offer有!
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 它承受着该等级不该有的简单, leetcode 564 寻找最近的回文数
  • 线性表及其算法(java实现)
  • 用quicker-worker.js轻松跑一个大数据遍历
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 字符串匹配基础上
  • Linux权限管理(week1_day5)--技术流ken
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ​决定德拉瓦州地区版图的关键历史事件
  • # 数论-逆元
  • ###项目技术发展史
  • #WEB前端(HTML属性)
  • (1) caustics\
  • (2.2w字)前端单元测试之Jest详解篇
  • (4)Elastix图像配准:3D图像
  • (三)Hyperledger Fabric 1.1安装部署-chaincode测试
  • (实战篇)如何缓存数据
  • (一)RocketMQ初步认识