当前位置: 移动技术网 > IT编程>开发语言>Java > Spring学习流程

Spring学习流程

2020年07月22日  | 移动技术网IT编程  | 我要评论

Spring学习目录

Spring简介


1. 什么是Spring?

Spring是分层的 Java SE/EE应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control:反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核。

提供了展现层 SpringMVC和持久层 Spring JDBCTemplate以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的Java EE 企业应用开源框架

2. Spring的优势在哪?

  1. 方便解耦,简化开发 => Spring离不开解耦
  2. AOP 编程的支持
  3. 声明式事务的支持
  4. 方便程序的测试 => 可以集成junit

Spring的快速入门


1. Spring的开发步骤

  1. 导入 Spring 开发的基本包坐标 => 普通项目的导包
  2. 编写 Dao 接口和实现类
  3. 创建 Spring 核心配置文件 => applicationContext.xml
  4. 在 Spring 配置文件中配置 UserDaoImpl
  5. 使用 Spring 的 API 获得 Bean 实例 => getBean方法

2. 代码实现

  1. 导入Spring开发的基本包坐标
<properties>
	<spring.version>5.0.5.RELEASE</spring.version>
</properties>
<!--导入spring的context坐标,context依赖core、beans、expression-->
<dependencies> 
    <dependency>  
        <groupId>org.springframework</groupId> 
        <artifactId>spring-context</artifactId> 
        <version>${spring.version}</version> <!--这里找一个用的多的版本即可 -->
    </dependency>
</dependencies>
  1. 编写 Dao 接口和实现类
public interface UserDao {  
    public void save();
}

public class UserDaoImpl implements UserDao {  
        @Override  
        public void save() {
        	System.out.println("UserDao save method running....");
	}
}
  1. 创建 Spring 核心配置文件:
    在resources路径下创建applicationContext.xml配置文件
    配置文件可以任意命名,但习惯命名为applicationContext.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>
  1. 在 Spring 配置文件中配置 UserDaoImpl
<?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">
    <!-- 就这句话,将userDao注入到容器中 -->
   <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
</beans>

  1. 使用 Spring 的 API 获得 Bean 实例
public static void main(String[] args) {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserDao userDao = (UserDao) applicationContext.getBean("userDao");
    userDao.save();
}

Spring的文件配置


1. Bean标签的基本配置

  1. Bean标签用于将配置对象交给Spring来创建,而不是我们自己new一个对象
  2. 默认调用无参构造方法,如果没有就会创建失败
  3. id属性:唯一标识符,每个Bean实例在Spring容器中的id不相同
  4. class属性:Bean的全限定名

2. Bean标签的范围配置

scope属性: 对象的作用范围,有以下取值:

  1. singleton:默认值,单例
  2. prototype:多例
  3. request:WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中
  4. session:WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中
  5. global session:WEB 项目中,应用在 Portlet 环境,如果没有 Portlet 环境那么globalSession 相当于 session

PS:

  • 当值为singleton时:
    • Bean的实例化个数只有一个
    • 当Spring核心文件被加载时,Bean实例被实例化
    • 生命周期:
      • 对象创建:应用加载,创建容器时,对象被创建
      • 对象运行:容器存活,对象就存在
      • 对象销毁:应用卸载,容器销毁,对象被销毁
  • 当值为prototype时:
    • Bean的实例化个数有多个
    • 当调用getBean方法时,Bean实例被实例化
    • 生命周期:
      • 对象创建:使用对象时(getBean),创建新的对象实例
      • 对象运行:对象在使用,存活
      • 对象销毁:当对象长时间不使用,被GC回收

3. Bean的生命周期配置

  1. init-method:指定类中的初始化方法名称
  2. destroy-method:指定类中销毁方法名称
<?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="userDao" class="com.itheima.dao.impl.UserDaoImpl" init-method="init"></bean>

4. Bean的三种实例化方法

  1. 使用无参构造方法实例化 => 根据无参构造方法创建对象,没有会创建失败
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
  1. 工厂静态方法实例化 => 工厂的静态方法返回Bean实例
public class StaticFactoryBean {
	// 注意一定是静态的
    public static UserDao createUserDao(){    
    	return new UserDaoImpl();
    }
}
<bean id="userDao" class="com.itheima.factory.StaticFactoryBean" factory-method="createUserDao" />
  1. 工厂实例方法实例化 => 工厂的非静态方法返回Bean实例
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"/>

PS: 注意 工厂实例化 注入的都是factory-method所对应方法的返回值

5. Bean的依赖注入入门

  1. 创建UserService(service层,调用dao层),调用UserDao
public class UserServiceImpl implements UserService {
	@Override
	public void save() {
         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");       	    
         UserDao userDao = (UserDao) applicationContext.getBean("userDao");	
         userDao.save();
 	}
 }
  1. 将UserServiceImpl的创建交由Spring
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>
  1. 从Spring容器中获取UserService进行操作
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
userService.save();

6. Bean的依赖注入概念

依赖注入(Dependency Injection):它是 Spring 框架核心 IOC 的具体实现。
在编写程序时,通过控制反转,把对象的创建交给了 Spring,但是代码中不可能出现没有依赖的情况。
IOC 解耦只是降低他们的依赖关系,但不会消除。例如:业务层仍会调用持久层的方法。
那这种业务层和持久层的依赖关系,在使用 Spring 之后,就让 Spring 来维护了。
简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取
例如:Service层调用Dao层,我们可以交友Spring去创建,而不是每次都自己在Service层中new一个Dao层实例

7. Bean的依赖注入方式

  1. 构造方法
// 正常的通过Spring获取Dao的方式
public class UserServiceImpl implements UserService {
	@Override
	public void save() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");       
	    UserDao userDao = (UserDao) applicationContext.getBean("userDao");    
	    userDao.save();
	}
 }
<!-- 将userDao注入到userService中,先要将userDao交由Spring创建 -->
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
	<!-- name是构造方法的属性,ref是引用Spring容器的id值 -->      		   	
	<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
// 配置bean依赖注入之后
public class UserServiceImpl implements UserService {
	private UserDao userDao;
	public UserServiceImpl(UserDao userDao){ 
		// 这个形参userDao和上面的constructor-arg的name属性一致
		this.userDao = userDao;
	}
	@Override
	public void save() {
		// 上面的获取就不需要了,注入之后相当于直接配置好了
	    userDao.save();
	}
 }
  1. set方法
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">
	<!-- 这个name属性的值要和java代码的set方法后半部分的一致 -->
	<!-- 例如上面是setUserDao,后半部分为UserDao,首字母小写即为name填写的值 -->
	<property name="userDao" ref="userDao"/>
</bean>

PS:
set方法可以用p命名空间注入
p命名空间本质也是set方法注入,只是方便了一点
在xml中引入p命名空间,之后修改注入方式

xmlns:p="http://www.springframework.org/schema/p"
<!-- 上面的name属性是什么,就是xxx-ref -->
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>

8. Bean的依赖注入的数据类型

之前用的都是自定义的类,为引用类型
注入数据可以有三种数据类型:

  1. 普通数据类型
  2. 引用数据类型
  3. 集合数据类型

普通数据类型的注入

public class UserDaoImpl implements UserDao {
	private String name;
    private int age;
    public void setCompany(String name) {
        this.name= name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void save() {
        System.out.println(name + ":" + age);
        System.out.println("UserDao save method running....");   
    }
}
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
    <property name="name" value="xyr"></property>
    <property name="age" value="18"></property>
</bean>

集合类型的注入

  1. 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">
    <!-- name值还是set后半部分, 没办法用ref和value了 -->
    <property name="userList">
  		<!-- 内部使用集合标签 -->
        <list>
        	<!--如果List中的类型是基本类型,可以用value标签-->
        	<!-- 其余的类型可以使用引用 -->
            <bean class="com.itheima.domain.User"/>
            <bean class="com.itheima.domain.User"/>
            <ref bean="u1"/>
            <ref bean="u2"/>       
        </list>
    </property>
</bean>
  1. 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 -->
            <entry key="user1" value-ref="u1"/>
            <entry key="user2" value-ref="u2"/>
        </map>
    </property>
</bean>
  1. 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">
    	<!-- 集合是map和list之类的,配置是props -->
        <props>
            <prop key="p1">aaa</prop>
            <prop key="p2">bbb</prop> 
            <prop key="p3">ccc</prop>
        </props>
    </property>
</bean>

9. 引入其他的配置文件(分模块开发)

可以只加载主配置文件,其余的通过import标签加载进来

<!-- 这个resource的值也是随便写的,对应上即可 -->
<import resource="applicationContext-xxx.xml"/>

Spring相关的API


1. ApplicationContext的继承体系

ApplicationContext是个接口,代表应用上下文,可以通过它获取Bean的实例对象

2. ApplicationContext的实现类

  1. ClassPathXmlApplicationContext => 类加载路径下加载配置文件,推荐使用
  2. FileSystemXmlApplicationContext => 磁盘路径上加载配置文件,可以在磁盘的任意位置
  3. AnnotationConfigApplicationContext => 当使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。

3. getBean的使用方法

// 源码
public Object getBean(String name) throws BeansException {  
	assertBeanFactoryActive();   
	return getBeanFactory().getBean(name);
}
public <T> T getBean(Class<T> requiredType) throws BeansException {
   	assertBeanFactoryActive();
	return getBeanFactory().getBean(requiredType);
}

可以看到有两种重载方法
当传入为字符串时,通过id从容器中获取Bean实例,返回Object,需要强转
当传入为Class类型时,根据类型从容器中获取Bean实例,不需要强转,但是如果容器中有多个相同类型的Bean,会报错

	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	UserService userService1 = (UserService) applicationContext.getBean("userService");
	UserService userService2 = applicationContext.getBean(UserService.class);

Spring配置数据源(连接池)

1. 开发步骤

  1. 导入数据源的坐标和数据库驱动坐标 => 导包
  2. 创建数据源对象
  3. 设置数据源的基本连接数据
  4. 使用数据源获取连接资源和归还连接资源

2. 代码实现

  1. 导入坐标
<!-- C3P0连接池 -->
<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>
<!-- Druid连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>
  1. 创建连接池
  2. 设置数据源的基本连接数据
  3. 使用数据源获取连接资源和归还连接资源
@Test
public void testC3P0() throws Exception {
	//创建数据源
	ComboPooledDataSource dataSource = new ComboPooledDataSource();
	//设置数据库连接参数
    dataSource.setDriverClass("com.mysql.jdbc.Driver");
    dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
    dataSource.setUser("root");
    dataSource.setPassword("root");
	//获得连接对象
	Connection connection = dataSource.getConnection();
	System.out.println(connection);
	//归还连接对象
	connection.close();
}

@Test
public void testDruid() throws Exception {
    //创建数据源
    DruidDataSource dataSource = new DruidDataSource();
    //设置数据库连接参数
    dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 
    dataSource.setUrl("jdbc:mysql://localhost:3306/test");   
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    //获得连接对象
    Connection connection = dataSource.getConnection();    
    System.out.println(connection);
    //归还连接对象
	connection.close();
}

3. 提取基本连接数据

  1. 提取 jdbc.properties配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
@Test
public void testC3P0ByProperties() throws Exception {
    //加载类路径下的jdbc.properties
    ResourceBundle rb = ResourceBundle.getBundle("jdbc");
    ComboPooledDataSource dataSource = new ComboPooledDataSource(); 
    dataSource.setDriverClass(rb.getString("jdbc.driver"));   
    dataSource.setJdbcUrl(rb.getString("jdbc.url")); 
    dataSource.setUser(rb.getString("jdbc.username")); 
    dataSource.setPassword(rb.getString("jdbc.password"));
    Connection connection = dataSource.getConnection();   
    System.out.println(connection);
}

4. 使用Spring配置数据源

<!-- 注意class,每个数据源不一样 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!-- name属性还是set方法的后半部分,注意数据源不同相应做出改变 -->
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
    <property name="user" value="root"/>
    <property name="password" value="root"/>
</bean>

抽取jdbc配置文件获取连接信息

  1. 首先引入命名空间和约束路径:
    命名空间:xmlns:context=“http://www.springframework.org/schema/context”
    约束路径:
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
  2. 之后使用Spring容器加载properties文件
  3. 将value换成SpringEL表达式
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

Spring注解开发

Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率。

1. Spring原始注解开发

Spring原始注解主要是替代Bean标签的配置

注解 说明
@Component 使用在类上用于实例化Bean
@Controller 使用在web层类上用于实例化Bean
@Service 使用在service层类上用于实例化Bean
@Repository 使用在dao层类上用于实例化Bean
@Autowired 使用在字段上用于根据类型依赖注入
@Qualifier 结合@Autowired一起使用用于根据名称进行依赖注入
@Resource 相当于@Autowired+@Qualifier,按照名称进行注入
@Value 注入普通属性
@Scope 标注Bean的作用范围
@PostConstruct 使用在方法上标注该方法是Bean的初始化方法
@PreDestroy 使用在方法上标注该方法是Bean的销毁方法

PS:使用注解开发要配置一个组件扫描,告诉Spring哪个包下的Bean需要识别配置

<!-- 注解的组件扫描, 放在applicationContext.xml中 -->
<context:component-scan base-package="com.itheima"></context:component-scan>

这四个注解作用一样,是用来区分不同层上的不同写法

  1. 当不知道是哪一层的代码用@Component
  2. Web层用@Controller
  3. Service层用@Service
  4. Dao层用@Repository
注解 说明
@Component 使用在类上用于实例化Bean
@Controller 使用在web层类上用于实例化Bean
@Service 使用在service层类上用于实例化Bean
@Repository 使用在dao层类上用于实例化Bean

例如:

//@Component("userDao")
@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Override
    public void save() {
    	System.out.println("save running... ...");
    }
}

注解 说明
@Autowired 使用在字段上用于根据类型依赖注入
@Qualifier 结合@Autowired一起使用用于根据名称进行依赖注入(id)
@Resource 相当于@Autowired+@Qualifier,按照名称进行注入
@Value 注入普通属性

这四个用来注入对象

/************* Service层   ****************/
//@Component("userService")
@Service("userService")
public class UserServiceImpl implements UserService {
    /*@Autowired
    @Qualifier("userDao")*/
    @Resource(name="userDao")
    private UserDao userDao;
    @Override
    public void save() {       
   	  userDao.save();
    }
}
/*************** Dao层  **************************/
@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Value("注入普通数据")
    private String str;
    // SpEL表达式
    @Value("${jdbc.driver}")
    private String driver;
    @Override
    public void save() {
        System.out.println(str);
        System.out.println(driver);
        System.out.println("save running... ...");
    }
}

@Scope标注范围

//@Scope("prototype")
@Scope("singleton")
public class UserDaoImpl implements UserDao {
   //此处省略代码
}

@PostConstruct标注初始化方法,使用@PreDestroy标注销毁方法

@PostConstruct
public void init(){
	System.out.println("初始化方法....");
}
@PreDestroy
public void destroy(){
	System.out.println("销毁方法.....");
}

2. Spring新注解开发

原始注解无法完全替代XML文件,比如使用注解还需要注解扫描等。
加上以下的新注解即可完全替代XML文件。

注解 说明
@Configuration 用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解
@ComponentScan 用于指定 Spring 在初始化容器时要扫描的包。 作用和在 Spring 的 xml 配置文件中的 <context:component-scan base-package=“com.itheima”/>一样
@Bean 使用在方法上,标注将该方法的返回值存储到 Spring 容器中
@PropertySource 用于加载.properties 文件中的配置
@Import 用于导入其他配置类
@Configuration // 标注是一个配置类
@ComponentScan("com.itheima") // 组件扫描的路径
@Import({DataSourceConfiguration.class}) // 引入其他的配置类,是个数组,可以引入多个
public class SpringConfiguration {
}


@PropertySource("classpath:jdbc.properties") // 不能忘记classpath
public class DataSourceConfiguration {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
}

@Bean(name="dataSource") // 不是自己写的,也不是基本数据类型,用这个方法
public DataSource getDataSource() throws PropertyVetoException { 
    ComboPooledDataSource dataSource = new ComboPooledDataSource(); 
    dataSource.setDriverClass(driver);
    dataSource.setJdbcUrl(url);
    dataSource.setUser(username);
    dataSource.setPassword(password);
    return dataSource;
} 

Spring整合Junit

  1. 问题来源:每个测试方法中都有以下两行代码
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = app.getBean("user"); 
  1. 解决方法:上面代码的作用是获取Spring容器。直接让SpringJunit负责创建Spring容器,将配置文件告诉它,将需要测试的Bean直接注入。

1. Spring集成Junit步骤

  1. 导入spring集成Junit的坐标
  2. 使用@Runwith注解替换原来的运行期
  3. 使用@ContextConfiguration指定配置文件或配置类
  4. 使用@Autowired注入需要测试的对象
  5. 创建测试方法进行测试

2. 代码实现

  1. 导入spring集成Junit的坐标
<!--此处需要注意的是,spring5 及以上版本要求 junit 的版本必须是 4.12 及以上-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.0.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
  1. 使用@Runwith注解替换原来的运行期 => 让SpringJunit去创建Spring容器
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringJunitTest { // 这个是测试类
}
  1. 使用@ContextConfiguration指定配置文件或配置类
// 注意ContextConfiguration,不是上面的Configuration
// 这里是告诉Spring的配置文件在哪,而不是标志他是一个配置文件
@RunWith(SpringJUnit4ClassRunner.class)
//加载spring核心配置文件 => XML方式
//@ContextConfiguration(value = {"classpath:applicationContext.xml"})
//加载spring核心配置类 => 注解方式
@ContextConfiguration(classes = {SpringConfiguration.class})
public class SpringJunitTest {
}
  1. 使用@Autowired注入需要测试的对象
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class})
public class SpringJunitTest {
    @Autowired // 根据类型注入
    private UserService userService;
}
  1. 测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class})public class SpringJunitTest {
    @Autowired
    private UserService userService;
    @Test
    public void testUserService(){
   	 userService.save();
    }
}

Spring的AOP简介


1. 什么是AOP

AOP 为 Aspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

AOP 是 OOP(面向对象编程) 的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

2. AOP的作用

在程序运行期间,在不修改源码的情况下对方法进行功能增强,便于维护

3. AOP的底层实现

AOP底层通过Spring提供的动态代理技术实现。在运行期间,生成代理对象,执行时进行增强功能的介入,调用目标对象方法,完成功能增强。

4. AOP的动态代理技术

  1. JDK代理:基于接口的动态代理技术
  2. cglib代理: 基于父类的动态代理技术
    所以基于JDK代理时代理对象和目标对象相当于兄弟,只能用接口来接收,基于JDK可以用目标对象接收

5. AOP的相关概念

  • Target(目标对象):代理的目标对象
  • Proxy (代理):一个类被 AOP 织入增强后,就产生一个结果代理类
  • Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点
  • Pointcut(切入点):所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义
  • Advice(通知/ 增强):所谓通知是指拦截到 Joinpoint 之后所要做的事情就是通知
  • Aspect(切面):是切入点和通知(引介)的结合
  • Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程。spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入

6. AOP 开发明确的事项

需要编写的内容

  • 编写核心业务代码(目标类的目标方法)
  • 编写切面类,切面类中有通知(增强功能方法)
  • 在配置文件中,配置织入关系,即将哪些通知与哪些连接点进行结合

AOP 底层使用哪种代理方式

在 spring 中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。


基于XML的AOP开发


1. 快速入门

  1. 导入 AOP 相关坐标
  2. 创建目标接口和目标类(内部有切点)
  3. 创建切面类(内部有增强方法)
  4. 将目标类和切面类的对象创建权交给 spring
  5. 在 applicationContext.xml 中配置织入关系
  6. 测试代码

可以看到第一步大部分是导入坐标,出去测试代码,最重要的部分就是要有 切点(第二步),通知(第三步),织入(第五步)

2. 代码实现

  1. 导入 AOP 相关坐标
<!--导入spring的context坐标,context依赖aop-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.0.5.RELEASE</version>
</dependency>
<!-- aspectj的织入 -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.13</version>
</dependency>
  1. 创建目标接口和目标类(内部有切点)
public interface TargetInterface {
    public void method();
}

public class Target implements TargetInterface {
    @Override
    public void method() { // 我们要对他进行增强,所以这个是切点
        System.out.println("Target running....");
    }
}
  1. 创建切面类(内部有增强方法)
public class MyAspect {
    //前置增强方法
    public void before(){
        System.out.println("前置代码增强.....");
    }
}
  1. 将目标类和切面类的对象创建权交给 spring
<!--配置目标类-->
<bean id="target" class="com.itheima.aop.Target"></bean>
<!--配置切面类-->
<bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>
  1. 在 applicationContext.xml 中配置织入关系
<!-- 导入aop的命名空间 -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 重点!! 配置织入 -->
<aop:config>
    <!--引用myAspect的Bean为切面对象-->
    <aop:aspect ref="myAspect"> <!-- 上面配置好的切面类 -->
        <!--配置Target的method方法执行时要进行myAspect的before方法前置增强-->
        <aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.method())"></aop:before>
    </aop:aspect>
</aop:config>
  1. 测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
    @Autowired
    private TargetInterface target;
    @Test
    public void test1(){
        target.method();
    }
}

3. XML配置AOP的详解

1. 切点表达式

表达式语法:

execution([修饰符] 返回值类型 包名.类名.方法名(参数))
  • 访问修饰符可以省略
  • 返回值类型、包名、类名、方法名可以使用星号* 代表任意
  • 包名与类名之间一个点 . 代表当前包下的类,两个点 … 表示当前包及其子包下的类
  • 参数列表可以使用两个点 … 表示任意个数,任意类型的参数列表

例子:

execution(public void com.itheima.aop.Target.method())	<!-- 对特定的method方法增强 -->
execution(void com.itheima.aop.Target.*(..)) <!-- 对Target类下的所有方法增强 -->
execution(* com.itheima.aop.*.*(..)) <!-- 对aop包下面的所有类的所有方法增强,不包括aop->包->类的这些方法 -->
execution(* com.itheima.aop..*.*(..)) <!-- 包括aop->包->类的方法 -->
execution(* *..*.*(..)) <!-- 所有方法增强 -->

2. 通知(增强)的类型

<aop:通知类型 method="切面类的某个方法" pointcut="切点表达式"></aop:通知类型>

有以下五种:

  1. 前置通知 <aop:before>
  2. 后置通知 <aop:after-returning>
  3. 环绕通知 <aop:around>
  4. 异常抛出通知 <aop:throwing>
  5. 最终通知 <aop:after>

3. 切点表达式的抽取

当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。

<aop:config>
    <!--引用myAspect的Bean为切面对象-->
    <aop:aspect ref="myAspect">
        <aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
        <aop:before method="before" pointcut-ref="myPointcut"></aop:before>
    </aop:aspect>
</aop:config>

基于注解的 AOP 开发


1. 快速入门

  1. 创建目标接口和目标类(内部有切点)
  2. 创建切面类(内部有增强方法)
  3. 将目标类和切面类的对象创建权交给 spring
  4. 在切面类中使用注解配置织入关系
  5. 在配置文件中开启组件扫描和 AOP 的自动代理
  6. 测试

2. 代码实现

  1. 创建目标接口和目标类(内部有切点)
public interface TargetInterface {
    public void method();
}

public class Target implements TargetInterface {
    @Override
    public void method() {
        System.out.println("Target running....");
    }
}
  1. 创建切面类(内部有增强方法)
public class MyAspect {
    //前置增强方法
    public void before(){
        System.out.println("前置代码增强.....");
    }
}
  1. 将目标类和切面类的对象创建权交给 spring
@Component("target")
public class Target implements TargetInterface {
    @Override
    public void method() {
        System.out.println("Target running....");
    }
}
@Component("myAspect")
public class MyAspect {
    public void before(){
        System.out.println("前置代码增强.....");
    }
}
  1. 在切面类中使用注解配置织入关系
@Component("myAspect")
@Aspect // 一定要标识是个切面类
public class MyAspect {
    @Before("execution(* com.itheima.aop.*.*(..))")
    public void before(){
        System.out.println("前置代码增强.....");
    }
}
  1. 在配置文件中开启组件扫描和 AOP 的自动代理
<!--组件扫描-->
<context:component-scan base-package="com.itheima.aop"/>

<!--aop的自动代理, 如果不写这个, 织入相当于没写 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

3. 注解配置 AOP 详解

1. 注解通知类型

通知的配置语法:@通知注解(“切点表达式")

  1. 前置通知 @Before
  2. 后置通知 @AfterReturning
  3. 环绕通知 @Around
  4. 异常抛出通知 @AfterThrowing
  5. 最终通知 @After

2. 切点表达式的抽取

@Component("myAspect")
@Aspect
public class MyAspect {
    @Before("MyAspect.myPoint()")
    public void before(){
        System.out.println("前置代码增强.....");
    }
    // 方法内部什么都不用写,只是用来写注解
    // 使用方法可以使MyAspect.myPoint()
    // 也可以是MyPoint()
    @Pointcut("execution(* com.itheima.aop.*.*(..))")
    public void myPoint(){}
}

JdbcTemplate基本使用


1. JdbcTemplate的开发步骤

  1. 导入spring-jdbc和spring-tx坐标 => spring-tx是事务方面的包
  2. 创建数据库表和实体
  3. 创建JdbcTemplate对象
  4. 执行数据库操作

2. JdbcTemplate的快速入门代码

  1. 导入坐标
<?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.itheima</groupId>
  <artifactId>itheima_spring_jdbc</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>itheima_spring_jdbc Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  <dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.32</version>
    </dependency>
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.10</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.2.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.0</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.3</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
  </dependencies>
</project>
  1. 创建数据库表和实体
    在这里插入图片描述
package com.itheima.domain;

public class Account {

    private String name;
    private double money;

    public String getNa me() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}
  1. 创建JdbcTemplate对象
    执行数据库操作
@Test
    //测试JdbcTemplate开发步骤
    public void test1() throws PropertyVetoException {
        //创建数据源对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUser("root");
        dataSource.setPassword("root");

        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        //设置数据源对象  知道数据库在哪
        jdbcTemplate.setDataSource(dataSource);
        //执行操作
        int row = jdbcTemplate.update("insert into account values(?,?)", "tom", 5000);
        System.out.println(row);

    }

3. spring产生JdbcTemplate模板对象代码

	<!--数据源对象-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <!--jdbc模板对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

4. 抽取数据库连接配置

<!-- 配置文件 -->
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
<!-- 引入context的命名空间和约束路径 -->

    <!--加载jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--数据源对象-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--jdbc模板对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

5. JdbcTemplate的常用操作

1. 更新操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

	// 插入操作上面全是,这里不写了    

	//修改更新
    @Test
    public void testUpdate(){
        jdbcTemplate.update("update account set money=? where name=?",10000,"tom");
    }
	//删除
    @Test
    public void testDelete(){
        jdbcTemplate.update("delete from account where name=?","tom");
    }

}

2. 查询操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
	//聚合查询
    @Test
    public void testQueryCount(){
        Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        System.out.println(count);
    }
	//查询一个
    @Test
    public void testQueryOne(){
    	// BeanPropertyRowMapper的泛型写实体,参数写实体的class
        Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "tom");
        System.out.println(account);
    }
	//查询所有
    @Test
    public void testQueryAll(){
        List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(accountList);
    }

声明式事务控制


1. 编程式事务控制相关对象(了解它的对象)

  1. PlatformTransactionManager
  2. TransactionDefinition
  3. TransactionStatus

PlatformTransactionManager是spring的平台事物管理器,提供常用的操作事务的方法

  • 获取事务的状态信息
  • 提交事务
  • 回滚事务
    本身是一个接口,对于不同的Dao层实现有不同的实现类

TransactionDefinition是事务的定义信息对象

  • 获取事务的隔离级别
  • 获得事务的传播行为
  • 获得超时时间
  • 是否只读

TransactionStatus 接口提供的是事务具体的运行状态

  • 是否存储回滚点
  • 事务是否完成
  • 是否是新事务
  • 事务是否回滚

2. 基于 XML 的声明式事务控制

Spring 声明式事务控制底层是AOP

  1. 需要知道谁是切点
  2. 谁是通知(增强)
  3. 配置切面
<!-- 引入tx(事务)命名空间 -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!--事务增强配置-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    	<!-- tx:method相当于切点,对谁增强,有参数 -->
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>

<!--事务的aop增强-->
<aop:config>
    <aop:pointcut id="myPointcut" expression="execution(* com.itheima.service.impl.*.*(..))"/>
    <!-- pointcut中的方法被增强,增强的方式在txAdvice中配置 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"></aop:advisor>
</aop:config>

tx:method的参数

  • name:切点方法名称
  • isolation:事务的隔离级别
  • propogation:事务的传播行为
  • timeout:超时时间
  • read-only:是否只读

3. 基于注解的声明式事务控制

  1. Dao层代码
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void out(String outMan, double money) {
        jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
    }
    public void in(String inMan, double money) {
        jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
    }
}
  1. Service层代码
@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

	// 只需要加这个注解
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public void transfer(String outMan, String inMan, double money) {
        accountDao.out(outMan,money);
        int i = 1/0;
        accountDao.in(inMan,money);
    }
}
  1. 编写 applicationContext.xml 配置文件
<!-- 除去上面的,还需要组件扫描和事务的注解驱动 -->
<!--组件扫描-->
<context:component-scan base-package="com.itheima"/>
<!--事务的注解驱动-->
<tx:annotation-driven/>

PS:

  • 使用 @Transactional 在需要进行事务控制的类或是方法上修饰
  • 在类上配置所有方法一套配置,同时在类和方法上,方法的配置优先
  • xml配置文件中要开启事务的注解驱动<tx:annotation-driven />

本文地址:https://blog.csdn.net/weixin_43795939/article/details/107488128

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网