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

本文共 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

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月12日 18时28分16秒