2020.05-Study_update.1

week 5.4-5.10

-Study-update
-Mon冒泡算法,选择排序,Spring基础
-Tuefiter过滤器,解决表单提交编码问题,用web.xml配置编码,传递参数
-Wes监听器,session的生命周期控制,Session绑定监听器。idea技巧
-Thu动态绑定
-FriStudy-update
-Sat
-Sun自定义通知类,aop配置

5.4 Monday

冒泡

/**
 * @author lzr
 * @date 2020/5/4 10:03:44
 * @description 冒泡
 */
public class BubbleSortDriver {
    public static void main(String[] args) {
        int[] sums={6,5,234,5,6,7,45,5,2,4,5,6,2,4,6,7,27,8,2,7,245,37,46,456,41};
        bubbleSort(sums);
        for(int i:sums){
            System.out.print(i+"-");
        }
    }
    public static int[] bubbleSort(int[] nums){
        int length=nums.length;
        int temp;
        for(int i=0;i<length-1;i++){
            for(int j=0;j<length-1-i;j++){
                if(nums[j]>nums[j+1]){
                    temp=nums[j];
                    nums[j]=nums[j+1];
                    nums[j+1]=temp;
                }
            }
        }
        return nums;
    }
}
//输出
uction\FirstDemo" BubbleSortDriver
2-2-2-4-4-5-5-5-5-6-6-6-6-7-7-7-8-27-37-41-45-46-234-245-456-
Process finished with exit code 0

选择排序

/**
 * @author lzr
 * @date 2020/5/4 10:28:26
 * @description 选择排序
 */
public class SelectionSortDriver {
    public static void main(String[] args) {
        int[] sums={6,5,234,5,6,7,45,5,2,4,5,6,2,4,6,7,27,8,2,7,245,37,46,456,41};
        selectionSort(sums);
        for(int i:sums){
            System.out.print(i+"-");
        }
    }
    public static int[] selectionSort(int[] nums){
        int temp;
        int length=nums.length;
        for(int i=0;i<length-1;i++){
            int maxIndex=i;
            for(int j=i;j<length;j++){
                if(nums[maxIndex]<nums[j]){
                    maxIndex=j;
                }
            }
            temp=nums[maxIndex];
            nums[maxIndex]=nums[i];
            nums[i]=temp;
        }
        return nums;
    }
}
//输出
456-245-234-46-45-41-37-27-8-7-7-7-6-6-6-6-5-5-5-5-4-4-2-2-2-
Process finished with exit code 0

Spring 是一个容器,用来帮我们管理对象。

使用<bean name=“” class=“”></bean>
来让spring管理类。
Name属性,name可以使用特殊字符,也可以重复(实际不推荐重复)。
Class属性,是被管理对象的全包名,spring通过这个类来创建对象。
id属性,跟name相同,不可以使用特殊字符,不可以重复。

根据spring配置文件获取对象

 public static void main(String[] args) {
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext_annotation.xml");
        User2 u2=context.getBean(User2.class);
        System.out.println(u2.toString());
    }

applicationContext的所有bean会在容器创建时全部加载。
可以用lazy-init属性设置懒加载,将会在对象被获取时才加载该bean.
scope属性 默认singleton单例,prototype多例 ,request 跟request生命周期一样, session 跟session生命周期一样。
Init-method=“”属性在容器创建对象时调用方法。
destroy=“” 属性在容器销毁对象之前调用方法.
<property=“字段名” value=“值”>值类型注入
<property=“引用类型” ref=“”> 引用类型注入(该引用类型得配置bean)
构造方法的注入 用type属性和index属性来确认调用哪个构造方法。

复杂类型的注入

<!--collection复杂类型的注入-->
    <bean name="collection" class="com.maaoooo.collection.MyCollection">
        <!-- Array-->
        <property name="array">
            <array>
                <value>123</value>
                <value>321</value>
                <ref bean="cat"/>
            </array>
        </property>
        <!-- List-->
        <property name="list">
            <list>
                <value>list</value>
                <value>list2</value>
                <ref bean="cat"/>
            </list>
        </property>
        <!-- Set-->
        <property name="set">
            <set>
                <value>1</value>
                <value>2</value>
            </set>
        </property>
        <!-- map-->
        <property name="map">
            <map>
                <entry key="1" value="2"/>
                <entry key="2" value="3"/>
            </map>
        </property>
        <!--properties-->
        <property name="properties">
            <props>
                <prop key="name">小刘</prop>
                <prop key="type">老王</prop>
            </props>
        </property>
    </bean>

5.5 Tuesday

fiter

@WebFilter(filterName = “TestFilter”,urlPatterns = “/*”)
/*代表所有请求都会被过滤
继承自Fiter
init构造时调用,destroy销毁时调用,doFiter是主要方法。

@WebFilter(filterName = "TestFilter",urlPatterns = "/login.jsp")//也可以在web.xml配置,与配置servlet类似。
public class TestFilter implements Filter {
    public void destroy() {
        System.out.println("销毁");
    }
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //过滤客户端发往服务器端的请求
        System.out.println("dofiter");
        ((HttpServletResponse)resp).sendRedirect("index.jsp");
        chain.doFilter(req, resp);
        //chain后的代码是过滤服务器端发往用户的响应
    }
    public void init(FilterConfig config) throws ServletException {
        System.out.println("构造");
    }
}

编码问题
对所有请求进行编码设置

req.setCharacterEncoding("utf-8");

用web.xml配置编码,传递参数

<filter>
        <filter-name>testfiter</filter-name>
        <filter-class>com.maaoooo.filter.TestFilter</filter-class>
        <!--通过配置文件传参数-->
        <init-param>
            <param-name>Encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>testfiter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
public class TestFilter implements Filter {
    private String Encoding;
    public void destroy() {
        System.out.println("销毁");
    }
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //过滤客户端发往服务器端的请求
        req.setCharacterEncoding("utf-8");
        chain.doFilter(req, resp);
        //chain后的代码是过滤服务器端发往用户的响应
    }
    public void init(FilterConfig config) throws ServletException {
        System.out.println("构造");
        //构造时读取配置文件到FilterConfig中
        Encoding=config.getInitParameter("Encoding");
        System.out.println("当前编码:"+Encoding);
    }
}

5.6 Wednesday

监听器
用来监听request session application
httpServletRequest httpSession ServletContext
设置session的生命周期

<session-config>
  <session-timeout>5</session-timeout>//当用户5分钟没有发起请求,销毁session
</session-config>

属性监听器

@WebListener()
public class Listener implements  HttpSessionAttributeListener, ServletContextAttributeListener,ServletRequestAttributeListener {

监听request,session,application的属性设置。可以通过监听器取得属性的数据。

@Override
public void attributeAdded(ServletContextAttributeEvent event) {
    System.out.println("attributeAdded");
    System.out.println("attributeAdd名字是:"+event.getName()+" 值是:"+event.getValue());
}

@Override
public void attributeRemoved(ServletContextAttributeEvent event) {
    System.out.println("attributeRemoved");
}

@Override
public void attributeReplaced(ServletContextAttributeEvent event) {
    System.out.println("attributeReplaced");
}

Session绑定监听器
一般在类中继承,当Session设置该类的类型属性时,监听下的方法启动。

public class Maker implements HttpSessionBindingListener{
@Override
public void valueBound(HttpSessionBindingEvent event) {
    System.out.println("MakerAdd");
}

@Override
public void valueUnbound(HttpSessionBindingEvent event) {

}

idea技巧

/**
 * @author lzr
 * @date 2020/5/7 14:21:38
 * @description
 */
public class SessionManeger {

    private Maker m1;
    private Maker make2;

    public void test(Object m) {
        //变量的声明
        Maker maker = new Maker();//.var
        make2 = new Maker();//.filed全局变量
        Maker maker3 = new Maker();//Maker.new
        Maker maker4 = (Maker) m;//cast
        Maker m5 = (Maker) m;//castvar
        if (m == null) {//.null

        }
        if (m != null) {//.notnull

        }
        if (m != null) {//.nn

        }
        boolean b = true;
        if (b) {//.if

        }
        while (b) {//.while

        }
        //输出/返回
        String name = "123";
        System.out.println(name);//.sout
        //return name;//.return
        //循环
        int[] nums = new int[5];
        for (int i = 0; i < nums.length; i++) {//fori

        }
        for (int num : nums) {//.for

        }
        for (int i = nums.length - 1; i >= 0; i--) {//.forr
        }
    }
}

5.8 Friday

动态代理
代理模式的定义:代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用,通俗的来讲代理模式就是我们生活中常见的中介,动态代理和静态代理的区别在于静态代理我们需要手动的去实现目标对象的代理类,而动态代理可以在运行期间动态的生成代理类。

print接口

/**
 * @author lzr
 * @date 2020 05 08 20:52
 * @description
 */
public interface Print {
    public void print(String para);
}

print实现类

/**
 * @author lzr
 * @date 2020 05 08 20:56
 * @description
 */
public class PrintImpl implements Print{
    public void print(String para) {
        System.out.println("打印"+para);
    }
}

proxyHandle

/**
 * @author lzr
 * @date 2020 05 08 21:02
 * @description
 */
public class ProxyHandle implements InvocationHandler {
    private Object targeObject;
    public Object newProxyInstance(Object targeObject){
        this.targeObject=targeObject;
        return Proxy.newProxyInstance(targeObject.getClass().getClassLoader(),targeObject.getClass().getInterfaces(),this);
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("记录日志");
        return method.invoke(targeObject,args);
    }
}

Test

/**
 * @author lzr
 * @date 2020 05 08 21:18
 * @description
 */
public class Test {
    public static void main(String[] args) {
        Print print=new PrintImpl();
        print.print("hi");
        //---
        ProxyHandle proxyHandle=new ProxyHandle();
        Print print1= ((Print) proxyHandle.newProxyInstance(print));
        print1.print("hi");
    }
}

5.10 Sunday

自定义通知类

/**
 * @author lzr
 * @date 2020 05 10 15:40
 * @description
 */
public class MyAdvice {
    //before 前置通知 在目标方法前调用
    public void before(){
        System.out.println("before");
    }
    //after 最终通知 无论是否出现异常都会执行 后置通知
    public  void after(){
        System.out.println("after");
    }
    //afterReturning成功通知
    public  void afterReturning(){
        System.out.println("afterReturning");
    }
    //afterThrowing异常通知
    public void afterThrowing(){
        System.out.println("afterThrowing");
    }
    //环绕通知 需要我们手动调用目标方法 并且可以设置通知
    public void round(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("before");
        Object o=pjp.proceed();
        System.out.println("after");
    }
}

aop ApplicationContext配置

<?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"
       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
   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">
    <!--目标对象-->
    <bean id="userService" class="com.maaoooo.service.UserServiceImpl"></bean>
    <!--通知对象-->
    <bean id="MyAdvice" class="com.maaoooo.aop.MyAdvice"></bean>

    <aop:config>
        <!--切入点  expression 配置要增强的方法
        public void com.maaoooo.service.UserServiceImpl.save()增强单个方法
        * com.maaoooo.service.*Impl.*(..) 增强com.maaoooo.service.下的以Impl结尾的类的所有方法
                    id 唯一标识-->
        <aop:pointcut id="servicePC" expression="execution(* com.maaoooo.service.*Impl.*(..))"/>
        <!--切面 通知加切入点-->
        <aop:aspect ref="MyAdvice">
            <!--通知类型-->
            <aop:before method="before" pointcut-ref="servicePC"/>
            <aop:after method="after" pointcut-ref="servicePC"/>
            <aop:after-returning method="afterReturning" pointcut-ref="servicePC"/>
            <aop:after-throwing method="afterThrowing" pointcut-ref="servicePC"/>
            <aop:around method="around" pointcut-ref="servicePC"/>
        </aop:aspect>
    </aop:config>
</beans>

测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class TestTest {
    @Resource(name = "userService")
    private UserService us;
    @org.junit.Test
    public void test1() {
        us.find();
    }
}

输出

信息: Refreshing org.springframework.context.support.GenericApplicationContext@548a9f61: startup date [Sun May 10 21:47:35 CST 2020]; root of context hierarchy
before
aroundBefore
find
aroundAfter
afterReturning
after
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值