Spring(一)

Spring是什么

Spring官网


在这里插入图片描述
在这里插入图片描述


Spring有哪些模块
在这里插入图片描述


我们在用Eclipse来进行开发的时候,可以安装一个插件:
在这里插入图片描述


搭建 Spring 开发环境

在这里插入图片描述


新建Spring项目
在这里插入图片描述


有一个 HelloWorld 类:

public class HelloWorld {

	private String user;
	
	public HelloWorld() {
		System.out.println("HelloWorld's constructor...");
	}
	
	public void setUser(String user) {
		this.user = user;
	}
	
	public HelloWorld(String user) {
		this.user = user;
	}

	public void hello(){
	}
	
}

在没有Spring的时候,我们创建一个对象就是这样来创建的:

public class Main {
	public static void main(String[] args) {
		
		HelloWorld helloWorld = new HelloWorld();
		helloWorld.setUser("Tom");
		helloWorld.hello(); 
		
	}	
}

现在,当我们用了Spring的时候,就是可以这样来进行创建对象了:
首先,我们创建一个Spring的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!-- 配置一个 bean -->
	<bean id="helloWorld" class="com.atguigu.spring.helloworld.HelloWorld">
		<!-- 为属性赋值 -->
		<property name="user" value="Jerry"></property>
	</bean>
	
</beans>

在这里插入图片描述


这个时候,就是可以这样来获取bean的实例:

public class Main {
	
	public static void main(String[] args) {
		//1. 创建 Spring 的 IOC 容器
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		
		//2. 从 IOC 容器中获取 bean 的实例
		HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld3");
		
		//根据类型来获取 bean 的实例: 要求在  IOC 容器中只有一个与之类型匹配的 bean, 若有多个则会抛出异常. 
		//一般情况下, 该方法可用, 因为一般情况下, 在一个 IOC 容器中一个类型对应的 bean 也只有一个. 
		//HelloWorld helloWorld1 = ctx.getBean(HelloWorld.class);
		
		//3. 使用 bean
		helloWorld.hello();

	}
	
}

如果我们只创建了ioc容器,不进行调用getBean方法来获取对象实例,那么这个时候,ioc会为我们做什么事呢 ?为了清楚的看到效果,我们给HelloWorld这个类里面的无参构造器和set方法各打印一句话,让我们看看运行之后,控制台是如何打印的:

public class HelloWorld {

	private String user;
	
	public HelloWorld() {
		System.out.println("HelloWorld's constructor...");
	}
	
	public void setUser(String user) {
		System.out.println("setUser:" + user);
		this.user = user;
	}
	
	public HelloWorld(String user) {
		this.user = user;
	}

	public void hello(){
		System.out.println("Hello: " + user);
	}
	
}

在这里插入图片描述

Spring 中的 Bean 配置

在这里插入图片描述


控制反转和依赖注入:
在这里插入图片描述


  • 在传统的方式,如果一个对象要和另外一个对象之间有依赖关系,我们可以通过把一个对象作为另外一个对象的成员属性,然后通过set方法的方式来进行关联;

A a = new A();
B b = new B();
b.setA(a);

  • 现在,有了Spring的依赖注入就可以自动的建立依赖关系;
    在这里插入图片描述

IOC的前生
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


配置Bean:基于xml文件配置的方式

在这里插入图片描述


在 Spring 的 IOC 容器里配置 Bean

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


Spring IOC容器

在这里插入图片描述


在这里插入图片描述


ApplicationContext:

在这里插入图片描述


在这里插入图片描述


从IOC容器中获取Bean的方法

在这里插入图片描述


我们还可以用类型来进行获取 Bean 的实例对象,这个时候,就是不用进行强转:
在这里插入图片描述


通过类型来进行获取要求这个类型的 Bean 在 IOC 容器里面是唯一的,通过 id 来进行获取就不会有这样的问题,因为我明确指名了是哪一个 Bean 的实例:
在这里插入图片描述
这个时候,IOC容器就不知道要返回哪一个了:
在这里插入图片描述


在这里插入图片描述


属性的注入方式

在这里插入图片描述

属性注入(set方法注入)

在这里插入图片描述

构造方法注入

在这里插入图片描述


有一个Car类:

public class Car {

	private String company;
	private String brand;

	private int maxSpeed;
	private float price;

	public Car(String company, String brand, float price) {
		super();
		this.company = company;
		this.brand = brand;
		this.price = price;
	}

	public Car(String company, String brand, int maxSpeed) {
		super();
		this.company = company;
		this.brand = brand;
		this.maxSpeed = maxSpeed;
	}

	public Car(String company, String brand, int maxSpeed, float price) {
		super();
		this.company = company;
		this.brand = brand;
		this.maxSpeed = maxSpeed;
		this.price = price;
	}

	@Override
	public String toString() {
		return "Car [company=" + company + ", brand=" + brand + ", maxSpeed="
				+ maxSpeed + ", price=" + price + "]";
	}
}

我们可以这样来设置属性:
这个是按照构造器里面的参数顺序来配的:
在这里插入图片描述

在这里插入图片描述


实际上,我们也可以用index来指明参数的顺序:
在这里插入图片描述
现在有一个问题,有两个参数个数相同的构造器:

	public Car(String company, String brand, float price) {
		super();
		this.company = company;
		this.brand = brand;
		this.price = price;
	}

	public Car(String company, String brand, int maxSpeed) {
		super();
		this.company = company;
		this.brand = brand;
		this.maxSpeed = maxSpeed;
	}

在这里插入图片描述


index和type是可以混合使用的,这个时候,就是可以精确的匹配了:
在这里插入图片描述


在这里插入图片描述

注入属性值的细节

在这里插入图片描述


value属性:
在这里插入图片描述


子节点:
在这里插入图片描述


含有特殊字符的就可以这样来写:
在这里插入图片描述


引用其他的Bean

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


这样配的话,就是一个内部Bean,内部Bean不用写id,在类的内部声明的一个Bean,在外部是不能够被引用的:

	<!-- 声明使用内部 bean -->
	<bean id="service2" class="com.atguigu.spring.ref.Service">
		<property name="dao">
			<!-- 内部 bean, 类似于匿名内部类对象. 不能被外部的 bean 来引用, 也没有必要设置 id 属性 -->
			<bean class="com.atguigu.spring.ref.Dao">
				<property name="dataSource" value="c3p0"></property>
			</bean>
		</property>
	</bean>

用构造器配置的方式也是一样的:
在这里插入图片描述

null值和级联属性

在这里插入图片描述


设置null值
在这里插入图片描述


设置级联属性:在设置级联属性的时候,对象一定要先进行赋值,不然的话,对象就会为null值,也就对里面的属性赋不了值:
在这里插入图片描述


需要注意的地方:
在这里插入图片描述


在这里插入图片描述

集合属性

在这里插入图片描述


List
在这里插入图片描述
在这里插入图片描述


Map
在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


properties
在这里插入图片描述


在这里插入图片描述


我们可以把集合拿出来定义,以达到共享的目的:
在这里插入图片描述


在这里插入图片描述

P命名空间

在这里插入图片描述


在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值