Java EE - spring框架初识
发布日期:2021-06-30 19:50:12 浏览次数:2 分类:技术文章

本文共 3053 字,大约阅读时间需要 10 分钟。

问题导读:

spring 框架的简介

解决方案:

概念:

spring 的核心是依赖注入和控制反转。依赖注入,正常类A依赖类B,如果在类A的对象a中要使用类B的对象b,需要在a中new,依赖注入就是,框架来创建a和b对象,将b注入到a中,框架接管了这个以来对象的创建工作,并且把其(b)注入到需要他的a中。控制反转,创建对象的职责一直以来是,使用他的组件的,但是spring实现了将这个控制权反转到框架上,ioc容器来管理这件事。

IOC容器的使用:

  • xml配置(配置文件可以是一份也可以是多份也可以多份导入到一份)
  • 2.5版本之后增加了通过注解的配置支持
  • 环境搭建

控制反转实现

1.添加jar
2.配置文件
3.bean
package com.lpl.bean;public class Product {	private String name;	private Integer age;	//默认无参构造器	public Product() {	}	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;	}	}
4.创建对象、测试
package com.lpl.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lpl.bean.Product;public class Demo1 {	public static void main(String[] args) {		//读取ioc容器中beans的信息		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");		//从ioc容器中取出我们需要的bean		Product product = context.getBean("product", Product.class);		product.setName("Spring");		product.setAge(13);		System.out.println("名字:"+product.getName()+" 年龄:"+product.getAge());	}}

ioc容器通过构造器初始化对象

1.配置文件
2.bean
//有参构造器	public Product(String name, Integer age) {		this.name = name;		this.age = age;	}
3.测试
Product product = context.getBean("product", Product.class);		/*product.setName("Spring");		product.setAge(13);*/		System.out.println("名字:"+product.getName()+" 年龄:"+product.getAge());

依赖注入实现

1)setter 方式依赖注入

1.配置信息
2.bean
2.1 
//为他添加setter	private Address address;
2.2
package com.lpl.bean;public class Address {	private String address;	private String post_code;	public Address() {			}	public Address(String address, String post_code) {		this.address = address;		this.post_code = post_code;	}	public String getAddress() {		return address;	}	public void setAddress(String address) {		this.address = address;	}	public String getPost_code() {		return post_code;	}	public void setPost_code(String post_code) {		this.post_code = post_code;	}}
3.测试
package com.lpl.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lpl.bean.Product;public class Demo1 {	public static void main(String[] args) {		//读取ioc容器中beans的信息		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");		//从ioc容器中取出我们需要的bean		Product product = context.getBean("product", Product.class);		/*product.setName("Spring");		product.setAge(13);*/		System.out.println("名字:"+product.getName()+" 年龄:"+product.getAge()+" 地址:"+product.getAddress().getAddress()				+" 邮编:"+product.getAddress().getPost_code());	}}

2)构造器方式注入

1.配置信息
2.bean
//构造器注入	public Product(String name,Integer age,Address address) {		this.name = name;		this.age = age;		this.address = address;	}
3.测试

转载地址:https://lipenglin.blog.csdn.net/article/details/52489714 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java EE - AOP 和 spring 的bean 生命周期
下一篇:TomCat - linux 安装Tomcat

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月13日 16时00分35秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

企业邮箱如何申请注册,邮箱申请如何免费注册? 2019-04-30
微信企业邮箱,手机邮箱格式地址怎么写? 2019-04-30
公司如何申请企业邮箱,公司邮箱怎么申请,公司企业邮箱哪个好? 2019-04-30
电子邮箱账号怎么申请,怎样申请邮箱账号呢 2019-04-30
邮箱怎么发邮件,邮件发信量多少,职场新人怎么发汇报邮件呢? 2019-04-30
maven 多层次pom 新引入包,编译成功,还是没有将包引入到本地 2019-04-30
leetCode2 两数相加 2019-04-30
【工具使用】使用pip与conda安装、更新与卸载Pytorch和torchvision 2019-04-30
【深度学习笔记】batchsize, time step(iteration), epoch 区别与联系 2019-04-30
【解决错误】ModuleNotFoundError No module named matplotlib 2019-04-30
【工具使用】Google免费云环境Colaboratory使用 2019-04-30
【深度学习笔记】卷积层,全连接层,池化层的相关输出参数计算 2019-04-30
【NLP学习笔记】文本分类概述 2019-04-30
【深度学习笔记】文本分类 2019-04-30
【转载】炼丹实验室:深度学习网络调参技巧 2019-04-30
【论文阅读笔记】Graph Convolutional Networks for Text Classification 2019-04-30
【论文阅读笔记】文本分类论文汇总 2019-04-30
【论文阅读笔记】Convolutional Neural Networks for Sentence Classification 2019-04-30
【NLP学习笔记】One-hot encoding:独热编码 2019-04-30
【工具使用】CSDN编辑器markdown字体、颜色与字号的设置 2019-04-30