Java学习笔记01 ── Spring(IOC 上)

框架的介绍

今天开始学习Spring框架。首先框架是一种规范,是用来支撑我们开发的一种半成品。使用框架能够简化我们的开发,它已经给我们封装好了一些冗余的代码,我们直接拿过来用就可以。框架主要是注解+反射+设计模式,能够很好的解耦合。常用的框架一般是比较成熟的,它对一些业务处理的很好,甚至比我们自己处理的要好。有更好的、更省事的可以直接使用,我们为什么还要自己开发呢。

Spring框架

Spring框架是我们web开发的很重要的一个框架,是一个开源框架,其中最重要的特性是IOC和AOP,IOC即是控制反转,将对bean的控制从我们转给了程序本身,AOP是面向切面编程。Spring的特性:非侵入式、IOC、AOP、容器、组件化、一站式。
在这里插入图片描述

Spring项目的初体验

加入jar包

Spring依赖的五个jar包:
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELE2ASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
commons-logging-1.1.1.jar
在工程中创建一个bin目录将其加入并加载。
在这里插入图片描述

创建xml文件

创建一个conf的资源目录,然后创建Spring的xml文件,名为applicationContext.xml。
在这里插入图片描述

创建Person对象,对其初始化并获取它

创建person类

创建一个Person类,有id和name两个属性,生成相应的get、set方法,构造器,tostring方法。

package com.glq.Person;

public class Person {
	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Person(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Person() {
		super();
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + "]";
	}
}

编辑xml文件

编辑xml文件,对Person进行配置。一个标签就是一个对象,id属性唯一表示该对象,class属性表示该对象属于哪一个类。在标签中可以使用标签来对对象的属性进行初始化,name属性表示对象的属性名、value属性表示对象的属性值(只能初始化字面量)。下面就初始化了一个id为10001、name为林宪宇的对象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="p1" class="com.glq.Person.Person">
	<property name="id" value="10001"></property>
	<property name="name" value="林宪宇"></property>
</bean>

</beans>

获取对象

首先要创建IOC容器对象,然后 获取person对象。这里用了三种方式来获取,getBean的参数分别为id值、类型、id和类型。

public class PersonTest {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		Object bean1 = ac.getBean("p1");
		Person bean2 = ac.getBean(Person.class);
		Person bean3 = ac.getBean("p1", Person.class);
		
		System.out.println("bean1" + bean1);
		System.out.println("bean2" + bean2);
		System.out.println("bean3" + bean3);
	}
}

通过这三种方式都可以获取到对象,执行结果如下:
在这里插入图片描述

默认单例

我们将bean3的id属性修改成99999,然后重新打印看结果
在这里插入图片描述
可以看到三个对象都进行了修改,证明是单例的。

IOC和DI

IOC

ioc即反转控制,就是javebean不需要我们来创建,将控制权交给程序,我们不需要知道对象是怎么创建的,只需要获取就可以。

DI

DI即依赖注入。DI就是IOC,IOC就是DI。IOC是一种思想,而DI是具体实现。Person类依赖于id和name两个属性,就对这两个属性进行注入,注入即赋值。

ApplicationContext

ApplicationContext是最顶层的接口,下面有ConfigurableApplicationContext子接口。主要实现类有两个ClassPathXmlApplicationContext、 FileSystemXmlApplicationContext。其中第一个对应路径下的xml配置文件,可以简单的理解为写相对路径;第二个配置文件对应着文件系统的xml配置文件们可以简单的理解为写绝对路径。
在这里插入图片描述

给bean属性赋值

注入的方式

set注入

标签给属性赋值就是使用的set方法来注入,这里我们验证一下。依然使用上面的demo,我们将setId()给注释掉,重新运行一下程序看结果。可以看到xml文件中直接就报错了,提示找不到set方法,证明是使用set方法来注入的。
在这里插入图片描述

构造方法注入

使用标签来进行构造方法注入。标签中有几个常用属性,分别是value(初始化的值)、index(索引,表名是类中的第几个属性,从0开始)、type(要注入的属性的类型)。

	<bean id="p2" class="com.glq.Person.Person">
		<constructor-arg value="lxy" index="1" type="java.lang.String"></constructor-arg>
		<constructor-arg value="10002" index="0" type="java.lang.Integer"></constructor-arg>
	</bean>

字面量属性

所谓字面量属性就是指value标签所能直接使用的值,一般就是基本数据类型及其包装类+String类型

非字面量

给对象属性赋值,这里我们重新构建一个demo。 一个student类 有id和name两个属性,一个Teacher类,有id、Student student、Student[] studentArray、List studentList、 Map< Integer, Student> studentMap五个属性,这里没有什么具体含义,只是用来演示使用。

Teacher类:
package com.glq.student;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Teacher {
	private Integer id;
	private Student student;
	private Student[] studentArray;
	private List<Student> studentList;
	private Map< Integer, Student> studentMap;
	public Teacher() {
		super();
	}
	public Teacher(Integer id, Student student, Student[] studentArray, List<Student> studentList,
			Map<Integer, Student> studentMap) {
		super();
		this.id = id;
		this.student = student;
		this.studentArray = studentArray;
		this.studentList = studentList;
		this.studentMap = studentMap;
	}
	@Override
	public String toString() {
		return "Teacher [id=" + id + ", student=" + student + ", studentArray=" + Arrays.toString(studentArray)
				+ ", studentList=" + studentList + ", studentMap=" + studentMap + "]";
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public Student[] getStudentArray() {
		return studentArray;
	}
	public void setStudentArray(Student[] studentArray) {
		this.studentArray = studentArray;
	}
	public List<Student> getStudentList() {
		return studentList;
	}
	public void setStudentList(List<Student> studentList) {
		this.studentList = studentList;
	}
	public Map<Integer, Student> getStudentMap() {
		return studentMap;
	}
	public void setStudentMap(Map<Integer, Student> studentMap) {
		this.studentMap = studentMap;
	}
	
	
}

Student类:
package com.glq.student;

public class Student {
	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}
	public Student(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Student() {
		super();
	}
	
}

对象属性

给上述demo中的Student student属性赋值,改属性是一个对象类型,我们可以通过内部bean或者ref的方式赋值

内部bean

即在标签中在定义一个bean,这个bean只能在这个标签中使用。

<bean id="t1" class="com.glq.student.Teacher">
	<property name="id" value="10001"></property>
	<property name="student">
		<bean class="com.glq.student.Student">
			<property name="id" value="20001"></property>
			<property name="name" value="s1"></property>
		</bean>
	</property>
</bean>
ref

首先定义一个id为s2的Student对象,然后在Teacher对象中使用ref属性进行引入。

student:
<bean id="s2" class="com.glq.student.Student">
	<property name="id" value="20002"></property>
	<property name="name" value="s2"></property>
</bean>
teacher:
<bean id="t2" class="com.glq.student.Teacher">
	<property name="id" value="10001"></property>
	<property name="student" ref="s2">
	</property>
</bean>

数组属性

给Student[] studentArray属性赋值,这里用到标签,然后在标签之中使用标签将对象引入,若是字面量则使用标签。

<bean id="t3" class="com.glq.student.Teacher">
	<property name="id" value="10003"></property>
	<property name="studentArray">
		<array >
			<ref bean="s2"/>
			<ref bean="s3"/>
			<ref bean="s4"/>
		</array>
	</property>
</bean>

list属性

给List studentList属性赋值,与给数组类似,只需要把标签换成标签即可

<bean id="t4" class="com.glq.student.Teacher">
	<property name="id" value="10004"></property>
	<property name="studentList">
		<list>
			<ref bean="s2"/>
			<ref bean="s3"/>
			<ref bean="s4"/>
		</list>
	</property>
</bean>

map属性

给 Map< Integer, Student> studentMap属性赋值。给map类型的属性赋值需要用到标签,在标签中有个标签来表示一个键值对,然后在标签中使用标签来给键赋值,使用/标签给值赋值。

<bean id="t5" class="com.glq.student.Teacher">
	<property name="id" value="10004"></property>
	<property name="studentMap">
		<map>
			<entry>
				<key>
					<value>1</value>
				</key>
				<ref bean="s2"/>
			</entry>
			<entry>
				<key>
					<value>2</value>
				</key>
				<ref bean="s3"/>
			</entry>
		</map>
	</property>
</bean>

list/map类型的bean

集合也是一种类型,我们想要创建一个list/map类型的bean需要怎么做呢?下面以list为例子,map与之类似。
1.首先引入util命名空间,在sts的xml文件下方有一个Namespace栏点进去然后选择util即可
在这里插入图片描述
2.然后使用util:list标签来创建一个list对象

<util:list id="students">
	<ref bean="s2"/>
	<ref bean="s3"/>
	<ref bean="s4"/>
</util:list>

p命名空间

为了简化赋值过程,我们可以引入p命名空间。
1.首先引入p命名空间,在sts的xml文件下方有一个Namespace栏点进去然后选择p即可
在这里插入图片描述
2.使用示范

<bean id="t6" class="com.glq.student.Teacher" p:id="10006" p:student-ref="s2" p:studentList-ref="students_list" p:studentMap-ref="students_map"></bean>

FactoryBean

Spring中有两种bean,一种是普通的bean一种是工厂bean
首先创建Car类,有brand、price两个属性,然后创建一个CarFactory类,该类实现FactoryBean接口,需要重写getObject、getObjectType、isSingleton方法,三个方法分别是返回对象,返回对象类型,是否是单例。

public class CarFactory implements FactoryBean<Car>{

	@Override
	public Car getObject() throws Exception {
		Car car = new Car("宝马", 9.99);
		return car;
	}

	@Override
	public Class<?> getObjectType() {
		// TODO Auto-generated method stub
		return Car.class;
	}

	@Override
	public boolean isSingleton() {
		// TODO Auto-generated method stub
		return false;
	}
	

}

在xml文件中进行配置bean

<bean id="car" class="com.glq.factoryteat.CarFactory"></bean>

然后创建test类,获取bean并且打印看结果。发现返回的是一个car对象。工厂模式即只需要获取结果,不需要知道它是怎么来的。

		ApplicationContext ac = new ClassPathXmlApplicationContext("car.xml");
		Object bean = ac.getBean("car");
		System.out.println(bean);

在这里插入图片描述



今天的内容到此结束,谢谢大家的观看,如有错误请指正,谢谢!CSDN记录成长!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值