Spring详细使用总结
文章目录
- Spring
- 1 基本使用
- 2 Bean的装配
- 3 注入/DI
- 3.1 基于XML的DI
- 3.2 基于注解的DI
- 4 AOP概要
- 5 AspectJ对AOP的实现
- 5.1 通知类型
- 5.2 切入点表达式
- 5.3 基于注解的实现
- 5.4 基于XML的实现
- 6 Spring实现AOP
- 7 集成MyBatis
- 8 Spring事务
- 8.1 事务管理器接口
- 8.2 Spring 的回滚方式
- 8.3 事务定义接口
- 8.4 相关程序
- 8.5 使用 Spring 的事务注解管理事务
- 8.6 使用 AspectJ 的 AOP 配置管理事务
- 9 Spring与Web
- 10 其他
Spring
1 基本使用
pom文件
<dependencies><!--核心依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.6.RELEASE</version></dependency>
</dependencies>
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"><!--导入分支配置文件--><import resource="classpath:spring-school.xml"/><!--自定义对象scope:指定作用域--><bean id="someService" class="com.beiyin.service.SomeServiceImpl" scope="prototype"/><!--非自定义对象--><bean id="myDate" class="java.util.Date"/>
</beans>
Test
@Testpublic void test6() {// 指定配置文件的位置和名称String resource = "applicationContext.xml";// String resource = "D:\\applicationContext.xml";// 创建spring容器对象// 配置文件在根路径下ApplicationContext ac = new ClassPathXmlApplicationContext(resource);// 配置文件在磁盘路径下// ApplicationContext ac = new FileSystemXmlApplicationContext(resource);// 从容器对象中获得beanSomeService someService = (SomeService) ac.getBean("someService");// 执行方法someService.doSome();Date myDate = (Date) ac.getBean("myDate");System.out.println(myDate.toString());}
2 Bean的装配
默认调用无参构造器,创建实例对象。
Bean 的作用域
- singleton 单例模式,默认;
- prototype 原型模式,每次调用,获得新的实例;
- request 每次HTTP请求,产生新的实例,只能用于Web应用;
- session 不同的HTTP session,产生不同的实例,只能用于Web应用。
3 注入/DI
对 bean 对象的属性进行初始化。
3.1 基于XML的DI
设置注入和构造注入
<?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="school" class="com.beiyin.service.School"/><!--设值注入(使用setter/getter方法)--><bean id="student" class="com.beiyin.service.Student"><!--基本类型--><property name="name" value="ding"/><property name="age" value="20"/><!--引用类型--><property name="school" ref="school"/><!--使用ref标签--><!--<property name="school">--><!--<ref bean="school"/>--><!--</property>--></bean><!--构造注入--><bean id="myStudent" class="com.beiyin.service.Student"><constructor-arg name="name" value="ding"/><constructor-arg name="age" value="14"/><constructor-arg name="school" ref="school"/></bean></beans>
引用类型自动注入
byName
<?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="school" class="com.beiyin.service.School"/><!--设值注入(使用setter/getter方法)--><bean id="student" class="com.beiyin.service.Student" autowire="byName"><!--基本类型--><property name="name" value="ding"/><property name="age" value="20"/></bean>
</beans>
byType
<?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"><!--注意同byName的区别--><bean id="mySchool" class="com.beiyin.service.School"/><!--设值注入(使用setter/getter方法)--><bean id="student" class="com.beiyin.service.Student" autowire="byType"><!--基本类型--><property name="name" value="ding"/><property name="age" value="20"/></bean>
</beans>
3.2 基于注解的DI
配置组件扫描器
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><import resource="classpath:spring-school.xml"/><!--配置组件扫描器,扫描多个包--><context:component-scan base-package="com.beiyin.service,com.beiyin.dao"/>
</beans>
创建bean
// 指定bean的id
@Component("someService")
public class SomeServiceImpl implements SomeService {public SomeServiceImpl(){System.out.println("无参构造");}@Overridepublic void doSome() {System.out.println("实现doSmoe方法");}
}
- @Component:创建基本的bean;
- @Repository:创建持久层bean;
- @Service:创建业务层bean;
- @Component:创建控制层bean。
基本类型属性注入
public class Student {@Value("张三") // 无需setterprivate String name;
}
引用类型属性byType自动注入
@Component("mySchool")
public class School {
}public class Student {@Autowired // 按类型自动注入,无需setterprivate School school;
}
引用类型属性byName自动注入
public class Student {@Autowired // 按名称自动注入,无需setter@Qualifier("mySchool")private School school;
}
required
public class Student {@Autowired(required = false) // 当注入失败时,赋予null值private School school;
}
@Resource
默认按名称,找不到时再按类型。
public class Student {@Resource(name = "mySchool") // 指定名称private School school;
}
4 AOP概要
AOP 为 Aspect Oriented Programming 的缩写,意为:面向切面编程,可通过运行期动态代理实现程序功能的统一维护的一种技术。
优势 :
- 减少重复;
- 专注业务;
AOP术语
- 切面(Aspect ):泛指交叉业务逻辑,比如事务处理/日志处理。
- 连接点(JoinPoint ):可以被切面织入的具体方法。通常业务接口中的方法均为连接点。
- 切入点(Pointcut):指声明的一个或多个连接点的集合。
- 目标对象(Target ):将要被增强的对象。即包含主业务逻辑的类的对象。
- 通知(Advice ):通知是切面的一种实现,可以完成简单织入功能(织入功能就是在这里完成的)。
5 AspectJ对AOP的实现
5.1 通知类型
- 前置通知
- 后置通知
- 环绕通知
- 异常通知
- 最终通知
5.2 切入点表达式
execution ( [modifiers-pattern] 访问权限类型 ret-type-pattern 返回值类型 [declaring-type-pattern] 全限定性类名 name-pattern(param-pattern)方法名(参数类型和参数个数) [throws-pattern] 抛出异常类型 )
符号 | 意义 |
---|---|
* | 0至多个任意字符 |
… | 方法参数中表示任意多个参数;包名后表示当前包及子包 |
+ | 用在类/接口名后,表示当前类/接口及其子类/实现类 |
execution(public * *(..))
指定切入点为:任意公共方法。
execution(* set*(..))
指定切入点为:任何一个以“set”开始的方法。
execution(* com.xyz.service.*.*(..))
指定切入点为:定义在 service 包里的任意类的任意方法。
execution(* com.xyz.service..*.*(..))
指定切入点为:定义在 service 包或者子包里的任意类的任意方法。“..”出现在类名中时,后
面必须跟“*”,表示包、子包下的所有类。
execution(* *..service.*.*(..))
指定所有包下的 serivce 子包下所有类(接口)中所有方法为切入点
execution(* *.service.*.*(..))
指定只有一级包下的 serivce 子包下所有类(接口)中所有方法为切入点
execution(* *.ISomeService.*(..))
指定只有一级包下的 ISomeSerivce 接口中所有方法为切入点
execution(* *..ISomeService.*(..))
指定所有包下的 ISomeSerivce 接口中所有方法为切入点
execution(* com.xyz.service.IAccountService.*(..))
指定切入点为:IAccountService 接口中的任意方法。
execution(* com.xyz.service.IAccountService+.*(..))
指定切入点为:IAccountService 若为接口,则为接口中的任意方法及其所有实现类中的任意方法;若为类,则为该类及其子类中的任意方法。
execution(* joke(String,int)))
指定切入点为:所有的 joke(String,int)方法,且 joke()方法的第一个参数是 String,第二个参数是 int。如果方法中的参数类型是 java.lang 包下的类,可以直接使用类名,否则必须使用全限定类名,
如 joke( java.util.List, int)。
execution(* joke(String,*)))
指定切入点为:所有的 joke()方法,该方法第一个参数为 String,第二个参数可以是任意类型,
如joke(String s1,String s2)和joke(String s1,double d2)都是,
但joke(String s1,double d2,String s3)不是。
execution(* joke(String,..)))
指定切入点为:所有的 joke()方法,该方法第一个参数为 String,后面可以有任意个参数且参数类型不限,如 joke(String s1)、joke(String s1,String s2)和 joke(String s1,double d2,String s3)都是。
execution(* joke(Object))
指定切入点为:所有的 joke()方法,方法拥有一个参数,且参数是 Object 类型。joke(Object ob)是,但,joke(String s)与 joke(User u)均不是。
execution(* joke(Object+)))
指定切入点为:所有的 joke()方法,方法拥有一个参数,且参数是 Object 类型或该类的子类。不仅 joke(Object ob)是,joke(String s)和 joke(User u)也是。
5.3 基于注解的实现
pom.xml
<!--AOP依赖-->
<dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>4.3.16.RELEASE</version>
</dependency>
java实现
// 接口
public interface OneService {int sayHello(String name,int age);
}// 实现类
@Component
public class OneServiceImpl implements OneService {@Overridepublic int sayHello(String name, int age) {// 实现抛出异常// age = 1/0;System.out.println("hello" +name);return age + 5;}
}// 切面类
@Component
@Aspect
public class MyAspect {/*** 前置通知* @param joinPoint 代表切入点表达式,可获取切入带你表达式/方法签名/目标对象,所有通知都可包含*/@Before(value = "execution(* com.beiyin.service.aop.OneServiceImpl.*(..))")public void before(JoinPoint joinPoint){// 连接点的方法定义Signature signature = joinPoint.getSignature();System.out.println(signature);/*int com.beiyin.service.aop.OneService.sayHello(String,int)*/// 方法参数信息Object[] args = joinPoint.getArgs();for (Object arg : args) {System.out.println(arg);/*丁15*/}System.out.println("前置通知");}/***后置通知* @param result 定义在注解属性returning中,代表执行结果*/@AfterReturning(value = "execution(* com.beiyin.service.aop.OneServiceImpl.*(..))",returning = "result")public void after(Object result){// 获得目标方法执行结果System.out.println("执行结果:"+result);System.out.println("后置通知");}/*** 环绕通知* @param pjp 用于执行目标方法* @return* @throws Throwable*/@Around(value ="execution(* com.beiyin.service.aop.OneServiceImpl.*(..))")public Object around(ProceedingJoinPoint pjp) throws Throwable {System.out.println("环绕通知:在目标前");// 执行目标方法Object proceed = pjp.proceed();// 修改目标执行结果proceed = 5;System.out.println("环绕通知:在目标后");return proceed;}/*** 异常通知 (注意:只有在切入点的方法发生异常时,才会执行)* @param ex 异常对象*/@AfterThrowing(value ="execution(* com.beiyin.service.aop.OneServiceImpl.*(..))",throwing = "ex")public void throwing(Throwable ex){System.out.println("发生了异常;"+ex.getMessage());}/*** 最终通知*/@After(value = "pt()")public void after(){System.out.println("无论如何,都会执行");}/*** 定义切入点,简化配置*/@Pointcut(value = "execution(* com.beiyin.service.aop.OneServiceImpl.*(..))")private void pt(){}
}
/*无异常通知顺序:
环绕通知:在目标前
前置通知
环绕通知:在目标后
最终通知:一定执行
后置通知
*/
/*有异常通知顺序:
环绕通知:在目标前
前置通知
最终通知:一定执行
异常通知
*/
5.4 基于XML的实现
java
// 目标类
public class OneServiceImpl implements OneService {@Overridepublic int sayHello(String name, int age) {// age = 1/0;System.out.println("hello" +name);return age + 5;}
}// 切面类
public class MyAspect {public void before(JoinPoint joinPoint) {System.out.println("前置通知");}public void afterReturn(Object result) {System.out.println("后置通知");}public Object around(ProceedingJoinPoint pjp) throws Throwable {System.out.println("环绕通知:在目标前");// 执行目标方法Object proceed = pjp.proceed();// 修改目标执行结果proceed = 5;System.out.println("环绕通知:在目标后");return proceed;}public void throwing(Throwable ex) {System.out.println("发生了异常;" + ex.getMessage());}public void after() {System.out.println("无论如何,都会执行");}}
配置文件
<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://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"><!--创建切面对象--><bean id="myAspect" class="com.beiyin.service.aop.MyAspect"/><!--创建目标对象--><bean id="oneServiceImpl" class="com.beiyin.service.aop.OneServiceImpl"/><!--切面配置--><aop:config><!--指定切入点--><aop:pointcut id="pt" expression="execution(* com.beiyin.service.aop.OneServiceImpl.*(..))"/><!--配置通知--><aop:aspect id="ap" ref="myAspect"><!--前置通知--><aop:before method="before" pointcut-ref="pt"/><!--后置通知--><aop:after-returning method="afterReturn" pointcut-ref="pt" returning="result"/><!--环绕通知--><aop:around method="around" pointcut-ref="pt"/><!--异常通知--><aop:after-throwing method="throwing" pointcut-ref="pt" throwing="ex"/><!--最终通知--><aop:after method="after" pointcut-ref="pt"/></aop:aspect></aop:config></beans>
6 Spring实现AOP
java
// 接口
public interface OneService {int sayHello(String name,int age);
}// 实现类
public class OneServiceImpl implements OneService {@Overridepublic int sayHello(String name, int age) {age = 1/0;System.out.println("hello" +name);return age + 5;}
}/*
切面类
必须实现相应的接口
*/
// 前置通知
public class MyBefore implements MethodBeforeAdvice {@Overridepublic void before(Method method, Object[] args, Object target) throws Throwable {System.out.println("前置通知");}
}// 后置通知
public class MyAfter implements AfterReturningAdvice {@Overridepublic void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println("后置通知");}
}// 环绕通知
public class MyAround implements MethodInterceptor {@Overridepublic Object invoke(MethodInvocation invocation) throws Throwable {System.out.println("环绕通知:目标前");Object proceed = invocation.proceed();System.out.println("环绕通知:目标后");return proceed;}
}// 异常通知 该接口没有方法,需要自己写方法,名字必须是afterThrowing
public class MyThrow implements ThrowsAdvice {/**** @param e 测试的是必须只有这一个参数*/public void afterThrowing(Throwable e){System.out.println("异常通知");}
}
配置文件
<?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 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"><!--创建目标对象--><bean id="oneServiceImpl" class="com.beiyin.service.aop.OneServiceImpl"/><!--创建切面对象--><bean id="before" class="com.beiyin.service.aop.sp.MyBefore"/><bean id="after" class="com.beiyin.service.aop.sp.MyAfter"/><bean id="around" class="com.beiyin.service.aop.sp.MyAround"/><bean id="myThrow" class="com.beiyin.service.aop.sp.MyThrow"/><!--定义代理对象--><bean id="proxyOneService" class="org.springframework.aop.framework.ProxyFactoryBean"><!--目标--><property name="target" ref="oneServiceImpl"/><!--实现的接口--><property name="proxyInterfaces" value="com.beiyin.service.aop.OneService"/><!--指定切面对象--><property name="interceptorNames"><list><value>before</value><value>around</value><value>after</value><value>myThrow</value></list></property></bean></beans>
7 集成MyBatis
pom.xml
<dependencies><!--spring核心依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.6.RELEASE</version></dependency><!--spring事务依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.2.6.RELEASE</version></dependency><!--springjdbc依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.6.RELEASE</version></dependency><!--mybatis依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.4</version></dependency><!--spring集成mybatis依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.4</version></dependency><!--连接池依赖--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.12</version></dependency><!--mysql依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.19</version></dependency></dependencies>
java
// 实体类
@Data
public class User {private Integer id;private String name;private Character sex;private Integer age;private String address;
}// dao接口
public interface UserDao {List<User> selectAll();
}// 业务类
public class OneServiceImpl {private UserDao userDao;public void AllName() {List<User> users = userDao.selectAll();StringBuilder stringBuilder = new StringBuilder();users.forEach(user -> stringBuilder.append(user.getName()));System.out.println("所有名字:" + stringBuilder);}// 为了设置注入public OneServiceImpl setUserDao(UserDao userDao) {this.userDao = userDao;return this;}
}
Mapper文件:UserDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.beiyin.dao.UserDao"><select id="selectAll" resultType="com.beiyin.entity.User">select * from user;</select>
</mapper>
mybatis主配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><setting name="logImpl" value="STDOUT_LOGGING"/></settings><typeAliases><package name="com.beiyin.entity"></package></typeAliases><mappers><package name="com.beiyin.dao"></package></mappers>
</configuration>
数据库配置文件
# 注意mysql8的写法
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
jdbc.name=root
jdbc.pwd = root
spring配置
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--引入属性配置文件--><context:property-placeholder location="classpath:jdbc.properties"/><!--配置数据库连接池--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="clone"><!--基本配置--><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.name}"/><property name="password" value="${jdbc.pwd}"/><!--配置初始化大小/最小数/最大数--><property name="initialSize" value="1"/><property name="minIdle" value="1"/><property name="maxActive" value="20"/><!--获取连接最大等待时间--><property name="maxWait" value="60000"/><!--配置检测需要关闭连接时间的间隔--><property name="timeBetweenEvictionRunsMillis" value="60000"/></bean><!--注册工厂Bean--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:mybatis.xml"/></bean><!--动态代理对象--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><property name="basePackage" value="com.beiyin.dao"/></bean><!--业务对象--><bean class="com.beiyin.service.aop.OneServiceImpl" id="oneService"><property name="userDao" ref="userDao"/></bean></beans>
测试
@Testpublic void test1(){String resource = "applicationContext.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(resource);UserDao userDao = (UserDao) ac.getBean("userDao");List<User> users = userDao.selectAll();users.forEach(user-> System.out.println(user));OneServiceImpl oneService = (OneServiceImpl) ac.getBean("oneService");oneService.AllName();}
8 Spring事务
事务原本是数据库中的概念,在 Dao 层。但一般情况下,需要将事务提升到业务层,即 Service 层。
8.1 事务管理器接口
事务管理器是 PlatformTransactionManager 接口对象。其主要用于完成事务的提交、回滚,及获取事务的状态信息。
常用的实现类:
- DataSourceTransactionManager:使用 JDBC 或 MyBatis 进行数据库操作时使用。
- HibernateTransactionManager:使用 Hibernate 进行持久化数据时使用。
8.2 Spring 的回滚方式
Spring 事务的默认回滚方式是:发生运行时异常和 error 时回滚,发生受查(编译)异常时提交。不过,对于受查异常,程序员也可以手工设置其回滚方式。
注意:当程序出现运行时异常时,java不要求必须处理(抛出/捕获),因此,当出现运行时异常时,实质上是未处理的状态,或者说是程序员未预料到的状态,此时就需要回滚。
8.3 事务定义接口
事务定义接口TransactionDefinition中定义了事务描述相关的三类常量:事务隔离级别、事务传播行为、事务默认超时时限,及对它们的操作。
五个事务隔离级别常量
- DEFAULT:采用 DB 默认的事务隔离级别。MySql 的默认为 REPEATABLE_READ; Oracle默认为READ_COMMITTED。
- READ_UNCOMMITTED:读未提交。未解决任何并发问题。
- READ_COMMITTED:读已提交。解决脏读,存在不可重复读与幻读。
- REPEATABLE_READ:可重复读。解决脏读、不可重复读,存在幻读。
- SERIALIZABLE:串行化。不存在并发问题。
七个事务传播行为常量
- PROPAGATION_REQUIRED:指定的方法必须在事务内执行。若当前存在事务,就加入到当前事务中;若当前没有事务,则创建一个新事务。这是默认行为。
- PROPAGATION_REQUIRES_NEW:指定的方法支持当前事务,但若当前没有事务,也可以以非事务方式执行。
- PROPAGATION_SUPPORTS:总是新建一个事务,若当前存在事务,就将当前事务挂起,直到新事务执行完毕。
- PROPAGATION_MANDATORY
- PROPAGATION_NESTED
- PROPAGATION_NEVER
- PROPAGATION_NOT_SUPPORTED
事务默认超时时限
常量 TIMEOUT_DEFAULT 定义了事务底层默认的超时时限,sql 语句的执行时长。比较复杂,一般选择默认。
8.4 相关程序
java
// 实体类
@Data
public class User {private Integer id;private String name;private Character sex;private Integer age;private String address;
}// dao接口
public interface UserDao {List<User> selectAll();int insertByName(User user);int deleteByName(String name);}// 自定义异常
public class MyException extends RuntimeException {public MyException() {}public MyException(String message) {super(message);}
}// 业务类
public class UserOptionImpl implements UserOption {private UserDao userDao;@Overridepublic void userDo() {List<User> users = userDao.selectAll();for (User user : users) {if ("zhao".equals(user.getName())) {int i = userDao.insertByName(user);System.out.println("插入记录数;"+i);}}if (1==1){// throw new MyException("操作user异常");}userDao.deleteByName("liu");}public UserOptionImpl setUserDao(UserDao userDao) {this.userDao = userDao;return this;}
}// 测试@Testpublic void test1(){String resource = "applicationContext.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(resource);UserOption userOption = (UserOption) ac.getBean("userOption");userOption.userDo();}
8.5 使用 Spring 的事务注解管理事务
通过@Transactional 注解方式,可将事务织入到相应 public 方法中,实现事务管理。
@Transactional的可选属性:
- propagation:事务传播属性,默认值为Propagation.REQUIRED。
- isolation:事务的隔离级别,默认值为Isolation.DEFAULT。
- readOnly:设置对数据库只读,默认值为 false。
- timeout:设置本操作与数据库连接的超时时限,单位为秒,默认-1。
- rollbackFor:指定需要回滚的异常类,可以是一个,也可以是数组。
- rollbackForClassName:指定需要回滚的异常类类名。
- noRollbackFor:指定不需要回滚的异常类。
- noRollbackForClassName:指定不需要回滚的异常类类名。
注意:@Transactional只能用于public方法上,用于其他方法不会出错,但也无作用。
spring配置
<?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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://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/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!--引入属性配置文件--><context:property-placeholder location="classpath:jdbc.properties"/><!--配置数据库连接池--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="clone"><!--基本配置--><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.name}"/><property name="password" value="${jdbc.pwd}"/><property name="timeBetweenEvictionRunsMillis" value="60000"/></bean><!--注册工厂Bean--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:mybatis.xml"/></bean><!--动态代理对象--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><property name="basePackage" value="com.beiyin.dao"/></bean><!--定义业务对象--><bean class="com.beiyin.service.UserOptionImpl" id="userOption"><property name="userDao" ref="userDao"/></bean><!--声明事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--声明事务注解驱动--><tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
业务方法
public class UserOptionImpl implements UserOption {private UserDao userDao;@Transactional(rollbackFor = {MyException.class,NullPointerException.class})@Overridepublic void userDo() {List<User> users = userDao.selectAll();for (User user : users) {if ("zhao".equals(user.getName())) {int i = userDao.insertByName(user);System.out.println("插入记录数;"+i);}}if (1==1){// throw new MyException("操作user异常");}userDao.deleteByName("liu");}
}
8.6 使用 AspectJ 的 AOP 配置管理事务
spring配置
<?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:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"><!--引入属性配置文件--><context:property-placeholder location="classpath:jdbc.properties"/><!--配置数据库连接池--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="clone"><!--基本配置--><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.name}"/><property name="password" value="${jdbc.pwd}"/><property name="timeBetweenEvictionRunsMillis" value="60000"/></bean><!--注册工厂Bean--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:mybatis.xml"/></bean><!--动态代理对象--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><property name="basePackage" value="com.beiyin.dao"/></bean><!--定义业务对象--><bean class="com.beiyin.service.UserOptionImpl" id="userOption"><property name="userDao" ref="userDao"/></bean><!--声明事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--配置事务通知--><tx:advice id="userTransaction" transaction-manager="transactionManager"><tx:attributes><!--指定相应方法的事务--><tx:method name="user*" rollback-for="com.beiyin.exception.MyException"/><!--指定除上面个的其他方法的事务--><tx:method name="*" propagation="SUPPORTS"/></tx:attributes></tx:advice><!--aop配置--><aop:config><aop:pointcut id="pt" expression="execution(* com.beiyin.service.UserOptionImpl.*(..))"/><!--声明增强器--><aop:advisor advice-ref="userTransaction" pointcut-ref="pt"/></aop:config>
</beans>
优势: 无需为每一个目标类添加注解配置,只需在spring中统一定义即可。
9 Spring与Web
pom.xml
<dependencies><!--spring容器依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.6.RELEASE</version></dependency><!--spring-web依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.2.6.RELEASE</version></dependency><!--servlet依赖--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><!-- jsp依赖 --><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2.1-b03</version><scope>provided</scope></dependency></dependencies>
java
// 业务类
public class MyService {public void print(){System.out.println("---------");}
}// servlet
public class RegServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 获得ServletContext容器ServletContext context = req.getServletContext();// 获取spring容器第一种方法WebApplicationContext wc = (WebApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);MyService myService = (MyService) wc.getBean("myService");// 获取spring容器第二种方法// WebApplicationContext rc = WebApplicationContextUtils.getRequiredWebApplicationContext(context);// MyService myService = (MyService) rc.getBean("myService");myService.print();// 获得参数String name = req.getParameter("name");String age = req.getParameter("age");System.out.println("name:"+name+" age:"+age);//重定向req.getRequestDispatcher("/result.jsp").forward(req,resp);}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_0.xsd"><display-name>Archetype Created Web Application</display-name><!--注册servlet--><servlet><servlet-name>regServlet</servlet-name><servlet-class>com.RegServlet</servlet-class></servlet><servlet-mapping><servlet-name>regServlet</servlet-name><url-pattern>/reg</url-pattern></servlet-mapping><!--注册监听器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--指定spring配置文件--><context-param><param-name>contextConfigLocation</param-name><!--默认为WEB-INF/applicationContext.xml,但是一般不用--><param-value>classpath:application.xml</param-value></context-param>
</web-app>
index.jsp
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h2>Hello World!</h2>
<form action="reg" method="post">姓名:<input type="text" name="name"/>年龄:<input type="text" name="age"><input type="submit" value="提交">
</form>
</body>
</html>
10 其他
手动获取spring上下文对象
/*** 手动获取Spring上下文和Bean对象** @Author YinWenBing* @Date 2017/1/6 17:07*/
@Component
public class ApplicationUtil implements ApplicationContextAware {private static ApplicationContext applicationContext;/*** 加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware接口,会自动调用该方法** @param applicationContext* @throws BeansException*/@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {ApplicationUtil.applicationContext = applicationContext;}/*** 获取Spring上下文* @return*/public static ApplicationContext getApplicationContext() {return applicationContext;}/*** 获取Spring Bean* @param name* @return* @throws BeansException*/public static Object getBean(String name) throws BeansException {return applicationContext.getBean(name);}
}
手动获取ServletContext
@Component
public class Test {@Autowiredprivate WebApplicationContext webApplicationContext;public ServletContext get(){ServletContext servletContext = webApplicationContext.getServletContext();return servletContext;}
}
关于上下文问题
spring存在一个ApplicationContext容器,servlet技术存在一个ServletContext容器,ApplicationContext中含有ServletContext。
监听器
@Component
public class MyServerContextListener implements ApplicationListener<ContextRefreshedEvent> {@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {// 建立映射表Map<String, String> map = new HashMap<>();ApplicationContext applicationContext = event.getApplicationContext();ServletContext bean = applicationContext.getBean(ServletContext.class);// 将映射表放入上下文域中bean.setAttribute("myMap",map);}
}
如若内容造成侵权/违法违规/事实不符,请联系编程学习网邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
相关文章
- 万万没想到,亚麻也是职场PUA老手....
疫情下全美都在缩招裁员,而Amazon却逆势增长,随之而来的还有人员规模的疯狂扩张:从9月4日~9月14日,Amzon接连宣布扩招,合计将在全美和加拿大增加10w岗位 然而大举招人的背后往往紧跟着换血式裁员——Amazo…...
2024/3/16 15:42:59 - STM32的RTC晶振不起振的原因及解决方法
STM32外部晶振不起振 使用STM32cubemx生成工程不起振,烧录标准库的程序后,晶振启震,再烧录STM32cubemx生成工程,晶振正常启震。 STM32外部晶振不起振 https://blog.csdn.net/qq_33559992/article/details/83009134?utm_medium…...
2024/5/2 7:11:14 - ubuntu 安装 pptp 服务
ubuntu 安装 pptpd 服务 参考链接 安装及配置过程可以参考以下链接: https://gitee.com/null_848_6596/ubuntu_pptpd_vpn https://blog.csdn.net/zwliang98/article/details/108357108 测试验证 /// 在服务端查看pptpd服务 ps -aux | grep pptpwindows10客户端…...
2024/5/2 17:55:09 - vue学习经验总结(双向数据绑定底层原理)
双向绑定原理 vue的双向绑定是由数据劫持结合发布者-订阅者模式实现的,那么什么是数据劫持? vue是如何进行数据劫持的?说白了就是通过Object.defineProperty()来劫持对象属性的setter和getter操作,在数据变动时做你想…...
2024/4/15 14:48:08 - 看完阿里大佬分享的微服务实战文档,让我茅塞顿开,超赞!
前言 很多人对于微服务技术也都有着一些疑虑,比如: 微服务这技术虽然面试的时候总有人提,但作为一个开发,是不是和我关系不大?那不都是架构师的事吗?微服务不都是大厂在玩吗?我们这个业务体量…...
2024/4/23 10:32:06 - 前后端分离场景应用接口安全考虑
前后端分离场景应用接口安全考虑接口安全指标分类指标分析认证和授权防篡改加密传输防重放防模拟必须防模拟怎么办在各个前端上怎么做(TODO)IOS APP (TODO)android APP(TODO)WEB 浏览器(TODO&am…...
2024/4/2 13:38:14 - Spring Data JPA(后续...)
Spring Data JPA 接口继承关系: Repository: CrudRepository package org.springframework.data.repository; import java.util.Optional;NoRepositoryBean public interface CrudRepository<T, ID> extends Repository<T, ID> {<S ex…...
2024/4/3 0:26:27 - CentOs 8设置中文
CentOs 8设置中文 dnf install glibc-langpack-zh.x86_64 #安装中文支持 echo LANGzh_CN.UTF-8 > /etc/locale.conf #修改系统的字符集 source /etc/locale.conf #使立即生效...
2024/3/30 3:24:12 - Jsp+Servlet 简单登录注册查询
注册功能: 制作一个注册页面 用户输入: 用户名密码年龄 注册成功:——>跳转至登录页面进行登录 注册失败:——>文字或其他形式的提示皆可 简易查询: 制作一个查询页面 输入用户名 显示该用户的用户名、密码、年…...
2024/5/1 8:23:56 - 易撰爆文采集+伪原创操作工具
...
2024/3/12 0:34:52 - 干货分享——虹科汽车毫米波雷达测试解决方案
上周的上海国际汽车测试及质量监控展览会上,虹科卫星通信与无线电事业部为大家带来的《汽车毫米波雷达测试解决方案》受到了众多业内人士的好评~ 那么这次演讲到底有哪些干货呢?我们一起来回顾一下吧! 毫米波雷达技术 车载毫米波雷达通过天线…...
2024/3/15 22:18:36 - 解决Postman安装之后,一直是是米白色界面
我们有时候安装好postman之后,有时候就出现米白色的界面什么也不显示,我特意搜索了一下,网上说的都没有解决我这个问题,后来,我记得有时间软件要设置兼容模式 右键属性,找到兼容性,以兼容性运转这个软件,点上对号 要是还是不行,计算机右键属性,打开面板 选择版本号对应兼容模…...
2024/4/28 4:18:26 - 神经网络中偏置的作用
转载自:https://blog.csdn.net/xwd18280820053/article/details/70681750?utm_mediumdistribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.add_param_isCf&depth_1-utm_sourcedistribute.pc_relevant.none-task-blog-BlogCommendFromMac…...
2024/3/16 16:59:39 - 手机NFC识别跟看门狗(韦根26)识别卡片的id区别
手机识别:72372F09; 看门狗:154089330; 将看门狗的十进制转换为十六进制为92F3772,补0凑齐8位:092F3772 每两位分割,得到: 09 2F 37 72 取反,得到 72 37 2F 09 与手机NF…...
2024/4/18 3:23:26 - 第十四课、计算器核心解析算法(下)------------------狄泰软件学院
一、后缀表达式的计算 1、遍历后缀表达式中的数字和运算符 (1)、若当前元素为数字:进栈 (2)、当前元素为运算符: A、从栈中弹出右操作数 B、从栈中弹出左操作数 C、根据符号进行计算 D、将运算结果压…...
2024/3/22 3:02:44 - 判断一个图是否有环
对于无向图 算法1 我们知道对于环1-2-3-4-1,每个节点的度都是2,基于此我们有如下算法(这是类似于有向图的拓扑排序): 求出图中所有顶点的度,删除图中所有度<1的顶点以及与该顶点相关的边,…...
2024/4/24 4:17:35 - 连Linux的开机流程都不了解,怎么好意思说自己是程序员?
想要让一台计算机工作,我们要做的第一件事就是让这台计算机开机。可能一般用户看到的只是一个简单的启动界面,进入用户界面后就可以进行相关的操作了,但是实际上在开机的时候计算机运行了很多程序,而运行的第一个程序就是Boot Loa…...
2024/4/28 9:59:06 - arcgis栅格数据重命名
# -*- coding: utf-8 -*-import arcpywks rC:\Users\HP\Desktop\HB_Qt arcpy.env.workspace wksprint u"**********开始**********"rasters arcpy.ListRasters("*", "tif") for raster in rasters:newName "new_" rasterdataType…...
2024/5/1 8:59:11 - SpringBoot如何切片处理视频大文件
需求: 项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在20G内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以20G来进行限制。 PC端全平台支持,要求支持Windows,Mac,Linu…...
2024/4/9 8:38:45 - 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
bool isPalindrome(int x){long res 0;int tmp x;if(x<0)return false;while (x!0){res res *10 x % 10;x / 10;}if(res tmp){return true;} else{return false;}}注:我是借鉴了网上的大佬才写出这个算法,感觉这些人真的好强啊啊啊啊!…...
2024/3/10 18:24:05
最新文章
- WebGL是啥
WebGL(全写为Web Graphics Library)是一种3D绘图协议,这种绘图技术标准允许把JavaScript和OpenGL ES 2.0结合在一起,通过增加OpenGL ES 2.0的一个JavaScript绑定,WebGL可以为HTML5 Canvas提供硬件3D加速渲染。这样&…...
2024/5/2 19:11:14 - 梯度消失和梯度爆炸的一些处理方法
在这里是记录一下梯度消失或梯度爆炸的一些处理技巧。全当学习总结了如有错误还请留言,在此感激不尽。 权重和梯度的更新公式如下: w w − η ⋅ ∇ w w w - \eta \cdot \nabla w ww−η⋅∇w 个人通俗的理解梯度消失就是网络模型在反向求导的时候出…...
2024/3/20 10:50:27 - OpenCV单通道图像按像素成倍比例放大(无高斯平滑处理)
OpenCV中的resize函数可以对图像做任意比例的放大(/缩小)处理,该处理过程会对图像做高斯模糊化以保证图像在进行放大(/缩小)后尽可能保留源图像所展现的具体内容(消除固定频率插值/采样带来的香农采样信息损失)&#x…...
2024/5/1 13:33:49 - JVM(Java虚拟机)
文章目录 一、JVM简介1.1 JVM概念1.2 什么是Java虚拟机呢?Java虚拟机的好处是什么呢? 二、JVM整体组成部分三、类加载器3.1 类加载子系统3.2 类加载过程3.2.1 装载(Load)3.2.2 链接(Link)3.2.3 初始化(Initialize) 四、运行时数据区4.1 方法区࿰…...
2024/5/1 13:49:25 - 416. 分割等和子集问题(动态规划)
题目 题解 class Solution:def canPartition(self, nums: List[int]) -> bool:# badcaseif not nums:return True# 不能被2整除if sum(nums) % 2 ! 0:return False# 状态定义:dp[i][j]表示当背包容量为j,用前i个物品是否正好可以将背包填满ÿ…...
2024/5/2 11:19:01 - 【Java】ExcelWriter自适应宽度工具类(支持中文)
工具类 import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet;/*** Excel工具类** author xiaoming* date 2023/11/17 10:40*/ public class ExcelUti…...
2024/5/2 16:04:58 - Spring cloud负载均衡@LoadBalanced LoadBalancerClient
LoadBalance vs Ribbon 由于Spring cloud2020之后移除了Ribbon,直接使用Spring Cloud LoadBalancer作为客户端负载均衡组件,我们讨论Spring负载均衡以Spring Cloud2020之后版本为主,学习Spring Cloud LoadBalance,暂不讨论Ribbon…...
2024/5/1 21:18:12 - TSINGSEE青犀AI智能分析+视频监控工业园区周界安全防范方案
一、背景需求分析 在工业产业园、化工园或生产制造园区中,周界防范意义重大,对园区的安全起到重要的作用。常规的安防方式是采用人员巡查,人力投入成本大而且效率低。周界一旦被破坏或入侵,会影响园区人员和资产安全,…...
2024/5/2 9:47:31 - VB.net WebBrowser网页元素抓取分析方法
在用WebBrowser编程实现网页操作自动化时,常要分析网页Html,例如网页在加载数据时,常会显示“系统处理中,请稍候..”,我们需要在数据加载完成后才能继续下一步操作,如何抓取这个信息的网页html元素变化&…...
2024/5/2 9:47:31 - 【Objective-C】Objective-C汇总
方法定义 参考:https://www.yiibai.com/objective_c/objective_c_functions.html Objective-C编程语言中方法定义的一般形式如下 - (return_type) method_name:( argumentType1 )argumentName1 joiningArgument2:( argumentType2 )argumentName2 ... joiningArgu…...
2024/5/2 6:03:07 - 【洛谷算法题】P5713-洛谷团队系统【入门2分支结构】
👨💻博客主页:花无缺 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅! 本文由 花无缺 原创 收录于专栏 【洛谷算法题】 文章目录 【洛谷算法题】P5713-洛谷团队系统【入门2分支结构】🌏题目描述🌏输入格…...
2024/5/2 9:47:30 - 【ES6.0】- 扩展运算符(...)
【ES6.0】- 扩展运算符... 文章目录 【ES6.0】- 扩展运算符...一、概述二、拷贝数组对象三、合并操作四、参数传递五、数组去重六、字符串转字符数组七、NodeList转数组八、解构变量九、打印日志十、总结 一、概述 **扩展运算符(...)**允许一个表达式在期望多个参数࿰…...
2024/5/1 11:24:00 - 摩根看好的前智能硬件头部品牌双11交易数据极度异常!——是模式创新还是饮鸩止渴?
文 | 螳螂观察 作者 | 李燃 双11狂欢已落下帷幕,各大品牌纷纷晒出优异的成绩单,摩根士丹利投资的智能硬件头部品牌凯迪仕也不例外。然而有爆料称,在自媒体平台发布霸榜各大榜单喜讯的凯迪仕智能锁,多个平台数据都表现出极度异常…...
2024/5/2 5:31:39 - Go语言常用命令详解(二)
文章目录 前言常用命令go bug示例参数说明 go doc示例参数说明 go env示例 go fix示例 go fmt示例 go generate示例 总结写在最后 前言 接着上一篇继续介绍Go语言的常用命令 常用命令 以下是一些常用的Go命令,这些命令可以帮助您在Go开发中进行编译、测试、运行和…...
2024/5/1 20:22:59 - 用欧拉路径判断图同构推出reverse合法性:1116T4
http://cplusoj.com/d/senior/p/SS231116D 假设我们要把 a a a 变成 b b b,我们在 a i a_i ai 和 a i 1 a_{i1} ai1 之间连边, b b b 同理,则 a a a 能变成 b b b 的充要条件是两图 A , B A,B A,B 同构。 必要性显然࿰…...
2024/5/2 9:47:28 - 【NGINX--1】基础知识
1、在 Debian/Ubuntu 上安装 NGINX 在 Debian 或 Ubuntu 机器上安装 NGINX 开源版。 更新已配置源的软件包信息,并安装一些有助于配置官方 NGINX 软件包仓库的软件包: apt-get update apt install -y curl gnupg2 ca-certificates lsb-release debian-…...
2024/5/2 9:47:27 - Hive默认分割符、存储格式与数据压缩
目录 1、Hive默认分割符2、Hive存储格式3、Hive数据压缩 1、Hive默认分割符 Hive创建表时指定的行受限(ROW FORMAT)配置标准HQL为: ... ROW FORMAT DELIMITED FIELDS TERMINATED BY \u0001 COLLECTION ITEMS TERMINATED BY , MAP KEYS TERMI…...
2024/5/2 0:07:22 - 【论文阅读】MAG:一种用于航天器遥测数据中有效异常检测的新方法
文章目录 摘要1 引言2 问题描述3 拟议框架4 所提出方法的细节A.数据预处理B.变量相关分析C.MAG模型D.异常分数 5 实验A.数据集和性能指标B.实验设置与平台C.结果和比较 6 结论 摘要 异常检测是保证航天器稳定性的关键。在航天器运行过程中,传感器和控制器产生大量周…...
2024/5/2 8:37:00 - --max-old-space-size=8192报错
vue项目运行时,如果经常运行慢,崩溃停止服务,报如下错误 FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 因为在 Node 中,通过JavaScript使用内存时只能使用部分内存(64位系统&…...
2024/5/2 9:47:26 - 基于深度学习的恶意软件检测
恶意软件是指恶意软件犯罪者用来感染个人计算机或整个组织的网络的软件。 它利用目标系统漏洞,例如可以被劫持的合法软件(例如浏览器或 Web 应用程序插件)中的错误。 恶意软件渗透可能会造成灾难性的后果,包括数据被盗、勒索或网…...
2024/5/2 9:47:25 - JS原型对象prototype
让我简单的为大家介绍一下原型对象prototype吧! 使用原型实现方法共享 1.构造函数通过原型分配的函数是所有对象所 共享的。 2.JavaScript 规定,每一个构造函数都有一个 prototype 属性,指向另一个对象,所以我们也称为原型对象…...
2024/5/1 14:33:22 - C++中只能有一个实例的单例类
C中只能有一个实例的单例类 前面讨论的 President 类很不错,但存在一个缺陷:无法禁止通过实例化多个对象来创建多名总统: President One, Two, Three; 由于复制构造函数是私有的,其中每个对象都是不可复制的,但您的目…...
2024/5/2 18:46:52 - python django 小程序图书借阅源码
开发工具: PyCharm,mysql5.7,微信开发者工具 技术说明: python django html 小程序 功能介绍: 用户端: 登录注册(含授权登录) 首页显示搜索图书,轮播图࿰…...
2024/5/2 7:30:11 - 电子学会C/C++编程等级考试2022年03月(一级)真题解析
C/C++等级考试(1~8级)全部真题・点这里 第1题:双精度浮点数的输入输出 输入一个双精度浮点数,保留8位小数,输出这个浮点数。 时间限制:1000 内存限制:65536输入 只有一行,一个双精度浮点数。输出 一行,保留8位小数的浮点数。样例输入 3.1415926535798932样例输出 3.1…...
2024/5/1 20:56:20 - 配置失败还原请勿关闭计算机,电脑开机屏幕上面显示,配置失败还原更改 请勿关闭计算机 开不了机 这个问题怎么办...
解析如下:1、长按电脑电源键直至关机,然后再按一次电源健重启电脑,按F8健进入安全模式2、安全模式下进入Windows系统桌面后,按住“winR”打开运行窗口,输入“services.msc”打开服务设置3、在服务界面,选中…...
2022/11/19 21:17:18 - 错误使用 reshape要执行 RESHAPE,请勿更改元素数目。
%读入6幅图像(每一幅图像的大小是564*564) f1 imread(WashingtonDC_Band1_564.tif); subplot(3,2,1),imshow(f1); f2 imread(WashingtonDC_Band2_564.tif); subplot(3,2,2),imshow(f2); f3 imread(WashingtonDC_Band3_564.tif); subplot(3,2,3),imsho…...
2022/11/19 21:17:16 - 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机...
win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”问题的解决方法在win7系统关机时如果有升级系统的或者其他需要会直接进入一个 等待界面,在等待界面中我们需要等待操作结束才能关机,虽然这比较麻烦,但是对系统进行配置和升级…...
2022/11/19 21:17:15 - 台式电脑显示配置100%请勿关闭计算机,“准备配置windows 请勿关闭计算机”的解决方法...
有不少用户在重装Win7系统或更新系统后会遇到“准备配置windows,请勿关闭计算机”的提示,要过很久才能进入系统,有的用户甚至几个小时也无法进入,下面就教大家这个问题的解决方法。第一种方法:我们首先在左下角的“开始…...
2022/11/19 21:17:14 - win7 正在配置 请勿关闭计算机,怎么办Win7开机显示正在配置Windows Update请勿关机...
置信有很多用户都跟小编一样遇到过这样的问题,电脑时发现开机屏幕显现“正在配置Windows Update,请勿关机”(如下图所示),而且还需求等大约5分钟才干进入系统。这是怎样回事呢?一切都是正常操作的,为什么开时机呈现“正…...
2022/11/19 21:17:13 - 准备配置windows 请勿关闭计算机 蓝屏,Win7开机总是出现提示“配置Windows请勿关机”...
Win7系统开机启动时总是出现“配置Windows请勿关机”的提示,没过几秒后电脑自动重启,每次开机都这样无法进入系统,此时碰到这种现象的用户就可以使用以下5种方法解决问题。方法一:开机按下F8,在出现的Windows高级启动选…...
2022/11/19 21:17:12 - 准备windows请勿关闭计算机要多久,windows10系统提示正在准备windows请勿关闭计算机怎么办...
有不少windows10系统用户反映说碰到这样一个情况,就是电脑提示正在准备windows请勿关闭计算机,碰到这样的问题该怎么解决呢,现在小编就给大家分享一下windows10系统提示正在准备windows请勿关闭计算机的具体第一种方法:1、2、依次…...
2022/11/19 21:17:11 - 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”的解决方法...
今天和大家分享一下win7系统重装了Win7旗舰版系统后,每次关机的时候桌面上都会显示一个“配置Windows Update的界面,提示请勿关闭计算机”,每次停留好几分钟才能正常关机,导致什么情况引起的呢?出现配置Windows Update…...
2022/11/19 21:17:10 - 电脑桌面一直是清理请关闭计算机,windows7一直卡在清理 请勿关闭计算机-win7清理请勿关机,win7配置更新35%不动...
只能是等着,别无他法。说是卡着如果你看硬盘灯应该在读写。如果从 Win 10 无法正常回滚,只能是考虑备份数据后重装系统了。解决来方案一:管理员运行cmd:net stop WuAuServcd %windir%ren SoftwareDistribution SDoldnet start WuA…...
2022/11/19 21:17:09 - 计算机配置更新不起,电脑提示“配置Windows Update请勿关闭计算机”怎么办?
原标题:电脑提示“配置Windows Update请勿关闭计算机”怎么办?win7系统中在开机与关闭的时候总是显示“配置windows update请勿关闭计算机”相信有不少朋友都曾遇到过一次两次还能忍但经常遇到就叫人感到心烦了遇到这种问题怎么办呢?一般的方…...
2022/11/19 21:17:08 - 计算机正在配置无法关机,关机提示 windows7 正在配置windows 请勿关闭计算机 ,然后等了一晚上也没有关掉。现在电脑无法正常关机...
关机提示 windows7 正在配置windows 请勿关闭计算机 ,然后等了一晚上也没有关掉。现在电脑无法正常关机以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!关机提示 windows7 正在配…...
2022/11/19 21:17:05 - 钉钉提示请勿通过开发者调试模式_钉钉请勿通过开发者调试模式是真的吗好不好用...
钉钉请勿通过开发者调试模式是真的吗好不好用 更新时间:2020-04-20 22:24:19 浏览次数:729次 区域: 南阳 > 卧龙 列举网提醒您:为保障您的权益,请不要提前支付任何费用! 虚拟位置外设器!!轨迹模拟&虚拟位置外设神器 专业用于:钉钉,外勤365,红圈通,企业微信和…...
2022/11/19 21:17:05 - 配置失败还原请勿关闭计算机怎么办,win7系统出现“配置windows update失败 还原更改 请勿关闭计算机”,长时间没反应,无法进入系统的解决方案...
前几天班里有位学生电脑(windows 7系统)出问题了,具体表现是开机时一直停留在“配置windows update失败 还原更改 请勿关闭计算机”这个界面,长时间没反应,无法进入系统。这个问题原来帮其他同学也解决过,网上搜了不少资料&#x…...
2022/11/19 21:17:04 - 一个电脑无法关闭计算机你应该怎么办,电脑显示“清理请勿关闭计算机”怎么办?...
本文为你提供了3个有效解决电脑显示“清理请勿关闭计算机”问题的方法,并在最后教给你1种保护系统安全的好方法,一起来看看!电脑出现“清理请勿关闭计算机”在Windows 7(SP1)和Windows Server 2008 R2 SP1中,添加了1个新功能在“磁…...
2022/11/19 21:17:03 - 请勿关闭计算机还原更改要多久,电脑显示:配置windows更新失败,正在还原更改,请勿关闭计算机怎么办...
许多用户在长期不使用电脑的时候,开启电脑发现电脑显示:配置windows更新失败,正在还原更改,请勿关闭计算机。。.这要怎么办呢?下面小编就带着大家一起看看吧!如果能够正常进入系统,建议您暂时移…...
2022/11/19 21:17:02 - 还原更改请勿关闭计算机 要多久,配置windows update失败 还原更改 请勿关闭计算机,电脑开机后一直显示以...
配置windows update失败 还原更改 请勿关闭计算机,电脑开机后一直显示以以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!配置windows update失败 还原更改 请勿关闭计算机&#x…...
2022/11/19 21:17:01 - 电脑配置中请勿关闭计算机怎么办,准备配置windows请勿关闭计算机一直显示怎么办【图解】...
不知道大家有没有遇到过这样的一个问题,就是我们的win7系统在关机的时候,总是喜欢显示“准备配置windows,请勿关机”这样的一个页面,没有什么大碍,但是如果一直等着的话就要两个小时甚至更久都关不了机,非常…...
2022/11/19 21:17:00 - 正在准备配置请勿关闭计算机,正在准备配置windows请勿关闭计算机时间长了解决教程...
当电脑出现正在准备配置windows请勿关闭计算机时,一般是您正对windows进行升级,但是这个要是长时间没有反应,我们不能再傻等下去了。可能是电脑出了别的问题了,来看看教程的说法。正在准备配置windows请勿关闭计算机时间长了方法一…...
2022/11/19 21:16:59 - 配置失败还原请勿关闭计算机,配置Windows Update失败,还原更改请勿关闭计算机...
我们使用电脑的过程中有时会遇到这种情况,当我们打开电脑之后,发现一直停留在一个界面:“配置Windows Update失败,还原更改请勿关闭计算机”,等了许久还是无法进入系统。如果我们遇到此类问题应该如何解决呢࿰…...
2022/11/19 21:16:58 - 如何在iPhone上关闭“请勿打扰”
Apple’s “Do Not Disturb While Driving” is a potentially lifesaving iPhone feature, but it doesn’t always turn on automatically at the appropriate time. For example, you might be a passenger in a moving car, but your iPhone may think you’re the one dri…...
2022/11/19 21:16:57