Spring核心原理

3 篇文章 0 订阅

Spring是一个开放源代码的JavaEE设计层面框架,解决了业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用,它的核心是IOC控制反转和AOP面向切面编程。

七大模块:core、aop、orm、dao、web、context、web mvc。

IOC

控制反转,面向对象的设计原则,用来降低代码之间的耦合度。它有一种Bean对象容器,帮我们创建、管理、装配对象,即bean管理。

原理:xml解析、工厂模式、反射

提供IOC容器的接口:BeanFactory、ApplicationContext(加载配置文件时创建对象)。

ApplicationContext接口实现类:FileSystemXmlApplicationContext、ClassPathXmlApplicationContext。

实现IOC的方法:依赖注入。

bean管理:创建对象、注入属性(装配)。

bean管理的两种方式:xml配置、注解。

两种Bean:普通bean、工厂bean。

bean的作用域:singleton单例(默认)、prototype原型、request、session、application。

<bean id="user" class="com.xx.xxx.user" scope="propotype">
<!--原型,调用getBean方法时创建不同的对象-->
</bean>

Bean的生命周期:

1.执行无参构造创建bean
2.调用set方法设置属性值
3.执行初始化方法(postProcess)
4.获取bean实例
5.调用销毁方法销毁
xml配置创建对象

配置文件中配置bean。

<bean id="user" class="com.xx.xxx.user" name="别名">

id:唯一标识

class:全类名

默认通过无参构造创建对象。

有参构造创建:1)通过对象构造器参数下标 2)通过参数名 3)通过参数类型

<bean id="user" class="com.xx.xxx.user">
    <!--第一个参数设值-->
    <constructor-arg index="0" value="xx">
    <!--通过参数名设值-->
	<constructor-arg name="xx" value="xx">
	<constructor-arg type="int" value="xx">
</bean>
xml配置注入属性

1)set方法。

2)配置文件中配置属性注入。

属性注入方式:property标签 、collection集合、p命名空间 、c命名空间。

(1)property配置:

<bean id="user" class="com.xx.xxx.user" name="别名">
    <!--属性名,值-->
    <property name="xxx" value="xxx"/>
    <!--null注入-->
    <property name="xxx">
    	<null/>   
    </property>
    <!--特殊符号注入-->
    <!--转义或CDATA-->
    <property name="xxx">
    	<value><![CDATA[<<年龄>>]]></value>   
    </property>
    
    <!--外部Bean注入,ref指向哪个bean-->
    <!--类中需定义对象属性,set方法-->
    <property name="xxx" ref="xxx"/>
    <!--内部Bean注入(对象注入)-->
    <property name="xxx">
 		<bean id="dept" class="com.xx.xx.Dept">
        	<property name="dname" value="xx"></property>
        </bean>
    </property>
<!--通过级联赋值,对多个对象属性赋值,需要设置对象属性的get方法-->
    <!--<property name="dept.dname" value="xx"/>-->
    
    <!--数组注入-->
    <property name="xxx">
    	<array>
        	<value>1</value>
            <value>2</value>
        </array>    
    </property>
   <!--map注入-->
    <property name="xxx">
    	<map>
            <entry key="x" value="x"></entry>
        </map>    
    </property>
    <!--list类型注入-->
    <property name="xxx">
    	<list>
        	<value>1</value>
            <value>2</value>
        <!--list中存对象<ref bean="外部bean对象"></ref>-->
        </list>    
    </property>
</bean>

测试获取bean对象:

ApplicationContext  context = new ClassPathXmlApplicationContext("beans.xml");
xxx x = (xxx)context.getBean("xxx");

(2)p命名空间注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="                   http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="user" class="com.xx.xx.user" p:name="xx" p:age="12"/>
</beans>

(3)c命名空间注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="                   http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--constructor构造器参数注入-->
<!--需要有参构造-->
<bean id="user" class="com.xx.xx.user" c:name="xx" c:age="12"/>
</beans>

(4)公共集合属性提取

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="                   http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd">
	<util:list id="booklist">
   		 <value>xxx</value>
	</util:util>
    <bean id="book" class="com.xx.xx.book">
        <property name="list" ref="booklist"></property>
    </beans>
</beans>
自动装配

spring根据属性名称和属性类型自动将bean的属性值注入。

byName:默认,在容器中寻找set方法中对应的Beand的id,id唯一。

byType:寻找和自己属性类型相同的对象,class唯一。

<bean id="pet" class="com.xx.xx.pet"/>
<bean id="people" class="com.xx.xxx.user" autowire="byName">
    <!--<property name="name" ref="pet"/>-->
</bean>
引入外部属性文件

引入数据库连接池配置

1、引入依赖

2、配置context命名空间

<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="                   http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context		https://www.springframework.org/schema/context/spring-context.xsd">

3、引入外部文件jdbc.properties

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

4、配置连接池

<bean id="dbs" class="com.alibaba.druid.pool.DruidDataSource" autowire="byName">
    <property name="driverClassName" value="${prop.driverClass}"></property>
    <property name="url" value="${prop.url}"></property>
    <property name="username" value="${prop.userName}"></property>
    <property name="password" value="${prop.password}"></property>
</bean>
注解创建对象

bean管理中使用注解创建对象,在要放入spring容器的对象上加上@Component注解,按照mvc架构,它的衍生注解有@Repository@Service@Controller,属性值设置使用@Value(相当于property配置)。

1)引入aop依赖。

2)添加context命名空间,开启注解扫描。

<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="                   http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context		https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.xxx"/>
</beans>

3)在类上添加相关的bean注解,@Component(value="xx")

默认beanid为类名,首字母小写。value值设置beanid。

其他注解:

@Nullable,在方法上表示返回值可以为空;用在方法的参数上表示参数可空;用在属性上表示属性可为空。

@Scope : 设置作用域。@Scope(“singleton”):单例模式。

@Configuration : 代表这是一个配置类,就像我们之前看的beans.xml。配合@Bean使用将实体类注册,本质是一个@Component

@Bean : 注册一个bean,就相当于我们之前写的一个bean标签;这个方法的名字,就相当于bean标签中的id属性;这个方法的返回值,就相当于bean标签中的class属性。

@Import : @Import(XXX.class)可以引入其他配置类,使其合并为一个总配置类,使用时通过AnnotationConfig上下文来获取总配置类即可。

@Data : Lombok中的注解,放在实体类上会自己创建set和get方法,toString方法等。

自定义扫描哪些注解:

<context:component-scan base-package="com.xxx" use-default-filter="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context>
</context:component-scan>
注解注入属性

常用注解:Autowired、Qualified、Resource、Value(普通类型注入)。

1)添加context命名约束

2)开启注解配置

<context:component-scan base-package="com.xxx"/>
<context:annotation-config/>

3)添加@Autowired,根据属性类型实现自动装配

通过属性名称完成属性装配,使用@Qualified(value="xxx")注解指定bean对象。@Resource注解也可以完成自动装配,通过beanid和class类型寻找匹配,名称注入通过name。@Value注解对基本数据类型进行注入。

@Autowired与@Resource的不同

1)来源不同
@Autowired是Spring定义的注解,而@Resource是JSR-250定义的注解。

2)参数不同
@Autowired只包含一个参数:required,表示是否开启自动准入,默认是true。而@Resource包含七个参数,其中最重要的两个参数是:name 和 type。@Autowired如果要使用byName,需要使用@Qualifier一起配合。而@Resource如果指定了name,则用byName自动装配,如果指定了type,则用byType自动装配。

3)使用不同
@Autowired能够用在:构造器、方法、参数、成员变量和注解上,而@Resource能用在:类、成员变量和方法上。

4)装配顺序
@Autowired默认按byType自动装配,而@Resource默认byName自动装配。

完全注解开发

Config配置类代替xml文件实现自动装配,实际使用springboot也能完成类似操作。

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.ComponentScan

在一个类上加上注解@Configuration 代表这是一个配置类,配置类中加上@ComponentScan注解,扫描要注入的bean对象包,替代xml配置文件。

ApplicationContext  context = new AnnotationConfigApplicationContext(config.class);
AOP

面向切面编程,通过预编译方式和动态代理实现程序功能统一维护的一种技术。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

动态代理分为两大类:基于接口的动态代理基于类的动态代理

  • 基于接口 : JDK动态代理
  • 基于类 : cglib
  • 通过反射实现

专业术语:

横切关注点:跨越应用程序多个模块的方法和功能,如日志、安全、缓存、事务。
切面aspect:增强的过程,被模块化的特殊对象,它是一个类。
通知advice:需要增强的代码逻辑部分。
切入点pointcut:实际增强的方法,必须定义。
连接点jointpoint:可增强的方法。
目标target:被通知的对象。
代理proxy:向目标对象通知后创建的对象。

使用AOP需要导入一个依赖包 :

<!--https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>   
<groupId>org.aspectj</groupId>   <artifactId>aspectjweaver</artifactId>   <version>1.9.4</version>
</dependency>
方式一 : 通过JDK接口实现

创建UserService接口和实现类UserServiceImpl,创建实现类的代理对象,并实现aop切入实现,注意导入约束。

通过Proxy的newProxyInstance方法创建代理对象。

import java.lang.reflect.Proxy;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationHandler;

public class MyProxy{
 public static void main(String[] args){
 	Class[] interfaces = {UserService.class};
 	UserSerivce us = (UserService)Proxy.newProxyInstance(MyProxy.class.getClassLoader(),interfaces,new InvocationHandler(){
 	@Override
 	public Object invoke(Object proxy,Method method,
 		Object[] args) throws Throwable{
 		return null;
 		}
 	})
 }
}
方式二 : 通过AspectJ实现AOP

AspectJ是一个独立的AOP框架,结合spring实现aop操作。需要引入aop、aspectj、cglib、aopalliance依赖。

切入点表达式:

execution([权限修饰符] [返回类型] [全类名] [方法名] ([参数列表]))

例:expression="execution(* com.xxx.xx.UserServiceImpl.*(..))

(1)xml方式实现:

1)创建类及其增强类(代理对象类)

2)添加aop命名空间

3)xml中配置切入点

定义类:

public class book {   
    public void buy(){     
    	System.out.println("---buy-----");  
    }  
}

定义增强类:

public class Proxy {   
    public void before(){     
    	System.out.println("----方法执行前------");  
    }  
    public void after(){     
    	System.out.println("----方法执行后------"); 
    }
}

4)在Spring配置中增强(切入)

<!--创建两个类的对象-->
<bean id="book" class="com.xx.config.book"/>
<bean id="Proxy" class="com.xx.config.Proxy"/>
<!--aop的配置-->
<aop:config>   
<!--切入点--> 
<aop:pointcut id="P" 
  expression="execution(* com.xx.config.book.buy(..))"/>
<!--切面,增强类-->
<aop:aspect ref="Proxy">	
 <!--advice增强方法-->
 <aop:before pointcut-ref="p" method="before"/>     	  <aop:after pointcut-ref="p" method="after"/>   	</aop:aspect>
</aop:config>

(2)注解方式实现:

@Aspect注解表示该类为为增强类或代理对象类。

1)创建类及其增强类(代理对象类)

2)添加aop命名空间

3)开启注解扫描和生成代理对象

<?xml version="1.0" encoding="UTF-8"?>
<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/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context		https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop		https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.xxx"/>
<bean id="annotationProxy" class="com.xxx.config.AnnotationProxy"/>
<!--开启aspectj代理对象生成-->
<aop:aspectj-autoproxy/>
</beans>

aop:aspectj-autoproxy有一个proxy-target-class属性,默认为false,默认使用jdk动态代理。当为true,表示使用CGLib动态代理。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。

注:@EnableAspectJAutoProxy注解可以实现代理对象生成,效果一样。

4)使用注解创建类及增强类的对象

使用注解配置不同类型的advice通知(增强逻辑)。

@Aspect
public class AnnotationProxy{
    @Before(
    "execution(* com.x.service.UserServiceImpl.*(..))") 
    public void before(){     
    	System.out.println("------方法执行前------");
    }   
    @After(
    "execution(* com.x.service.UserServiceImpl.*(..))")  
    public void after(){       
    	System.out.println("-----方法执行后-------"); 
    }   
    @Around(
    "execution(* com.x.service.UserServiceImpl.*(..))") 
    public void around(ProceedingJoinPoint jp) throws Throwable {   
    	System.out.println("环绕前");  
   		System.out.println("签名:"+jp.getSignature());   
  		 //执行目标方法proceed     
    	Object proceed = jp.proceed();     
    	System.out.println("环绕后");    
        System.out.println(proceed); 
    }
}

五种advice通知(增强)类型:

@Before前置通知。

@Around环绕通知

@After最终通知

@AfterRetuning后置通知

@AfterThrowing异常通知

环绕之前...
before...
目标方法执行...
环绕之后...
after...
afterReturning...

提取公共切入点:定义一个方法,添加注解@Pointcut(value="切入点表达式"),在advice注解设置value值为方法名()。当有多个增强类对同一切入点增强,使用@Order()注解设置增强类优先级。

JdbcTemplate

它是spring对jdbc的封装。

1)导入mysql、druid、jdbc、tx、orm依赖包。

2)配置数据库连接池

在引入外部文件中已经配置过。

3)配置JdbcTemplate对象

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
</bean>

4)开启注解扫描,在dao层注入jdbcTemplate对象

Spring事务管理

Spring在不同的事务管理API之上定义了一个抽象层,使得开发人员不必了解底层的事务管理器API。Spring的声明事务管理底层原理使用aop。

Spring支持编程式事务管理和声明式的事务管理。声明式的事务管理有两种实现方式:注解和xml配置。

注解方式

1)使用Spring管理事务,导入tx事务依赖,命名空间导入 : 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/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context		https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop		https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

2)创建事务管理器

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">      
<property name="dataSource" ref="dataSource" />
</bean>

3)开启事务注解

<tx:annotation-driven transaction-manager
 = "transactionManager"/>   

4)在service类上添加事务注解

@Transactional,添加在类上,类中方法开启事务。spring定义了7种事务传播行为。

xml方式

1)配置事务管理器

2)配置事务通知

<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">   
<tx:attributes>      
<!--配置哪些方法使用什么样的事务,配置事务的传播特性-->     
<tx:method name="add" propagation="REQUIRED"/>       <tx:method name="delete" propagation="REQUIRED"/>       <tx:method name="update" propagation="REQUIRED"/>       <tx:method name="search*" propagation="REQUIRED"/>   
 <!--该方法只读,无法对数据库进行操作-->  
<tx:method name="get" read-only="true"/>  
<tx:method name="*" propagation="REQUIRED"/>   </tx:attributes>
</tx:advice>

3)配置aop,切入点、切面

<!--配置aop事务-->
<aop:config>   
<aop:pointcut id="txPointcut" 
    expression="execution(* com.x.dao.*.*(..))"/>   
<!--配置切面-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>

完全注解开发

使用@Configuration@ComponentScan@EnableTransactionManager@Bean注解完成事务。创建数据库连接池,事务管理器,jdbcTemplate。

整合Mybatis

编写实体类

public class User { 
	private int id;  
	private String name;    
	private String pwd; 
}

编写mybatis-config核心配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"       "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>  
<typeAliases>      
<package name="com.x.pojo"/> 
</typeAliases>   
<environments default="development">    
<environment id="development">          
<transactionManager type="JDBC"/>      
<dataSource type="POOLED">            
<property name="driver" value="com.mysql.jdbc.Driver"/>     <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8"/>             
<property name="username" value="root"/>               <property name="password" value="123456"/>           </dataSource>      
</environment>   
</environments>   
<mappers>       
<package name="com.x.dao"/>   
</mappers>
</configuration>

编写UserDao接口

public interface UserMapper {  
	public List<User> selectUser();
}

编写接口对应的Mapper映射文件

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.dao.UserMapper"> 
    <select id="selectUser" resultType="User">   
    select * from user   
    </select>
</mapper>

导入spring-webmvc、spring-jdbc、mybatis、mybatis-spring、mysql-connector-java、aspectjweaver包。

配置Maven静态资源过滤问题:

<build>   
<resources>      
<resource>         
<directory>src/main/java</directory>       
<includes>            
<include>**/*.properties</include>               <include>**/*.xml</include>         
</includes>        
<filtering>true</filtering>      
</resource>  
</resources>
</build>

Mybatis-Spring

MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。它将允许 MyBatis 参与到 Spring 的事务管理之中,创建映射器 mapper 和 SqlSession 并注入到 bean 中,以及将 Mybatis 的异常转换为 Spring 的 DataAccessException。最终,可以做到应用代码不依赖于 MyBatis,Spring 或 MyBatis-Spring。

在 MyBatis-Spring 中,可使用SqlSessionFactoryBean来创建 SqlSessionFactory。要配置这个工厂 bean,只需要把下面代码放在 Spring 的 XML 配置文件中:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
<property name="dataSource" ref="dataSource" />
</bean>
实现一

引入Spring配置文件beans.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>

配置数据源替换mybatis数据源

<!--配置数据源:可以用第三方的,也可用Spring的-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>   
<property name="username" value="root"/>  
<property name="password" value="123456"/>
</bean>

配置SqlSessionFactory,关联Mybatis

<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">   <property name="dataSource" ref="dataSource"/>   
<!--关联Mybatis-->  
<property name="configLocation" value="classpath:mybatis-config.xml"/>   
<property name="mapperLocations" value="classpath:com/kuang/dao/*.xml"/>
</bean>

注册sqlSessionTemplate,关联sqlSessionFactory

<!--注册sqlSessionTemplate , 关联sqlSessionFactory-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 
<!--利用构造器注入--> 
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

增加Dao接口的实现类;私有化sqlSessionTemplate

public class UserDaoImpl implements UserMapper {   //sqlSession不用我们自己创建了,Spring来管理  
private SqlSessionTemplate sqlSession;   
public void setSqlSession(SqlSessionTemplate sqlSession) {       this.sqlSession = sqlSession; 
}   
public List<User> selectUser() {   
UserMapper mapper = sqlSession.getMapper(UserMapper.class);     return mapper.selectUser();  
}
}

注册bean实现

<bean id="userDao" class="com.x.dao.UserDaoImpl">   <property name="sqlSession" ref="sqlSession"/>
</bean>

当前Mybatis配置文件内容:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>   
<typeAliases>    
<package name="com.x.pojo"/>  
</typeAliases>
</configuration>

测试

@Test
public void test2(){    
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");    
UserMapper mapper = (UserMapper) context.getBean("userDao");  List<User> user = mapper.selectUser();    System.out.println(user);}
实现二

dao继承Support类 , 直接利用 getSqlSession() 获得 , 然后直接注入SqlSessionFactory . 比起方式一 , 不需要管理SqlSessionTemplate , 而且对事务的支持更加友好。

将UserDaoImpl修改一下

public class UserDaoImpl extends SqlSessionDaoSupport implements UserMapper {  
public List<User> selectUser() {   
UserMapper mapper = getSqlSession().getMapper(UserMapper.class);     
return mapper.selectUser(); 
}}

修改bean的配置

<bean id="userDao" class="com.xx.dao.UserDaoImpl">   <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
WebFlux函数式编程

spring5的新功能,类似springMVC,用于web开发,流行的异步非阻塞的响应式编程框架,基于Reactor的api实现。

  • 20
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值