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 文件
test测试
@Testpublic void student1(){ApplicationContext context = new ClassPathXmlApplicationContext("student1.xml"); Student1 student1 = context.getBean("student1", Student1.class); System.out.println(student1); }
二、构造函数方式注入
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文件
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文件
test测试
@Testpublic void student3(){ApplicationContext context = new ClassPathXmlApplicationContext("student3.xml"); Student3 student3 = context.getBean("student3", Student3.class); System.out.println(student3); }
四、JavaConfig 方式注入 pojo层
/** * @Author: crush * @Date: 2021-06-17 17:16 * version 1.0 * JavaConfig 配置 */public class Student4 {@Value("wyh4")private String name; @Value("hngy4")private String school; @Overridepublic String toString() {return "Student4{" +"name='" + name + '\'' +", school='" + school + '\'' +'}'; }}
JavaConfig 类
@Configurationpublic class Student4Config {@Beanpublic Student4 student4(){return new Student4(); }}
xml文件 扫描包
测试:
@Testpublic void student4(){ApplicationContext context = new ClassPathXmlApplicationContext("student4.xml"); Student4 student4 = context.getBean("student4", Student4.class); System.out.println(student4); }
五、Service层注入详解
service
/** * @Author: crush * @Date: 2021-06-17 17:27 * version 1.0 * xml 配置 */public interface StudentService1 {void test(); }
serviceImpl
/** * @Author: crush * @Date: 2021-06-17 17:29 * version 1.0 * xml 配置 */public class StudentService1Impl implements StudentService1{@Overridepublic void test() {System.out.println("===StudentDao1Impl==="); }}
xml配置文件
总结 【Spring|Spring bean 四种注入方式详解】本篇文章就到这里了,希望能给你带来帮助,也希望能够您能够关注脚本之家的更多内容!
推荐阅读
- Activiti(一)SpringBoot2集成Activiti6
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- 2018-07-09|2018-07-09 Spring 的DBCP,c3p0
- spring|spring boot项目启动websocket
- Spring|Spring Boot 整合 Activiti6.0.0
- Spring集成|Spring集成 Mina
- springboot使用redis缓存
- Spring|Spring 框架之 AOP 原理剖析已经出炉!!!预定的童鞋可以识别下发二维码去看了
- Spring|Spring Boot之ImportSelector