当前位置:   article > 正文

学习记录七

学习记录七

一、力扣打卡

正则表达式匹配

拿到这个题目的时候,就可以知道“ . ”表示匹配任意一个字符,“ * ”表示匹配任意多个前面那个字符。

首先判断字符规律中是不是只含有“ .* ”,是的话就一定会匹配成功

然后判断是不是含有" . "或者“ * ”,如果不含有就先匹配字符串和字符规律的长度,长度不相等那么一定匹配失败。 

最后判断其他情况

=========================================================================

二、Spring的事务控制

编程式事务控制指的是通过写代码的方式对事务进行控制

1.编程式事务控制相关对象

①PlatformTransactionManager(平台事务管理器)

所以在使用声明式事务控制的时候就需要告诉spring我使用的是哪一种实现技术!

②TransactionDefinition(封装事务的信息)

(1)事务的隔离级别

        

(2)事务的传播行为(在解决业务方法调用的时候的统一性的问题) 

【REQUIRED:A业务方法调用B业务方法的时候,如果A业务方法没有事务,B为他新建一个事务;如果A业务方法有事务,B就加入进去!】 

③TransactionStatus(事务的状态对象)

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

①什么是声明式事务控制?

采用声明的方式来处理事务。这里所说的声明就是指在配置文件中声明,用在Spring配置文件中声明式的处理事务来代替代码式的处理事务。

②声明式事务控制的作用

a. 事务管理不侵入开发的组件。

b.在不需要事务管理的时候,只要在设定文件上修改一下,就可以移去事务管理服务,无需改变代码重新编译,这样维护起来很方便

【Spring声明事务控制底层就是AOP】

【典型案例:账户管理】

声明式事务控制明确事项:

··谁是切点?

··谁是通知?

··配置切面?

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
  10. ">
  11. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  12. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  13. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
  14. <property name="user" value="root"/>
  15. <property name="password" value="root"/>
  16. </bean>
  17. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  18. <property name="dataSource" ref="dataSource"/>
  19. </bean>
  20. <bean id="accountDao" class="dao.AccountDaoImpl">
  21. <property name="jdbcTemplate" ref="jdbcTemplate"/>
  22. </bean>
  23. <!--目标i对象 内部的方法就是我们的切点-->
  24. <bean id="accountService" class="service.impl.AccountServiceImpl">
  25. <property name="accountDao" ref="accountDao"/>
  26. </bean>
  27. <!--配置平台事务管理器-->
  28. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  29. <property name="dataSource" ref="dataSource"></property>
  30. </bean>
  31. <!--通知 事务的增强-->
  32. <!--transaction-manager指的就是平台事务管理器-->
  33. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  34. <tx:attributes>
  35. <tx:method name="*"/>
  36. </tx:attributes>
  37. </tx:advice>
  38. <!--配置事务AOP的织入-->
  39. <aop:config>
  40. <!--aop:advisor是专门为事务的增强提供的-->
  41. <aop:advisor advice-ref="txAdvice" pointcut="execution(* service.impl.*.*(..))"></aop:advisor>
  42. </aop:config>
  43. </beans>

Account.java 

  1. package domain;
  2. public class Account {
  3. private String name;
  4. private double money;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public double getMoney() {
  12. return money;
  13. }
  14. public void setMoney(double money) {
  15. this.money = money;
  16. }
  17. }

知识要点:

声明式事务控制的配置要点:平台事务管理器配置→事务通知的配置→事务AOP织入的配置 

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

4. Spirng集成web层

  1. package com.wxy.web;
  2. import com.wxy.service.UserService;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. public class UserServlet extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  13. ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
  14. UserService userService = app.getBean(UserService.class);
  15. userService.save();
  16. }
  17. }

应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件)方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件),这样的弊端是配置文件加载多次,应用上下文对象创建多次。

那么就可以使用监听器的方式!

在Web项目中,可以使用ServletContextListener监听Web应用的启动, 我们可以在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicatiotiContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。

设置如下:

ContextLoaderListener.java

  1. package com.wxy.listener;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import javax.servlet.ServletContext;
  5. import javax.servlet.ServletContextEvent;
  6. import javax.servlet.ServletContextListener;
  7. public class ContextLoaderListener implements ServletContextListener {
  8. @Override
  9. public void contextInitialized(ServletContextEvent servletContextEvent) {
  10. ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
  11. //将Spring的应用上下文对象存储到ServletContext域中
  12. ServletContext servletContext = servletContextEvent.getServletContext();
  13. servletContext.setAttribute("app",app);
  14. }
  15. @Override
  16. public void contextDestroyed(ServletContextEvent servletContextEvent) {
  17. }
  18. }

UserServlet.java

  1. package com.wxy.web;
  2. import com.wxy.service.UserService;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import javax.servlet.ServletContext;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. public class UserServlet extends HttpServlet {
  12. @Override
  13. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  14. // ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
  15. ServletContext servletContext = req.getServletContext();
  16. ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
  17. UserService userService = app.getBean(UserService.class);
  18. userService.save();
  19. }
  20. }

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!--配置监听器-->
  7. <listener>
  8. <listener-class>com.wxy.listener.ContextLoaderListener</listener-class>
  9. </listener>
  10. <servlet>
  11. <servlet-name>UserServlet</servlet-name>
  12. <servlet-class>com.wxy.web.UserServlet</servlet-class>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>UserServlet</servlet-name>
  16. <url-pattern>/userServlet</url-pattern>
  17. </servlet-mapping>
  18. </web-app>

优化!

上面就会限定加载文件只能是applicationContext.xml文件,那么我们可以配置的方式,让他后期修改只用在web.xml文件中进行修改就可以了

ContextLoaderListener.java

  1. package com.wxy.listener;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import javax.servlet.ServletContext;
  5. import javax.servlet.ServletContextEvent;
  6. import javax.servlet.ServletContextListener;
  7. public class ContextLoaderListener implements ServletContextListener {
  8. @Override
  9. public void contextInitialized(ServletContextEvent servletContextEvent) {
  10. ServletContext servletContext = servletContextEvent.getServletContext();
  11. //读取web.xml中的全局参数
  12. String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
  13. ApplicationContext app=new ClassPathXmlApplicationContext("contextConfigLocation");
  14. //将Spring的应用上下文对象存储到ServletContext域中
  15. servletContext.setAttribute("app",app);
  16. }
  17. @Override
  18. public void contextDestroyed(ServletContextEvent servletContextEvent) {
  19. }
  20. }

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>applicationContext.xml</param-value>
  9. </context-param>
  10. <!--配置监听器-->
  11. <listener>
  12. <listener-class>com.wxy.listener.ContextLoaderListener</listener-class>
  13. </listener>
  14. <servlet>
  15. <servlet-name>UserServlet</servlet-name>
  16. <servlet-class>com.wxy.web.UserServlet</servlet-class>
  17. </servlet>
  18. <servlet-mapping>
  19. <servlet-name>UserServlet</servlet-name>
  20. <url-pattern>/userServlet</url-pattern>
  21. </servlet-mapping>
  22. </web-app>

在上面的基础上,我还可以修改应用上下文到最大域的名字,否则需要用户记住名字! 

新创建一个工具类,就是用于获取名字

WebApplicationContextUtils.xml
  1. package com.wxy.listener;
  2. import org.springframework.context.ApplicationContext;
  3. import javax.servlet.ServletContext;
  4. public class WebApplicationContextUtils {
  5. public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
  6. return (ApplicationContext) servletContext.getAttribute("app");
  7. }
  8. }

UserService.java

  1. package com.wxy.web;
  2. import com.wxy.listener.WebApplicationContextUtils;
  3. import com.wxy.service.UserService;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. import javax.servlet.ServletContext;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.IOException;
  12. public class UserServlet extends HttpServlet {
  13. @Override
  14. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  15. // ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
  16. ServletContext servletContext = req.getServletContext();
  17. //ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
  18. ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
  19. UserService userService = app.getBean(UserService.class);
  20. userService.save();
  21. System.out.println("容器加载");
  22. }
  23. }

4.1 Spring提供获取应用上下文的工具

UserServlet.java

  1. package com.wxy.web;
  2. import com.wxy.service.UserService;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import org.springframework.web.context.WebApplicationContext;
  6. import org.springframework.web.context.support.WebApplicationContextUtils;
  7. import javax.servlet.ServletContext;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.io.IOException;
  13. public class UserServlet extends HttpServlet {
  14. @Override
  15. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  16. // ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
  17. ServletContext servletContext = req.getServletContext();
  18. //ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
  19. // ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
  20. WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
  21. UserService userService = app.getBean(UserService.class);
  22. userService.save();
  23. System.out.println("容器加载");
  24. }
  25. }

 web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:applicationContext.xml</param-value>
  9. </context-param>
  10. <!--配置监听器-->
  11. <!--<listener>
  12. <listener-class>com.wxy.listener.ContextLoaderListener</listener-class>
  13. </listener>-->
  14. <listener>
  15. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  16. </listener>
  17. <servlet>
  18. <servlet-name>UserServlet</servlet-name>
  19. <servlet-class>com.wxy.web.UserServlet</servlet-class>
  20. </servlet>
  21. <servlet-mapping>
  22. <servlet-name>UserServlet</servlet-name>
  23. <url-pattern>/userServlet</url-pattern>
  24. </servlet-mapping>
  25. </web-app>

pom.xml

  1. <!--添加这个依赖-->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-web</artifactId>
  5. <version>4.3.17.RELEASE</version>
  6. </dependency>

三、算法复习

动态规划算法:

1. 将问题划分为若干子问题

2. 不同于分治法的是各个子问题之间不是相互独立的,一个子问题会依赖于前一个子问题

3. 动态规划算法可以通过画表的方式来实现

动态规划算法常见案例----背包问题

刚刚看了某位博主的文章,对动态规划算法也有了一定的理解!

对于0-1背包问题,我们可以总结为下面的步骤

①确定数组元素的含义

②确定数组之间的关系

③找到初始条件

对于0-1背包问题,我们设数组v[i][j],表示我们所求的价值

然后确定数组元素之间的关系

因为可以装进去也可以不装,所以有两种情况

先是重量判断,可以装进去的前提下会有下面的条件

v[i][j]=max(v[i-1][j],v[i]+v[i-1][j-w[i]] )

所以最后的表达式

动态规划问题之青蛙跳台阶问题

一个

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/709768
推荐阅读
相关标签