框架是一个封装了很多功能和模块的程序的半成品。
可以理解为是一个由很多工具类组合而成的一个工具包。
Spring是分层的 Java SE/EE应用 full-stack 轻量级开源框架。是Java开发的灵魂框架。
Spring有两大内核:
IOC(Inverse Of Control:控制反转)
AOP(Aspect Oriented Programming:面向切面编程)
**IOC:**控制反转,其实就是把对象的创建权,交给spring,由Spring负责给我们创建对象
DI: 依赖注入,其实就是给对象的成员变量赋值。
**AOP:**面向切面编程,底层就是动态代理
事务: Spring提供了声明式事务,也就是说,我们可以通过配置的方式来添加事务。
①导入 Spring 开发的基本包坐标
②编写 Dao 接口和实现类
③创建 Spring 核心配置文件
④在 Spring 配置文件中配置 UserDaoImpl
⑤使用 Spring 的 API 获得 Bean 实例
<properties>
<spring.version>5.0.5.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
dependencies>
public interface UserDao {
public void save();
}
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("UserDao save method running....");
}
}
在类路径下(resources)创建applicationContext.xml配置文件
<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>
<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="userDao" class="com.itheima.dao.impl.UserDaoImpl">bean>
beans>
@Test
public void test1(){
//加载spring配置文件。获取spring核心容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取spring容器中的userDao对象
UserDao userDao = (UserDao) ac.getBean("userDao");
//调用方法
userDao.save();
}
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">bean>
**id:**Bean实例在Spring容器中的唯一标识
**class:**Bean的全限定名称
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" scope="singleton" >bean>
**scope:**指对象的作用范围,取值如下:
取值范围 | 说明 |
---|---|
singleton | 默认值,单例的,立即加载 |
prototype | 多例的,延迟加载 |
request | WEB项目中,Spring创建一个Bean的对象,将对象存入到request域中 |
session | WEB项目中,Spring创建一个Bean的对象,将对象存入到session域中 |
global session | WEB项目中,应用在Portlet环境,如果没有Portlet环境那么globalSession相当于session |
**init-method:**指定类中的初始化方法名称,当对象被创建时,调用该方法。
**destroy-method:**指定类中销毁方法名称,当对象被销毁时,调用该方法。
1) 使用无参构造方法实例化
默认调用无参构造方法创建对象,如果bean中没有默认无参构造函数,将会创建失败
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
2) 工厂静态方法实例化
//工厂类
public class StaticFactoryBean {
//静态方法,通过该方法获取对象
public static UserDao createUserDao(){
return new UserDaoImpl();
}
}
<bean id="userDao" class="com.itheima.factory.StaticFactoryBean"
factory-method="createUserDao" />
3) 工厂实例方法实例化
//工厂类
public class DynamicFactoryBean {
//非静态方法,需要先创建工厂类的对象,再通过对象调用该方法,获取对象
public UserDao createUserDao(){
return new UserDaoImpl();
}
}
<bean id="factoryBean" class="com.itheima.factory.DynamicFactoryBean"/>
<bean id="userDao" factory-bean="factoryBean" factory-method="createUserDao"/>
依赖注入(Dependency Injection):是SpringIOC的具体体现,简单来说,就是给对象的属性赋值
①在类中提供有参构造
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
②配置Spring容器调用有参构造时进行注入
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao">constructor-arg>
bean>
①在UserServiceImpl中添加setUserDao方法
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
②配置Spring容器调用set方法进行注入
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
bean>
Spring的依赖注入(也就是给成员变量赋值),可以分为三种类型。
这三种类型均可以使用“set方法赋值”,也可以使用“构造方法赋值”,以下以“set方法赋值”为例,进行演示。
public class UserDaoImpl implements UserDao {
private String company;
private int age;
public void setCompany(String company) {
this.company = company;
}
public void setAge(int age) {
this.age = age;
}
public void save() {
System.out.println(company + "===" +age);
}
}
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="company" value="传智播客">property>
<property name="age" value="15">property>
bean>
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
bean>
(1)List
public class UserDaoImpl implements UserDao {
private List<String> strList;
public void setStrList(List<String> strList) {
this.strList = strList;
}
public void save() {
System.out.println(strList);
System.out.println("UserDao save method running....");
}
}
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="strList">
<list>
<value>aaavalue>
<value>bbbvalue>
<value>cccvalue>
list>
property>
bean>
(2)集合数据类型(List
public class UserDaoImpl implements UserDao {
private List<User> userList;
public void setUserList(List<User> userList) {
this.userList = userList;
}
public void save() {
System.out.println(userList);
System.out.println("UserDao save method running....");
}
}
<bean id="u1" class="com.itheima.domain.User"/>
<bean id="u2" class="com.itheima.domain.User"/>
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="userList">
<list>
<bean class="com.itheima.domain.User"/>
<bean class="com.itheima.domain.User"/>
<ref bean="u1"/>
<ref bean="u2"/>
list>
property>
bean>
(3)集合数据类型( Map
public class UserDaoImpl implements UserDao {
private Map<String,User> userMap;
public void setUserMap(Map<String, User> userMap) {
this.userMap = userMap;
}
public void save() {
System.out.println(userMap);
System.out.println("UserDao save method running....");
}
}
<bean id="u1" class="com.itheima.domain.User"/>
<bean id="u2" class="com.itheima.domain.User"/>
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="userMap">
<map>
<entry key="user1" value-ref="u1"/>
<entry key="user2" value-ref="u2"/>
map>
property>
bean>
(4)集合数据类型(Properties)的注入
public class UserDaoImpl implements UserDao {
private Properties properties;
public void setProperties(Properties properties) {
this.properties = properties;
}
public void save() {
System.out.println(properties);
System.out.println("UserDao save method running....");
}
}
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="properties">
<props>
<prop key="p1">aaaprop>
<prop key="p2">bbbprop>
<prop key="p3">cccprop>
props>
property>
bean>
实际开发中,Spring的配置内容非常多,这就导致Spring配置很繁杂且体积很大,所以,可以将部分配置拆解到其他配置文件中,而在Spring主配置文件通过import标签进行加载
<import resource="applicationContext-xxx.xml"/>
//加载配置文件,获取Spring核心容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//从Spring核心容器中获取对象
//获取id="userService"的对象
UserService us1 = (UserService)ac.getBean("userService");
//获取类型为"UserService"的对象,
//注意,该操作要求,在spring核心容器中只能有一个"UserService"类型的对象
UserService us2 = ac.getBean(UserService.class);
//获取类型为"UserService",id为"userService"的对象,
UserService us3 = ac.getBean("userService",UserService.class);