bean 四种注入方式详解 Spring
Spring,bean,四种注入方式详解,博智网带你了解详细信息 。
目录
- 一、Set方式注入
- pojo层:
- 1.xml 文件
- test测试
- 二、构造函数方式注入
- pojo层
- 2.xml文件
- test测试
- 三、注解注入
- pojo层
- 3.xml文件
- test测试
- 四、JavaConfig 方式注入
- pojo层
- JavaConfig 类
- xml文件 扫描包
- 测试:
- 五、Service层注入详解
- service
- serviceImpl
- xml配置文件
- 总结
一、Set方式注入
pojo层:/** * @Author: crush * @Date: 2021-06-17 16:57 * version 1.0 * xml 配置注入版本set 方式 */public class Student1 {public String name;public String school;public void setName(String name) {this.name = name;}public void setSchool(String school) {this.school = school;}@Overridepublic String toString() {return "Student1{" +"name='" + name + '\'' +", school='" + school + '\'' +'}';}}
1.xml 文件<?xml version="1.0" encoding="UTF-8"?><beans xmlns="https://www.yf-zs.com/d/file/p/20210716150016133665/20210716150016133666"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.yf-zs.com/d/file/p/20210716150018133765/20210716150018133766.xsd"><!--set方式注入id是注入bean中的名字class 是全限定类名property 是按照set方式注入--><bean id="student1" class="com.crush.pojo.Student1"><property name="name" value="https://www.yf-zs.com/redian/wyh1"/><property name="school" value="https://www.yf-zs.com/redian/hngy1"/></bean></beans>
test测试 @Testpublic void student1(){ApplicationContext context = new ClassPathXmlApplicationContext("student1.xml");Student1 student1 = context.getBean("student1", Student1.class);System.out.println(student1);}
二、构造函数方式注入
【bean 四种注入方式详解 Spring】
pojo层/** * @Author: crush * @Date: 2021-06-17 17:02 * version 1.0 * xml 配置 构造函数方式注入 */public class Student2 {private String name;private String school;public Student2(String name, String school) {this.name = name;this.school = school;}@Overridepublic String toString() {return "Student2{" +"name='" + name + '\'' +", school='" + school + '\'' +'}';}}
2.xml文件<?xml version="1.0" encoding="UTF-8"?><beans xmlns="https://www.yf-zs.com/d/file/p/20210716150016133665/20210716150016133666"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.yf-zs.com/d/file/p/20210716150018133765/20210716150018133766.xsd"><!--set方式注入id是注入bean中的名字class 是全限定类名constructor 是按照构造方式注入index 是按照成员变量在构造函数中的参数的第几个name 表示成员变量名type 表示类型value 表示值ref 表示引用 可引用另外一个注入到Spring的中的值--><bean id="student2" class="com.crush.pojo.Student2"><constructor-arg index="0"name="name" type="java.lang.String" value="https://www.yf-zs.com/redian/wyh2"/><constructor-arg name="school" value="https://www.yf-zs.com/redian/hngy2"/></bean></beans>
test测试@Testpublic void student2(){ApplicationContext context = new ClassPathXmlApplicationContext("student2.xml");Student2 student2 = context.getBean("student2", Student2.class);System.out.println(student2);}
三、注解注入pojo层/** * @Author: crush * @Date: 2021-06-17 17:08 * version 1.0 */@Componentpublic class Student3 {@Value("wyh3")private String name;@Value("hngy3")private String school;@Overridepublic String toString() {return "Student3{" +"name='" + name + '\'' +", school='" + school + '\'' +'}';}}
3.xml文件<?xml version="1.0" encoding="UTF-8"?><beans xmlns="https://www.yf-zs.com/d/file/p/20210716150016133665/20210716150016133666"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="https://www.yf-zs.com/d/file/p/20210716150018133763/20210716150018133764"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.yf-zs.com/d/file/p/20210716150018133765/20210716150018133766.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--注解方式注入需要扫描注解在的包 注解才会生效--><context:component-scan base-package="com.crush.pojo"/></beans>
test测试@Testpublic void student3(){ApplicationContext context = new ClassPathXmlApplicationContext("student3.xml");Student3 student3 = context.getBean("student3", Student3.class);System.out.println(student3);}
- 四种基本人格类型特征
- 密西西比河最后注入什么湾
- 果啤有酒精吗
- 清理狗狗身上跳蚤的四种方法
- 专门调查有哪四种形式
- 注入洞庭湖的沅江发源地在
- 如何判断兰花该浇水了
- 什么ETF可以当天买卖
- 黑白条纹短裙配什么上衣
- 心理状态包括哪四种
