赞
踩
正则表达式匹配
拿到这个题目的时候,就可以知道“ . ”表示匹配任意一个字符,“ * ”表示匹配任意多个前面那个字符。
首先判断字符规律中是不是只含有“ .* ”,是的话就一定会匹配成功
然后判断是不是含有" . "或者“ * ”,如果不含有就先匹配字符串和字符规律的长度,长度不相等那么一定匹配失败。
最后判断其他情况
=========================================================================
编程式事务控制指的是通过写代码的方式对事务进行控制
1.编程式事务控制相关对象
①PlatformTransactionManager(平台事务管理器)
所以在使用声明式事务控制的时候就需要告诉spring我使用的是哪一种实现技术!
②TransactionDefinition(封装事务的信息)
(1)事务的隔离级别
(2)事务的传播行为(在解决业务方法调用的时候的统一性的问题)
【REQUIRED:A业务方法调用B业务方法的时候,如果A业务方法没有事务,B为他新建一个事务;如果A业务方法有事务,B就加入进去!】
③TransactionStatus(事务的状态对象)
2.基于XML的声明式事务控制
①什么是声明式事务控制?
采用声明的方式来处理事务。这里所说的声明就是指在配置文件中声明,用在Spring配置文件中声明式的处理事务来代替代码式的处理事务。
②声明式事务控制的作用
a. 事务管理不侵入开发的组件。
b.在不需要事务管理的时候,只要在设定文件上修改一下,就可以移去事务管理服务,无需改变代码重新编译,这样维护起来很方便
【Spring声明事务控制底层就是AOP】
【典型案例:账户管理】
声明式事务控制明确事项:
··谁是切点?
··谁是通知?
··配置切面?
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"
- xmlns:aop="http://www.springframework.org/schema/aop"
- 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/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
- ">
-
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <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>
-
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="dataSource"/>
- </bean>
-
- <bean id="accountDao" class="dao.AccountDaoImpl">
- <property name="jdbcTemplate" ref="jdbcTemplate"/>
- </bean>
-
- <!--目标i对象 内部的方法就是我们的切点-->
- <bean id="accountService" class="service.impl.AccountServiceImpl">
- <property name="accountDao" ref="accountDao"/>
- </bean>
-
- <!--配置平台事务管理器-->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
-
- <!--通知 事务的增强-->
- <!--transaction-manager指的就是平台事务管理器-->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="*"/>
- </tx:attributes>
- </tx:advice>
-
- <!--配置事务AOP的织入-->
- <aop:config>
- <!--aop:advisor是专门为事务的增强提供的-->
- <aop:advisor advice-ref="txAdvice" pointcut="execution(* service.impl.*.*(..))"></aop:advisor>
- </aop:config>
- </beans>

Account.java
- package domain;
-
- public class Account {
-
- private String name;
- private double money;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public double getMoney() {
- return money;
- }
-
- public void setMoney(double money) {
- this.money = money;
- }
- }

知识要点:
声明式事务控制的配置要点:平台事务管理器配置→事务通知的配置→事务AOP织入的配置
3. 基于注解的声明式事务控制
4. Spirng集成web层
- package com.wxy.web;
-
- import com.wxy.service.UserService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- public class UserServlet extends HttpServlet {
-
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
- UserService userService = app.getBean(UserService.class);
- userService.save();
- }
- }

应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件)方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件),这样的弊端是配置文件加载多次,应用上下文对象创建多次。
那么就可以使用监听器的方式!
在Web项目中,可以使用ServletContextListener监听Web应用的启动, 我们可以在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicatiotiContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。
设置如下:
ContextLoaderListener.java
- package com.wxy.listener;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
-
- public class ContextLoaderListener implements ServletContextListener {
- @Override
- public void contextInitialized(ServletContextEvent servletContextEvent) {
- ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
- //将Spring的应用上下文对象存储到ServletContext域中
- ServletContext servletContext = servletContextEvent.getServletContext();
- servletContext.setAttribute("app",app);
-
- }
-
- @Override
- public void contextDestroyed(ServletContextEvent servletContextEvent) {
-
- }
- }

UserServlet.java
- package com.wxy.web;
-
- import com.wxy.service.UserService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- public class UserServlet extends HttpServlet {
-
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- // ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
- ServletContext servletContext = req.getServletContext();
- ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
- UserService userService = app.getBean(UserService.class);
- userService.save();
- }
- }

web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/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_4_0.xsd"
- version="4.0">
-
- <!--配置监听器-->
- <listener>
- <listener-class>com.wxy.listener.ContextLoaderListener</listener-class>
- </listener>
-
- <servlet>
- <servlet-name>UserServlet</servlet-name>
- <servlet-class>com.wxy.web.UserServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>UserServlet</servlet-name>
- <url-pattern>/userServlet</url-pattern>
- </servlet-mapping>
-
- </web-app>

优化!
上面就会限定加载文件只能是applicationContext.xml文件,那么我们可以配置的方式,让他后期修改只用在web.xml文件中进行修改就可以了
ContextLoaderListener.java
- package com.wxy.listener;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
-
- public class ContextLoaderListener implements ServletContextListener {
- @Override
- public void contextInitialized(ServletContextEvent servletContextEvent) {
- ServletContext servletContext = servletContextEvent.getServletContext();
- //读取web.xml中的全局参数
- String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
-
- ApplicationContext app=new ClassPathXmlApplicationContext("contextConfigLocation");
- //将Spring的应用上下文对象存储到ServletContext域中
-
- servletContext.setAttribute("app",app);
-
- }
-
- @Override
- public void contextDestroyed(ServletContextEvent servletContextEvent) {
-
- }
- }

web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/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_4_0.xsd"
- version="4.0">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>applicationContext.xml</param-value>
- </context-param>
-
- <!--配置监听器-->
- <listener>
- <listener-class>com.wxy.listener.ContextLoaderListener</listener-class>
- </listener>
-
- <servlet>
- <servlet-name>UserServlet</servlet-name>
- <servlet-class>com.wxy.web.UserServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>UserServlet</servlet-name>
- <url-pattern>/userServlet</url-pattern>
- </servlet-mapping>
-
- </web-app>

在上面的基础上,我还可以修改应用上下文到最大域的名字,否则需要用户记住名字!
新创建一个工具类,就是用于获取名字
WebApplicationContextUtils.xml
- package com.wxy.listener;
-
- import org.springframework.context.ApplicationContext;
-
- import javax.servlet.ServletContext;
-
- public class WebApplicationContextUtils {
-
- public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
- return (ApplicationContext) servletContext.getAttribute("app");
- }
-
- }
UserService.java
- package com.wxy.web;
-
- import com.wxy.listener.WebApplicationContextUtils;
- import com.wxy.service.UserService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- public class UserServlet extends HttpServlet {
-
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- // ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
- ServletContext servletContext = req.getServletContext();
- //ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
- ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
- UserService userService = app.getBean(UserService.class);
- userService.save();
- System.out.println("容器加载");
- }
- }

4.1 Spring提供获取应用上下文的工具
UserServlet.java
- package com.wxy.web;
-
- import com.wxy.service.UserService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
-
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- public class UserServlet extends HttpServlet {
-
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- // ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
- ServletContext servletContext = req.getServletContext();
- //ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
- // ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
-
- WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
- UserService userService = app.getBean(UserService.class);
- userService.save();
- System.out.println("容器加载");
- }
- }

web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/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_4_0.xsd"
- version="4.0">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
-
- <!--配置监听器-->
- <!--<listener>
- <listener-class>com.wxy.listener.ContextLoaderListener</listener-class>
- </listener>-->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
- <servlet>
- <servlet-name>UserServlet</servlet-name>
- <servlet-class>com.wxy.web.UserServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>UserServlet</servlet-name>
- <url-pattern>/userServlet</url-pattern>
- </servlet-mapping>
-
- </web-app>

pom.xml
- <!--添加这个依赖-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>4.3.17.RELEASE</version>
- </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]] )
所以最后的表达式
动态规划问题之青蛙跳台阶问题
一个
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。