Spring的创建和使用
一、创建Spring项目
1.1 创建一个maven项目
创建Spring项目与创建Servlet项目类似,首先创建一个Maven项目。
1.2 添加spring依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
</dependencies>
1.3 添加启动类
在java文件夹下创建一个启动类,包含main方法即可,这个不是必须的,只是为了测试spring的存取对象的功能。
/**
* 启动类
*/
public class App {
public static void main(String[] args) {
}
}
二、存储Bean对象
在Java对象也可以叫做Bean。
存储Bean对象分为以下两步:
- 先创建一个Bean。
- 将创建的Bean注册到Spring容器中。
2.1 创建Bean
Bean就是一个普通的对象。
public class User {
public User(){
System.out.println("加载了User");
}
public void sayHi(String name){
System.out.println("Hi"+name);
}
}
2.2 将Bean注册到容器中
在resources文件的根目录下添加Spring配置文件spring-config.xml
配置文件:
<?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">
</beans>
将Bean对象注册到容器中就是在<beans>
中添加如下配置
<bean id="user" class="beans.User"></bean>
三、获取并使用Bean对象
前面我们将Bean存到了Spring容器中,接下来就可以获取并使用Bean对象了。
获取并使用Bea对象可以分为以下三步:
- 得到Spring上下文对象
- 通过Spring上下文,获取某一个指定的Bean对象
- 使用Bean对象
3.1 得到Spring上下文
3.1.1 使用ApplicationContext得到上下文
ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
3.1.2 使用BeanFactory得到上下文
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
3.1.3 ApplicationContext与BeanFactory的区别(经典面试题)
首先ApplicationContext和BeanFactory都是Spring的顶级接口。
- 继承关系和功能:BeanFactory是ApplicationContext的父类,BeanFactory提供了基础的访问容器的能力,而ApplicationContext除了继承了BeanFactory的所有功能之外,他还拥有独特的特性,如对国际化的支持等。
- 性能方面:ApplicationContext是一次性加载并初始化所有的Bean对象(饿汉模式),而BeanFactory是需要哪个采取加载哪个(懒汉模式)。因此ApplicationContext在启动时比较慢,会浪费比较多资源,但第一次使用bean时比较快;而BeanFactory不浪费任何资源,但在第一次使用bean时比较慢。
举例:
两个bean对象:
public class Student {
public Student(){
System.out.println("加载了Student");
}
public void sayHello(){
System.out.println("HELLO,STUDENT!");
}
}
public class User {
public User(){
System.out.println("加载了User");
}
public void sayHi(String name){
System.out.println("Hi"+name);
}
}
通过ApplicationContext来获取Spring上下文对象:
public class App {
public static void main(String[] args) {
//1.得到spring的上下文对象
ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
}
}
通过BeanFactory来获取Spring上下文对象:
public class App2 {
public static void main(String[] args) {
//1.获取spring的上下文对象
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
}
}
这也就说明上面提到的它两在加载时的区别。
3.2 获取指定的Bean对象
3.2.1 使用对象id获取
User user = (User) context.getBean("user");
3.2.2 使用类型获取
Student student = beanFactory.getBean(Student.class);
但这种方式存在一种隐患,当有一个类型被重复注册到配置文件中时,这时你再使用这种方法去获取bean,Spring就不分辨不来到底获取哪一个,就会报错。
因此引出了下面这种我们最常用的一种方式:
3.2.3 使用id+类型获取
Student student=beanFactory.getBean("student",Student.class);
这种方式就比较稳妥了!
3.3 使用bean
public class App {
public static void main(String[] args) {
//1.得到spring的上下文对象
ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
//2.获取指定的Bean对象
User user = (User) context.getBean("user");
// 3.使用bean
user.sayHi("xue");
}
}