spring是一个容器管理框架。
重点:
- ioc 控制反转,以往对象的创建式通过new方式,现在对应都由spring管理通过反射形式将对象初始化放在spring容器中。
- di 依赖注入,bean与bean之间根据依赖关系进行引用,之所以引入这个名词,应该是bean之间相互依赖的关系更能体现spring容器管理的特点。例如在下面例子中UserInfoService对象需要初始化User对象。
applicationContext.xml
中将对象配置到bean标签下,value指定的是示例参数,ref指定的是引用(例如在某个对象中需要使用另一个对象)。- 使用
ClassPathXmlApplicationContext
加载并初始化bean容器(可以理解成解析xml文件,通过Java反射机制初始化对象放在类似Hashmap中,例user = Class.forName("com.hiro.entity.User");
并设置属性,放入类似Hashmap中),当获取时候通过id获取即可。 ClassPathXmlApplicationContext
操作是占用内存的,所以保证ApplicationContext
是单态的。- 通过一个例子直观看一下。
- 1.maven工程目录
- 2.pom.xml添加依赖
- 3.创建几个对象
- 4.创建applicationContext.xml
- 5.创建bean容器,测试获取bean
- 6.修改获取单态ApplicationContext
1 maven 工程目录:
2 添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hiro</groupId>
<artifactId>springStart</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
</dependencies>
</project>
3 创建两个bean
package com.hiro.entity;
/**
* Project: springStart
*
* @author : hirolin
* @date : 2019/5/18 12:20
*/
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
package com.hiro.service;
import com.hiro.entity.User;
/**
* Project: springStart
*
* @author : hirolin
* @date : 2019/5/18 12:19
*/
public class UserInfoService {
private String userFrom;
private User user;
public String getUserFrom() {
return userFrom;
}
public void setUserFrom(String userFrom) {
this.userFrom = userFrom;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void say() {
System.out.println(userFrom);
System.out.println(user.toString());
}
}
4 创建applicationContext.xml
将创建的两个对象配置到bean标签下。
<?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="user" class="com.hiro.entity.User">
<property name="name" value="小花"/>
</bean>
<bean id="userInfoService" class="com.hiro.service.UserInfoService">
<property name="userFrom" value="web"></property>
<property name="user" ref="user"></property>
</bean>
</beans>
5 创建bean容器,测试获取bean
package com.hiro.test;
import com.hiro.service.UserInfoService;
import com.hiro.utils.ApplicationContextUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Project: springStart
*
* @author : hirolin
* @date : 2019/5/18 12:23
*/
public class Test {
public static void main(String [] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserInfoService userInfoService = (UserInfoService) applicationContext.getBean("userInfoService");
userInfoService.say();
}
}
6.修改获取单态ApplicationContext
使用饿汉式单例模式,使用final修改class防止被修改
package com.hiro.utils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Project: springStart
*
* @author : hirolin
* @date : 2019/5/18 12:34
*/
//使用final修饰 保证class不被继承修改
final public class ApplicationContextUtils {
private static ApplicationContext applicationContext = null;
//创建空的构造器
public ApplicationContextUtils() {
}
//饿汉式单例 类文件加载的时候就会初始化
static {
applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
}
测试:
package com.hiro.test;
import com.hiro.entity.User;
import com.hiro.service.UserInfoService;
import com.hiro.utils.ApplicationContextUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Project: springStart
*
* @author : hirolin
* @date : 2019/5/18 12:23
*/
public class Test {
public static void main(String [] args) throws ClassNotFoundException {
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// UserInfoService userInfoService = (UserInfoService) applicationContext.getBean("userInfoService");
// userInfoService.say();
UserInfoService userInfoService = (UserInfoService) ApplicationContextUtils.getApplicationContext().getBean("userInfoService");
userInfoService.say();
}
}