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

博客网站开发利用数据库成都保洁公司

博客网站开发利用数据库,成都保洁公司,做五金奖牌进什么网站,手表怎么在网站做推广IOC容器 1、IoC容器1.1、控制反转#xff08;IoC#xff09;1.2、依赖注入1.3、IoC容器在Spring的实现 2、基于XML管理Bean2.1、搭建子模块spring6-ioc-xml2.2、实验一#xff1a;获取bean①方式一#xff1a;根据id获取②方式二#xff1a;根据类型获取③方式三#xff… IOC容器 1、IoC容器1.1、控制反转IoC1.2、依赖注入1.3、IoC容器在Spring的实现 2、基于XML管理Bean2.1、搭建子模块spring6-ioc-xml2.2、实验一获取bean①方式一根据id获取②方式二根据类型获取③方式三根据id和类型④注意的地方⑤扩展知识 2.3、实验二依赖注入之setter注入2.4、实验三依赖注入之构造器注入2.5、实验四特殊值处理①字面量赋值②null值③xml实体④CDATA节 2.6、实验五为对象类型属性赋值方式一引用外部bean方式二内部bean方式三级联属性赋值 2.7、实验六为数组类型属性赋值2.8、实验七为集合类型属性赋值①为List集合类型属性赋值②为Map集合类型属性赋值③引用集合类型的bean 2.9、实验八p命名空间2.10、实验九引入外部属性文件2.11、实验十bean的作用域2.12、实验十一bean生命周期2.13、实验十二FactoryBean2.14、实验十三基于xml自动装配 3、基于注解管理Bean☆3.1、搭建子模块spring6-ioc-annotation3.2、开启组件扫描3.3、使用注解定义 Bean3.4、实验一Autowired注入①场景一属性注入②场景二set注入③场景三构造方法注入④场景四形参上注入⑤场景五只有一个构造函数无注解⑥场景六Autowired注解和Qualifier注解联合 3.5、实验二Resource注入①场景一根据name注入②场景二name未知注入③场景三 其他情况 3.6、Spring全注解开发 IOC 是 Inversion of Control 的简写译为“控制反转”它不是一门技术而是一种设计思想是一个重要的面向对象编程法则能够指导我们如何设计出松耦合、更优良的程序。 Spring 通过 IoC 容器来管理所有 Java 对象的实例化和初始化控制对象与对象之间的依赖关系。我们将由 IoC 容器管理的 Java 对象称为 Spring Bean它与使用关键字 new 创建的 Java 对象没有任何区别。 IoC 容器是 Spring 框架中最重要的核心组件之一它贯穿了 Spring 从诞生到成长的整个过程。 1、IoC容器 1.1、控制反转IoC 控制反转是一种思想。 控制反转是为了降低程序耦合度提高程序扩展力。 控制反转反转的是什么 将对象的创建权利交出去交给第三方容器负责。将对象和对象之间关系的维护权交出去交给第三方容器负责。 控制反转这种思想如何实现呢 DIDependency Injection依赖注入 1.2、依赖注入 DIDependency Injection依赖注入依赖注入实现了控制反转的思想。 依赖注入 指Spring创建对象的过程中将对象依赖属性通过配置进行注入 依赖注入常见的实现方式包括两种 第一种set注入第二种构造注入 所以结论是IOC 就是一种控制反转的思想 而 DI 是对IoC的一种具体实现。 Bean管理说的是Bean对象的创建以及Bean对象中属性的赋值或者叫做Bean对象之间关系的维护。 1.3、IoC容器在Spring的实现 Spring 的 IoC 容器就是 IoC思想的一个落地的产品实现。IoC容器中管理的组件也叫做 bean。在创建 bean 之前首先需要创建IoC 容器。Spring 提供了IoC 容器的两种实现方式 ①BeanFactory 这是 IoC 容器的基本实现是 Spring 内部使用的接口。面向 Spring 本身不提供给开发人员使用。 ②ApplicationContext BeanFactory 的子接口提供了更多高级特性。面向 Spring 的使用者几乎所有场合都使用 ApplicationContext 而不是底层的 BeanFactory。 ③ApplicationContext的主要实现类 类型名简介ClassPathXmlApplicationContext通过读取类路径下的 XML 格式的配置文件创建 IOC 容器对象FileSystemXmlApplicationContext通过文件系统路径读取 XML 格式的配置文件创建 IOC 容器对象ConfigurableApplicationContextApplicationContext 的子接口包含一些扩展方法 refresh() 和 close() 让 ApplicationContext 具有启动、关闭和刷新上下文的能力。WebApplicationContext专门为 Web 应用准备基于 Web 环境创建 IOC 容器对象并将对象引入存入 ServletContext 域中。 2、基于XML管理Bean 2.1、搭建子模块spring6-ioc-xml ①搭建模块 搭建方式如spring-first ②引入配置文件 引入spring-first模块配置文件beans.xml、log4j2.xml ③添加依赖 dependencies!--spring context依赖--!--当你引入Spring Context依赖之后表示将Spring的基础依赖引入了--dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion6.0.3/version/dependency!--junit5测试--dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-api/artifactIdversion5.3.1/version/dependency!--log4j2的依赖--dependencygroupIdorg.apache.logging.log4j/groupIdartifactIdlog4j-core/artifactIdversion2.19.0/version/dependencydependencygroupIdorg.apache.logging.log4j/groupIdartifactIdlog4j-slf4j2-impl/artifactIdversion2.19.0/version/dependency /dependencies④引入java类 引入spring-first模块java及test目录下实体类 package com.atguigu.spring6.bean;public class HelloWorld {public HelloWorld() {System.out.println(无参数构造方法执行);}public void sayHello(){System.out.println(helloworld);} } package com.atguigu.spring6.bean;import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloWorldTest {private Logger logger LoggerFactory.getLogger(HelloWorldTest.class);Testpublic void testHelloWorld(){} }2.2、实验一获取bean ①方式一根据id获取 由于 id 属性指定了 bean 的唯一标识所以根据 bean 标签的 id 属性可以精确获取到一个组件对象。上个实验中我们使用的就是这种方式。 ②方式二根据类型获取 Test public void testHelloWorld1(){ApplicationContext ac new ClassPathXmlApplicationContext(beans.xml);HelloWorld bean ac.getBean(HelloWorld.class);bean.sayHello(); }③方式三根据id和类型 Test public void testHelloWorld2(){ApplicationContext ac new ClassPathXmlApplicationContext(beans.xml);HelloWorld bean ac.getBean(helloworld, HelloWorld.class);bean.sayHello(); }④注意的地方 当根据类型获取bean时要求IOC容器中指定类型的bean有且只能有一个 当IOC容器中一共配置了两个 bean idhelloworldOne classcom.atguigu.spring6.bean.HelloWorld/bean bean idhelloworldTwo classcom.atguigu.spring6.bean.HelloWorld/bean根据类型获取时会抛出异常 org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ‘com.atguigu.spring6.bean.HelloWorld’ available: expected single matching bean but found 2: helloworldOne,helloworldTwo ⑤扩展知识 如果组件类实现了接口根据接口类型可以获取 bean 吗 可以前提是bean唯一 如果一个接口有多个实现类这些实现类都配置了 bean根据接口类型可以获取 bean 吗 不行因为bean不唯一 结论 根据类型来获取bean时在满足bean唯一性的前提下其实只是看『对象 instanceof 指定的类型』的返回结果只要返回的是true就可以认定为和类型匹配能够获取到。 java中instanceof运算符用于判断前面的对象是否是后面的类或其子类、实现类的实例。如果是返回true否则返回false。也就是说用instanceof关键字做判断时 instanceof 操作符的左右操作必须有继承或实现关系 2.3、实验二依赖注入之setter注入 ①创建学生类Student package com.atguigu.spring6.bean;public class Student {private Integer id;private String name;private Integer age;private String sex;public Student() {}public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex sex;}Overridepublic String toString() {return Student{ id id , name name \ , age age , sex sex \ };}}②配置bean时为属性赋值 spring-di.xml bean idstudentOne classcom.atguigu.spring6.bean.Student!-- property标签通过组件类的setXxx()方法给组件对象设置属性 --!-- name属性指定属性名这个属性名是getXxx()、setXxx()方法定义的和成员变量无关 --!-- value属性指定属性值 --property nameid value1001/propertyproperty namename value张三/propertyproperty nameage value23/propertyproperty namesex value男/property /bean③测试 Test public void testDIBySet(){ApplicationContext ac new ClassPathXmlApplicationContext(spring-di.xml);Student studentOne ac.getBean(studentOne, Student.class);System.out.println(studentOne); }2.4、实验三依赖注入之构造器注入 ①在Student类中添加有参构造 public Student(Integer id, String name, Integer age, String sex) {this.id id;this.name name;this.age age;this.sex sex; }②配置bean spring-di.xml bean idstudentTwo classcom.atguigu.spring6.bean.Studentconstructor-arg value1002/constructor-argconstructor-arg value李四/constructor-argconstructor-arg value33/constructor-argconstructor-arg value女/constructor-arg /bean注意 constructor-arg标签还有两个属性可以进一步描述构造器参数 index属性指定参数所在位置的索引从0开始name属性指定参数名 ③测试 Test public void testDIByConstructor(){ApplicationContext ac new ClassPathXmlApplicationContext(spring-di.xml);Student studentOne ac.getBean(studentTwo, Student.class);System.out.println(studentOne); }2.5、实验四特殊值处理 ①字面量赋值 什么是字面量 int a 10; 声明一个变量a初始化为10此时a就不代表字母a了而是作为一个变量的名字。当我们引用a的时候我们实际上拿到的值是10。 而如果a是带引号的‘a’那么它现在不是一个变量它就是代表a这个字母本身这就是字面量。所以字面量没有引申含义就是我们看到的这个数据本身。 !-- 使用value属性给bean的属性赋值时Spring会把value属性的值看做字面量 -- property namename value张三/②null值 property namenamenull / /property注意 property namename valuenull/property以上写法为name所赋的值是字符串null ③xml实体 !-- 小于号在XML文档中用来定义标签的开始不能随便使用 -- !-- 解决方案一使用XML实体来代替 -- property nameexpression valuea lt; b/④CDATA节 property nameexpression!-- 解决方案二使用CDATA节 --!-- CDATA中的C代表Character是文本、字符的含义CDATA就表示纯文本数据 --!-- XML解析器看到CDATA节就知道这里是纯文本就不会当作XML标签或属性来解析 --!-- 所以CDATA节中写什么符号都随意 --value![CDATA[a b]]/value /property2.6、实验五为对象类型属性赋值 ①创建班级类Clazz package com.atguigu.spring6.beanpublic class Clazz {private Integer clazzId;private String clazzName;public Integer getClazzId() {return clazzId;}public void setClazzId(Integer clazzId) {this.clazzId clazzId;}public String getClazzName() {return clazzName;}public void setClazzName(String clazzName) {this.clazzName clazzName;}Overridepublic String toString() {return Clazz{ clazzId clazzId , clazzName clazzName \ };}public Clazz() {}public Clazz(Integer clazzId, String clazzName) {this.clazzId clazzId;this.clazzName clazzName;} }②修改Student类 在Student类中添加以下代码 private Clazz clazz;public Clazz getClazz() {return clazz; }public void setClazz(Clazz clazz) {this.clazz clazz; }方式一引用外部bean 配置Clazz类型的bean bean idclazzOne classcom.atguigu.spring6.bean.Clazzproperty nameclazzId value1111/propertyproperty nameclazzName value财源滚滚班/property /bean为Student中的clazz属性赋值 bean idstudentFour classcom.atguigu.spring6.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/property!-- ref属性引用IOC容器中某个bean的id将所对应的bean为属性赋值 --property nameclazz refclazzOne/property /bean错误演示 bean idstudentFour classcom.atguigu.spring6.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/propertyproperty nameclazz valueclazzOne/property /bean如果错把ref属性写成了value属性会抛出异常 Caused by: java.lang.IllegalStateException: Cannot convert value of type ‘java.lang.String’ to required type ‘com.atguigu.spring6.bean.Clazz’ for property ‘clazz’: no matching editors or conversion strategy found 意思是不能把String类型转换成我们要的Clazz类型说明我们使用value属性时Spring只把这个属性看做一个普通的字符串不会认为这是一个bean的id更不会根据它去找到bean来赋值 方式二内部bean bean idstudentFour classcom.atguigu.spring6.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/propertyproperty nameclazz!-- 在一个bean中再声明一个bean就是内部bean --!-- 内部bean只能用于给属性赋值不能在外部通过IOC容器获取因此可以省略id属性 --bean idclazzInner classcom.atguigu.spring6.bean.Clazzproperty nameclazzId value2222/propertyproperty nameclazzName value远大前程班/property/bean/property /bean方式三级联属性赋值 bean idstudentFour classcom.atguigu.spring6.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/propertyproperty nameclazz refclazzOne/propertyproperty nameclazz.clazzId value3333/propertyproperty nameclazz.clazzName value最强王者班/property /bean2.7、实验六为数组类型属性赋值 ①修改Student类 在Student类中添加以下代码 private String[] hobbies;public String[] getHobbies() {return hobbies; }public void setHobbies(String[] hobbies) {this.hobbies hobbies; }②配置bean bean idstudentFour classcom.atguigu.spring.bean6.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/property!-- ref属性引用IOC容器中某个bean的id将所对应的bean为属性赋值 --property nameclazz refclazzOne/propertyproperty namehobbiesarrayvalue抽烟/valuevalue喝酒/valuevalue烫头/value/array/property /bean2.8、实验七为集合类型属性赋值 ①为List集合类型属性赋值 在Clazz类中添加以下代码 private ListStudent students;public ListStudent getStudents() {return students; }public void setStudents(ListStudent students) {this.students students; }配置bean bean idclazzTwo classcom.atguigu.spring6.bean.Clazzproperty nameclazzId value4444/propertyproperty nameclazzName valueJavaee0222/propertyproperty namestudentslistref beanstudentOne/refref beanstudentTwo/refref beanstudentThree/ref/list/property /bean若为Set集合类型属性赋值只需要将其中的list标签改为set标签即可 ②为Map集合类型属性赋值 创建教师类Teacher package com.atguigu.spring6.bean; public class Teacher {private Integer teacherId;private String teacherName;public Integer getTeacherId() {return teacherId;}public void setTeacherId(Integer teacherId) {this.teacherId teacherId;}public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName teacherName;}public Teacher(Integer teacherId, String teacherName) {this.teacherId teacherId;this.teacherName teacherName;}public Teacher() {}Overridepublic String toString() {return Teacher{ teacherId teacherId , teacherName teacherName \ };} }在Student类中添加以下代码 private MapString, Teacher teacherMap;public MapString, Teacher getTeacherMap() {return teacherMap; }public void setTeacherMap(MapString, Teacher teacherMap) {this.teacherMap teacherMap; }配置bean bean idteacherOne classcom.atguigu.spring6.bean.Teacherproperty nameteacherId value10010/propertyproperty nameteacherName value大宝/property /beanbean idteacherTwo classcom.atguigu.spring6.bean.Teacherproperty nameteacherId value10086/propertyproperty nameteacherName value二宝/property /beanbean idstudentFour classcom.atguigu.spring6.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/property!-- ref属性引用IOC容器中某个bean的id将所对应的bean为属性赋值 --property nameclazz refclazzOne/propertyproperty namehobbiesarrayvalue抽烟/valuevalue喝酒/valuevalue烫头/value/array/propertyproperty nameteacherMapmapentrykeyvalue10010/value/keyref beanteacherOne/ref/entryentrykeyvalue10086/value/keyref beanteacherTwo/ref/entry/map/property /bean③引用集合类型的bean !--list集合类型的bean-- util:list idstudentsref beanstudentOne/refref beanstudentTwo/refref beanstudentThree/ref /util:list !--map集合类型的bean-- util:map idteacherMapentrykeyvalue10010/value/keyref beanteacherOne/ref/entryentrykeyvalue10086/value/keyref beanteacherTwo/ref/entry /util:map bean idclazzTwo classcom.atguigu.spring6.bean.Clazzproperty nameclazzId value4444/propertyproperty nameclazzName valueJavaee0222/propertyproperty namestudents refstudents/property /bean bean idstudentFour classcom.atguigu.spring6.bean.Studentproperty nameid value1004/propertyproperty namename value赵六/propertyproperty nameage value26/propertyproperty namesex value女/property!-- ref属性引用IOC容器中某个bean的id将所对应的bean为属性赋值 --property nameclazz refclazzOne/propertyproperty namehobbiesarrayvalue抽烟/valuevalue喝酒/valuevalue烫头/value/array/propertyproperty nameteacherMap refteacherMap/property /bean使用util:list、util:map标签必须引入相应的命名空间 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd2.9、实验八p命名空间 引入p命名空间 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:utilhttp://www.springframework.org/schema/utilxmlns:phttp://www.springframework.org/schema/pxsi:schemaLocationhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd引入p命名空间后可以通过以下方式为bean的各个属性赋值 bean idstudentSix classcom.atguigu.spring6.bean.Studentp:id1006 p:name小明 p:clazz-refclazzOne p:teacherMap-refteacherMap/bean2.10、实验九引入外部属性文件 ①加入依赖 !-- MySQL驱动 -- dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.30/version /dependency!-- 数据源 -- dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.2.15/version /dependency②创建外部属性文件 jdbc.userroot jdbc.passwordatguigu jdbc.urljdbc:mysql://localhost:3306/ssm?serverTimezoneUTC jdbc.drivercom.mysql.cj.jdbc.Driver③引入属性文件 引入context 名称空间 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd/beans!-- 引入外部属性文件 -- context:property-placeholder locationclasspath:jdbc.properties/注意在使用 context:property-placeholder 元素加载外包配置文件功能前首先需要在 XML 配置的一级标签 中添加 context 相关的约束。 ④配置bean bean iddruidDataSource classcom.alibaba.druid.pool.DruidDataSourceproperty nameurl value${jdbc.url}/property namedriverClassName value${jdbc.driver}/property nameusername value${jdbc.user}/property namepassword value${jdbc.password}/ /bean⑤测试 Test public void testDataSource() throws SQLException {ApplicationContext ac new ClassPathXmlApplicationContext(spring-datasource.xml);DataSource dataSource ac.getBean(DataSource.class);Connection connection dataSource.getConnection();System.out.println(connection); }2.11、实验十bean的作用域 ①概念 在Spring中可以通过配置bean标签的scope属性来指定bean的作用域范围各取值含义参加下表 取值含义创建对象的时机singleton默认在IOC容器中这个bean的对象始终为单实例IOC容器初始化时prototype这个bean在IOC容器中有多个实例获取bean时 如果是在WebApplicationContext环境下还会有另外几个作用域但不常用 取值含义request在一个请求范围内有效session在一个会话范围内有效 ②创建类User package com.atguigu.spring6.bean; public class User {private Integer id;private String username;private String password;private Integer age;public User() {}public User(Integer id, String username, String password, Integer age) {this.id id;this.username username;this.password password;this.age age;}public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}Overridepublic String toString() {return User{ id id , username username \ , password password \ , age age };} }③配置bean !-- scope属性取值singleton默认值bean在IOC容器中只有一个实例IOC容器初始化时创建对象 -- !-- scope属性取值prototypebean在IOC容器中可以有多个实例getBean()时创建对象 -- bean classcom.atguigu.spring6.bean.User scopeprototype/bean④测试 Test public void testBeanScope(){ApplicationContext ac new ClassPathXmlApplicationContext(spring-scope.xml);User user1 ac.getBean(User.class);User user2 ac.getBean(User.class);System.out.println(user1user2); }2.12、实验十一bean生命周期 ①具体的生命周期过程 bean对象创建调用无参构造器 给bean对象设置属性 bean的后置处理器初始化之前 bean对象初始化需在配置bean时指定初始化方法 bean的后置处理器初始化之后 bean对象就绪可以使用 bean对象销毁需在配置bean时指定销毁方法 IOC容器关闭 ②修改类User public class User {private Integer id;private String username;private String password;private Integer age;public User() {System.out.println(生命周期1、创建对象);}public User(Integer id, String username, String password, Integer age) {this.id id;this.username username;this.password password;this.age age;}public Integer getId() {return id;}public void setId(Integer id) {System.out.println(生命周期2、依赖注入);this.id id;}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public void initMethod(){System.out.println(生命周期3、初始化);}public void destroyMethod(){System.out.println(生命周期5、销毁);}Overridepublic String toString() {return User{ id id , username username \ , password password \ , age age };} }注意其中的initMethod()和destroyMethod()可以通过配置bean指定为初始化和销毁的方法 ③配置bean !-- 使用init-method属性指定初始化方法 -- !-- 使用destroy-method属性指定销毁方法 -- bean classcom.atguigu.spring6.bean.User scopeprototype init-methodinitMethod destroy-methoddestroyMethodproperty nameid value1001/propertyproperty nameusername valueadmin/propertyproperty namepassword value123456/propertyproperty nameage value23/property /bean④测试 Test public void testLife(){ClassPathXmlApplicationContext ac new ClassPathXmlApplicationContext(spring-lifecycle.xml);User bean ac.getBean(User.class);System.out.println(生命周期4、通过IOC容器获取bean并使用);ac.close(); }⑤bean的后置处理器 bean的后置处理器会在生命周期的初始化前后添加额外的操作需要实现BeanPostProcessor接口且配置到IOC容器中需要注意的是bean后置处理器不是单独针对某一个bean生效而是针对IOC容器中所有bean都会执行 创建bean的后置处理器 package com.atguigu.spring6.process;import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanProcessor implements BeanPostProcessor {Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println(☆☆☆ beanName bean);return bean;}Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println(★★★ beanName bean);return bean;} }在IOC容器中配置后置处理器 !-- bean的后置处理器要放入IOC容器才能生效 -- bean idmyBeanProcessor classcom.atguigu.spring6.process.MyBeanProcessor/2.13、实验十二FactoryBean ①简介 FactoryBean是Spring提供的一种整合第三方框架的常用机制。和普通的bean不同配置一个FactoryBean类型的bean在获取bean的时候得到的并不是class属性中配置的这个类的对象而是getObject()方法的返回值。通过这种机制Spring可以帮我们把复杂组件创建的详细过程和繁琐细节都屏蔽起来只把最简洁的使用界面展示给我们。 将来我们整合Mybatis时Spring就是通过FactoryBean机制来帮我们创建SqlSessionFactory对象的。 /** Copyright 2002-2020 the original author or authors.** Licensed under the Apache License, Version 2.0 (the License);* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an AS IS BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/ package org.springframework.beans.factory;import org.springframework.lang.Nullable;/*** Interface to be implemented by objects used within a {link BeanFactory} which* are themselves factories for individual objects. If a bean implements this* interface, it is used as a factory for an object to expose, not directly as a* bean instance that will be exposed itself.** pbNB: A bean that implements this interface cannot be used as a normal bean./b* A FactoryBean is defined in a bean style, but the object exposed for bean* references ({link #getObject()}) is always the object that it creates.** pFactoryBeans can support singletons and prototypes, and can either create* objects lazily on demand or eagerly on startup. The {link SmartFactoryBean}* interface allows for exposing more fine-grained behavioral metadata.** pThis interface is heavily used within the framework itself, for example for* the AOP {link org.springframework.aop.framework.ProxyFactoryBean} or the* {link org.springframework.jndi.JndiObjectFactoryBean}. It can be used for* custom components as well; however, this is only common for infrastructure code.** pb{code FactoryBean} is a programmatic contract. Implementations are not* supposed to rely on annotation-driven injection or other reflective facilities./b* {link #getObjectType()} {link #getObject()} invocations may arrive early in the* bootstrap process, even ahead of any post-processor setup. If you need access to* other beans, implement {link BeanFactoryAware} and obtain them programmatically.** pbThe container is only responsible for managing the lifecycle of the FactoryBean* instance, not the lifecycle of the objects created by the FactoryBean./b Therefore,* a destroy method on an exposed bean object (such as {link java.io.Closeable#close()}* will inot/i be called automatically. Instead, a FactoryBean should implement* {link DisposableBean} and delegate any such close call to the underlying object.** pFinally, FactoryBean objects participate in the containing BeanFactorys* synchronization of bean creation. There is usually no need for internal* synchronization other than for purposes of lazy initialization within the* FactoryBean itself (or the like).** author Rod Johnson* author Juergen Hoeller* since 08.03.2003* param T the bean type* see org.springframework.beans.factory.BeanFactory* see org.springframework.aop.framework.ProxyFactoryBean* see org.springframework.jndi.JndiObjectFactoryBean*/ public interface FactoryBeanT {/*** The name of an attribute that can be* {link org.springframework.core.AttributeAccessor#setAttribute set} on a* {link org.springframework.beans.factory.config.BeanDefinition} so that* factory beans can signal their object type when it cant be deduced from* the factory bean class.* since 5.2*/String OBJECT_TYPE_ATTRIBUTE factoryBeanObjectType;/*** Return an instance (possibly shared or independent) of the object* managed by this factory.* pAs with a {link BeanFactory}, this allows support for both the* Singleton and Prototype design pattern.* pIf this FactoryBean is not fully initialized yet at the time of* the call (for example because it is involved in a circular reference),* throw a corresponding {link FactoryBeanNotInitializedException}.* pAs of Spring 2.0, FactoryBeans are allowed to return {code null}* objects. The factory will consider this as normal value to be used; it* will not throw a FactoryBeanNotInitializedException in this case anymore.* FactoryBean implementations are encouraged to throw* FactoryBeanNotInitializedException themselves now, as appropriate.* return an instance of the bean (can be {code null})* throws Exception in case of creation errors* see FactoryBeanNotInitializedException*/NullableT getObject() throws Exception;/*** Return the type of object that this FactoryBean creates,* or {code null} if not known in advance.* pThis allows one to check for specific types of beans without* instantiating objects, for example on autowiring.* pIn the case of implementations that are creating a singleton object,* this method should try to avoid singleton creation as far as possible;* it should rather estimate the type in advance.* For prototypes, returning a meaningful type here is advisable too.* pThis method can be called ibefore/i this FactoryBean has* been fully initialized. It must not rely on state created during* initialization; of course, it can still use such state if available.* pbNOTE:/b Autowiring will simply ignore FactoryBeans that return* {code null} here. Therefore it is highly recommended to implement* this method properly, using the current state of the FactoryBean.* return the type of object that this FactoryBean creates,* or {code null} if not known at the time of the call* see ListableBeanFactory#getBeansOfType*/NullableClass? getObjectType();/*** Is the object managed by this factory a singleton? That is,* will {link #getObject()} always return the same object* (a reference that can be cached)?* pbNOTE:/b If a FactoryBean indicates to hold a singleton object,* the object returned from {code getObject()} might get cached* by the owning BeanFactory. Hence, do not return {code true}* unless the FactoryBean always exposes the same reference.* pThe singleton status of the FactoryBean itself will generally* be provided by the owning BeanFactory; usually, it has to be* defined as singleton there.* pbNOTE:/b This method returning {code false} does not* necessarily indicate that returned objects are independent instances.* An implementation of the extended {link SmartFactoryBean} interface* may explicitly indicate independent instances through its* {link SmartFactoryBean#isPrototype()} method. Plain {link FactoryBean}* implementations which do not implement this extended interface are* simply assumed to always return independent instances if the* {code isSingleton()} implementation returns {code false}.* pThe default implementation returns {code true}, since a* {code FactoryBean} typically manages a singleton instance.* return whether the exposed object is a singleton* see #getObject()* see SmartFactoryBean#isPrototype()*/default boolean isSingleton() {return true;} }②创建类UserFactoryBean package com.atguigu.spring6.bean; public class UserFactoryBean implements FactoryBeanUser {Overridepublic User getObject() throws Exception {return new User();}Overridepublic Class? getObjectType() {return User.class;} }③配置bean bean iduser classcom.atguigu.spring6.bean.UserFactoryBean/bean④测试 Test public void testUserFactoryBean(){//获取IOC容器ApplicationContext ac new ClassPathXmlApplicationContext(spring-factorybean.xml);User user (User) ac.getBean(user);System.out.println(user); }2.14、实验十三基于xml自动装配 自动装配 根据指定的策略在IOC容器中匹配某一个bean自动为指定的bean中所依赖的类类型或接口类型属性赋值 ①场景模拟 创建类UserController package com.atguigu.spring6.autowire.controller public class UserController {private UserService userService;public void setUserService(UserService userService) {this.userService userService;}public void saveUser(){userService.saveUser();}}创建接口UserService package com.atguigu.spring6.autowire.service public interface UserService {void saveUser();}创建类UserServiceImpl实现接口UserService package com.atguigu.spring6.autowire.service.impl public class UserServiceImpl implements UserService {private UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao userDao;}Overridepublic void saveUser() {userDao.saveUser();}}创建接口UserDao package com.atguigu.spring6.autowire.dao public interface UserDao {void saveUser();}创建类UserDaoImpl实现接口UserDao package com.atguigu.spring6.autowire.dao.impl public class UserDaoImpl implements UserDao {Overridepublic void saveUser() {System.out.println(保存成功);}}②配置bean 使用bean标签的autowire属性设置自动装配效果 自动装配方式byType byType根据类型匹配IOC容器中的某个兼容类型的bean为属性自动赋值 若在IOC中没有任何一个兼容类型的bean能够为属性赋值则该属性不装配即值为默认值null 若在IOC中有多个兼容类型的bean能够为属性赋值则抛出异常NoUniqueBeanDefinitionException bean iduserController classcom.atguigu.spring6.autowire.controller.UserController autowirebyType/beanbean iduserService classcom.atguigu.spring6.autowire.service.impl.UserServiceImpl autowirebyType/beanbean iduserDao classcom.atguigu.spring6.autowire.dao.impl.UserDaoImpl/bean自动装配方式byName byName将自动装配的属性的属性名作为bean的id在IOC容器中匹配相对应的bean进行赋值 bean iduserController classcom.atguigu.spring6.autowire.controller.UserController autowirebyName/beanbean iduserService classcom.atguigu.spring6.autowire.service.impl.UserServiceImpl autowirebyName/bean bean iduserServiceImpl classcom.atguigu.spring6.autowire.service.impl.UserServiceImpl autowirebyName/beanbean iduserDao classcom.atguigu.spring6.autowire.dao.impl.UserDaoImpl/bean bean iduserDaoImpl classcom.atguigu.spring6.autowire.dao.impl.UserDaoImpl/bean③测试 Test public void testAutoWireByXML(){ApplicationContext ac new ClassPathXmlApplicationContext(autowire-xml.xml);UserController userController ac.getBean(UserController.class);userController.saveUser(); }3、基于注解管理Bean☆ 从 Java 5 开始Java 增加了对注解Annotation的支持它是代码中的一种特殊标记可以在编译、类加载和运行时被读取执行相应的处理。开发人员可以通过注解在不改变原有代码和逻辑的情况下在源代码中嵌入补充信息。 Spring 从 2.5 版本开始提供了对注解技术的全面支持我们可以使用注解来实现自动装配简化 Spring 的 XML 配置。 Spring 通过注解实现自动装配的步骤如下 引入依赖开启组件扫描使用注解定义 Bean依赖注入 3.1、搭建子模块spring6-ioc-annotation ①搭建模块 搭建方式如spring6-ioc-xml ②引入配置文件 引入spring-ioc-xml模块日志log4j2.xml ③添加依赖 dependencies!--spring context依赖--!--当你引入Spring Context依赖之后表示将Spring的基础依赖引入了--dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion6.0.3/version/dependency!--junit5测试--dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-api/artifactId/dependency!--log4j2的依赖--dependencygroupIdorg.apache.logging.log4j/groupIdartifactIdlog4j-core/artifactIdversion2.19.0/version/dependencydependencygroupIdorg.apache.logging.log4j/groupIdartifactIdlog4j-slf4j2-impl/artifactIdversion2.19.0/version/dependency /dependencies3.2、开启组件扫描 Spring 默认不使用注解装配 Bean因此我们需要在 Spring 的 XML 配置中通过 context:component-scan 元素开启 Spring Beans的自动扫描功能。开启此功能后Spring 会自动从扫描指定的包base-package 属性设置及其子包下的所有类如果类上使用了 Component 注解就将该类装配到容器中。 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!--开启组件扫描功能--context:component-scan base-packagecom.atguigu.spring6/context:component-scan /beans注意在使用 context:component-scan 元素开启自动扫描功能前首先需要在 XML 配置的一级标签 中添加 context 相关的约束。 情况一最基本的扫描方式 context:component-scan base-packagecom.atguigu.spring6 /context:component-scan情况二指定要排除的组件 context:component-scan base-packagecom.atguigu.spring6!-- context:exclude-filter标签指定排除规则 --!-- type设置排除或包含的依据typeannotation根据注解排除expression中设置要排除的注解的全类名typeassignable根据类型排除expression中设置要排除的类型的全类名--context:exclude-filter typeannotation expressionorg.springframework.stereotype.Controller/!--context:exclude-filter typeassignable expressioncom.atguigu.spring6.controller.UserController/-- /context:component-scan情况三仅扫描指定组件 context:component-scan base-packagecom.atguigu use-default-filtersfalse!-- context:include-filter标签指定在原有扫描规则的基础上追加的规则 --!-- use-default-filters属性取值false表示关闭默认扫描规则 --!-- 此时必须设置use-default-filtersfalse因为默认规则即扫描指定包下所有类 --!-- type设置排除或包含的依据typeannotation根据注解排除expression中设置要排除的注解的全类名typeassignable根据类型排除expression中设置要排除的类型的全类名--context:include-filter typeannotation expressionorg.springframework.stereotype.Controller/!--context:include-filter typeassignable expressioncom.atguigu.spring6.controller.UserController/-- /context:component-scan3.3、使用注解定义 Bean 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 相同。 3.4、实验一Autowired注入 单独使用Autowired注解默认根据类型装配。【默认是byType】 查看源码 package org.springframework.beans.factory.annotation;import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) Retention(RetentionPolicy.RUNTIME) Documented public interface Autowired {boolean required() default true; }源码中有两处需要注意 第一处该注解可以标注在哪里 构造方法上方法上形参上属性上注解上 第二处该注解有一个required属性默认值是true表示在注入的时候要求被注入的Bean必须是存在的如果不存在则报错。如果required属性设置为false表示注入的Bean存在或者不存在都没关系存在的话就注入不存在的话也不报错。 ①场景一属性注入 创建UserDao接口 package com.atguigu.spring6.dao;public interface UserDao {public void print(); }创建UserDaoImpl实现 package com.atguigu.spring6.dao.impl;import com.atguigu.spring6.dao.UserDao; import org.springframework.stereotype.Repository;Repository public class UserDaoImpl implements UserDao {Overridepublic void print() {System.out.println(Dao层执行结束);} }创建UserService接口 package com.atguigu.spring6.service;public interface UserService {public void out(); }创建UserServiceImpl实现类 package com.atguigu.spring6.service.impl;import com.atguigu.spring6.dao.UserDao; import com.atguigu.spring6.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService {Autowiredprivate UserDao userDao;Overridepublic void out() {userDao.print();System.out.println(Service层执行结束);} }创建UserController类 package com.atguigu.spring6.controller;import com.atguigu.spring6.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class UserController {Autowiredprivate UserService userService;public void out() {userService.out();System.out.println(Controller层执行结束。);}}测试一 package com.atguigu.spring6.bean;import com.atguigu.spring6.controller.UserController; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class UserTest {private Logger logger LoggerFactory.getLogger(UserTest.class);Testpublic void testAnnotation(){ApplicationContext context new ClassPathXmlApplicationContext(Beans.xml);UserController userController context.getBean(userController, UserController.class);userController.out();logger.info(执行成功);}}测试结果 以上构造方法和setter方法都没有提供经过测试仍然可以注入成功。 ②场景二set注入 修改UserServiceImpl类 package com.atguigu.spring6.service.impl;import com.atguigu.spring6.dao.UserDao; import com.atguigu.spring6.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService {private UserDao userDao;Autowiredpublic void setUserDao(UserDao userDao) {this.userDao userDao;}Overridepublic void out() {userDao.print();System.out.println(Service层执行结束);} }修改UserController类 package com.atguigu.spring6.controller;import com.atguigu.spring6.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class UserController {private UserService userService;Autowiredpublic void setUserService(UserService userService) {this.userService userService;}public void out() {userService.out();System.out.println(Controller层执行结束。);}}测试成功调用 ③场景三构造方法注入 修改UserServiceImpl类 package com.atguigu.spring6.service.impl;import com.atguigu.spring6.dao.UserDao; import com.atguigu.spring6.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService {private UserDao userDao;Autowiredpublic UserServiceImpl(UserDao userDao) {this.userDao userDao;}Overridepublic void out() {userDao.print();System.out.println(Service层执行结束);} }修改UserController类 package com.atguigu.spring6.controller;import com.atguigu.spring6.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class UserController {private UserService userService;Autowiredpublic UserController(UserService userService) {this.userService userService;}public void out() {userService.out();System.out.println(Controller层执行结束。);}}测试成功调用 ④场景四形参上注入 修改UserServiceImpl类 package com.atguigu.spring6.service.impl;import com.atguigu.spring6.dao.UserDao; import com.atguigu.spring6.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService {private UserDao userDao;public UserServiceImpl(Autowired UserDao userDao) {this.userDao userDao;}Overridepublic void out() {userDao.print();System.out.println(Service层执行结束);} }修改UserController类 package com.atguigu.spring6.controller;import com.atguigu.spring6.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;Controller public class UserController {private UserService userService;public UserController(Autowired UserService userService) {this.userService userService;}public void out() {userService.out();System.out.println(Controller层执行结束。);}}测试成功调用 ⑤场景五只有一个构造函数无注解 修改UserServiceImpl类 package com.atguigu.spring6.service.impl;import com.atguigu.spring6.dao.UserDao; import com.atguigu.spring6.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService {Autowiredprivate UserDao userDao;public UserServiceImpl(UserDao userDao) {this.userDao userDao;}Overridepublic void out() {userDao.print();System.out.println(Service层执行结束);} }测试通过 当有参数的构造方法只有一个时Autowired注解可以省略。 说明有多个构造方法时呢大家可以测试再添加一个无参构造函数测试报错 ⑥场景六Autowired注解和Qualifier注解联合 添加dao层实现 package com.atguigu.spring6.dao.impl;import com.atguigu.spring6.dao.UserDao; import org.springframework.stereotype.Repository;Repository public class UserDaoRedisImpl implements UserDao {Overridepublic void print() {System.out.println(Redis Dao层执行结束);} }测试测试异常 错误信息中说不能装配UserDao这个Bean的数量等于2 怎么解决这个问题呢当然要byName根据名称进行装配了。 修改UserServiceImpl类 package com.atguigu.spring6.service.impl;import com.atguigu.spring6.dao.UserDao; import com.atguigu.spring6.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService {AutowiredQualifier(userDaoImpl) // 指定bean的名字private UserDao userDao;Overridepublic void out() {userDao.print();System.out.println(Service层执行结束);} }总结 Autowired注解可以出现在属性上、构造方法上、构造方法的参数上、setter方法上。当带参数的构造方法只有一个Autowired注解可以省略。Autowired注解默认根据类型注入。如果要根据名称注入的话需要配合Qualifier注解一起使用。 3.5、实验二Resource注入 Resource注解也可以完成属性注入。那它和Autowired注解有什么区别 Resource注解是JDK扩展包中的也就是说属于JDK的一部分。所以该注解是标准注解更加具有通用性。(JSR-250标准中制定的注解类型。JSR是Java规范提案。)Autowired注解是Spring框架自己的。Resource注解默认根据名称装配byName未指定name时使用属性名作为name。通过name找不到的话会自动启动通过类型byType装配。Autowired注解默认根据类型装配byType如果想根据名称装配需要配合Qualifier注解一起用。Resource注解用在属性上、setter方法上。Autowired注解用在属性上、setter方法上、构造方法上、构造方法参数上。 Resource注解属于JDK扩展包所以不在JDK当中需要额外引入以下依赖【如果是JDK8的话不需要额外引入依赖。高于JDK11或低于JDK8需要引入以下依赖。】 dependencygroupIdjakarta.annotation/groupIdartifactIdjakarta.annotation-api/artifactIdversion2.1.1/version /dependency源码 package jakarta.annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) Retention(RetentionPolicy.RUNTIME) Repeatable(Resources.class) public interface Resource {String name() default ;String lookup() default ;Class? type() default Object.class;Resource.AuthenticationType authenticationType() default Resource.AuthenticationType.CONTAINER;boolean shareable() default true;String mappedName() default ;String description() default ;public static enum AuthenticationType {CONTAINER,APPLICATION;private AuthenticationType() {}} }①场景一根据name注入 修改UserDaoImpl类 package com.atguigu.spring6.dao.impl;import com.atguigu.spring6.dao.UserDao; import org.springframework.stereotype.Repository;Repository(myUserDao) public class UserDaoImpl implements UserDao {Overridepublic void print() {System.out.println(Dao层执行结束);} }修改UserServiceImpl类 package com.atguigu.spring6.service.impl;import com.atguigu.spring6.dao.UserDao; import com.atguigu.spring6.service.UserService; import jakarta.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService {Resource(name myUserDao)private UserDao myUserDao;Overridepublic void out() {myUserDao.print();System.out.println(Service层执行结束);} }测试通过 ②场景二name未知注入 修改UserDaoImpl类 package com.atguigu.spring6.dao.impl;import com.atguigu.spring6.dao.UserDao; import org.springframework.stereotype.Repository;Repository(myUserDao) public class UserDaoImpl implements UserDao {Overridepublic void print() {System.out.println(Dao层执行结束);} }修改UserServiceImpl类 package com.atguigu.spring6.service.impl;import com.atguigu.spring6.dao.UserDao; import com.atguigu.spring6.service.UserService; import jakarta.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService {Resourceprivate UserDao myUserDao;Overridepublic void out() {myUserDao.print();System.out.println(Service层执行结束);} }测试通过 当Resource注解使用时没有指定name的时候还是根据name进行查找这个name是属性名。 ③场景三 其他情况 修改UserServiceImpl类userDao1属性名不存在 package com.atguigu.spring6.service.impl;import com.atguigu.spring6.dao.UserDao; import com.atguigu.spring6.service.UserService; import jakarta.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service;Service public class UserServiceImpl implements UserService {Resourceprivate UserDao userDao1;Overridepublic void out() {userDao1.print();System.out.println(Service层执行结束);} }测试异常 根据异常信息得知显然当通过name找不到的时候自然会启动byType进行注入以上的错误是因为UserDao接口下有两个实现类导致的。所以根据类型注入就会报错。 Resource的set注入可以自行测试 总结 Resource注解默认byName注入没有指定name时把属性名当做name根据name找不到时才会byType注入。byType注入时某种类型的Bean只能有一个 3.6、Spring全注解开发 全注解开发就是不再使用spring配置文件了写一个配置类来代替配置文件。 package com.atguigu.spring6.config;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;Configuration //ComponentScan({com.atguigu.spring6.controller, com.atguigu.spring6.service,com.atguigu.spring6.dao}) ComponentScan(com.atguigu.spring6) public class Spring6Config { }测试类 Test public void testAllAnnotation(){ApplicationContext context new AnnotationConfigApplicationContext(Spring6Config.class);UserController userController context.getBean(userController, UserController.class);userController.out();logger.info(执行成功); }
http://wiki.neutronadmin.com/news/378667/

相关文章:

  • 邵阳 做网站公司赣州住房和建设局网站
  • 住房与城乡建设部网站 黑龙江网站品牌建设建议
  • 网站开发常用标签有什么免费做h5的素材网站
  • 网站开发器wordpress海报生成器
  • 多终端网站手机wap网站大全
  • 深圳做网站得外包公司有哪些局网站建设总结
  • 什么专业会做网站长安网站建设软件开发
  • 网站建设销售客户疑问微信属于营销型网站
  • 保定网站建设费用微网站和手机站区别
  • 建设简易电子商务网站流程图莞城最新通告
  • 网站建设方式与信息化拼多多海外跨境电商入驻流程
  • 沈阳企业网站建设惠州免费建站模板
  • 外贸婚纱礼服网站北京免费分类信息发布网
  • 网站安全建设模板100个最佳市场营销案例
  • 专业微网站电话ip地址反查域名
  • 湘潭网站建设 磐石网络在哪模板建站多少钱
  • 联邦快递网站建设的目标小程序商城装修
  • 厦门市建设工程造价网站首页ios个人开发者账号
  • 网站推广公司需要多少钱东莞阳光网站官网
  • 部门网站建设个人总结合肥市网站建设公司
  • 网站建设公司南昌常州网站制作优化
  • 图片站wordpress模板建站工具论坛
  • 建设银行官方网站登录入口上海网站建设服务框架
  • 企业建设网站哪家好flash网站推荐
  • 网站设计制作需要多少钱青岛网页设计师
  • 发布网站后不可能存在的文件夹是网站备案号 英文
  • 做企业网站需要注意哪些网站内容管理系统源码
  • 网站开发个人工作室网络服务是干什么的
  • 东莞做企业网站免费单页网站在线制作
  • 做好的网站模板怎么修改做通路富集分析的网站