2.20 Ri issue summary

1.Java What are four ways to create an object?

  1. new keywords
  2. clone
  3. reflection
  4. Object deserialization

For example, there is now a student class, create objects using the above four ways!

  • Student category

    package com.xbky.domain;
    
    import java.io.Serializable;
    
    public class Students implements Cloneable, Serializable {
    
        private int sid; //学号
        private String sname; //姓名
        private String gender; //性别
        private String birthday; //出生日期
        private String major; //专业
    
        //一定要保留这个默认的无参数的构造方法,否则spring也无法实例化。
    
        public Students() {
        }
    
        public Students(int sid, String sname, String gender, String birthday, String major) {
            this.sid = sid;
            this.sname = sname;
            this.gender = gender;
            this.birthday = birthday;
            this.major = major;
        }
    
        public int getSid() {
            return sid;
        }
    
        public void setSid(int sid) {
            this.sid = sid;
        }
    
        public String getSname() {
            return sname;
        }
    
        public void setSname(String sname) {
            this.sname = sname;
        }
    
        public String getGender() {
            return gender;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    
        public String getBirthday() {
            return birthday;
        }
    
        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }
    
        public String getMajor() {
            return major;
        }
    
        public void setMajor(String major) {
            this.major = major;
        }
    
        @Override
        public Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    
        @Override
        public String toString() {
            return "Students{" +
                    "sid=" + sid +
                    ", sname='" + sname + '\'' +
                    ", gender='" + gender + '\'' +
                    ", birthday='" + birthday + '\'' +
                    ", major='" + major + '\'' +
                    '}';
        }
    }
    
  • 4 ways to create objects

    package com.xbky.test;
    
    /*
    * Spring也不使用new来创建对象,是使用ioc容器来注入的。
    *
    * */
    
    import com.xbky.domain.Students;
    
    import java.io.*;
    import java.lang.reflect.Constructor;
    
    public class IocDemo {
    
        public static void main(String[] args) throws Exception {
            File file = new File("dest.data");
    
            Students s1 = new Students(100,"宋小宝","男","1999-10-10","二人转");
            System.out.println(s1);
    
            //使用clone来创建对象。
            Students s2 = (Students) s1.clone();
            System.out.println(s2);
    
            Students s3 = s1;
    
            System.out.println(s1 == s2);
            System.out.println(s3 == s1);
    
            //反射怎么创建对象呢。大家仔细观察我没有使用new.使用了反射。
            Class clazz = s1.getClass();
            Constructor constructor = clazz.getConstructor(int.class,String.class,String.class,String.class,String.class);
            Students s5 = (Students) constructor.newInstance(new Object[]{100,"小沈阳","女","1999-10-10","二人转"});
            System.out.println(s5);
    
            //反序列化创建对象。
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
            out.writeObject(s1); //把宋小宝对象写入到文件中。
    
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            Students s6 = (Students) in.readObject();
            System.out.println(s6);
            System.out.println(s1 == s6);
        }
    }
    

    Spring IOC container using a target injection

    Do not use new, but also by resolving internal reflection + xml document to achieve

    • Create a file in resources applicationContext.xml

         <bean id="s1" class="com.xbky.domain.Students">
              <property name="sid" value="200"/>
              <property name="sname" value="张三丰"/>
              <property name="gender" value="男" />
              <property name="birthday" value="1998-10-10"/>
              <property name="major" value="太极拳" />
          </bean>
    • Creating a class object test injection

      package com.xbky.test;
      
      import com.xbky.domain.Students;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class IocDemo2 {
      
          public static void main(String[] args) {
              //使用了spring ioc 容器注入了一个对象。没有使用new.内部也是通过反射+xml文档的解析来实现的。
              ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
              Students s1 = (Students) context.getBean("s2");
              System.out.println(s1);
          }
      }
      
    • Spring IOC order execution

      1. Under resources to load applicationContext.xml

      2. Parse this xml document

      3. getBean ( "s1") s1 parsed from the document to the object, using the java reflection mechanism, returns an object reflected

        Note: The initialization properties is achieved by setXXX to create an object by calling the default constructor without parameters to achieve, then the requirements of the class must want to keep the default no-argument constructor, or Spring ICO container can not be instantiated object.

    • IOC object initialization step

      1. Load applicationContext.xml
      2. Id resolve the corresponding objects
      3. The default constructor with no arguments, calling reflected by the object is instantiated
      4. By calling setXXX, initialization properties

2. What is maven? What are the core functions of the maven?

  • Apache organization as a highly successful open source project, Maven primarily serving the building project based on java platform, dependent on information management and project management. Whether it is a small open source library projects, or large-scale enterprise applications; whether it is the traditional waterfall development, or popular agile development, Maven can flourish. There Maven dependency management, module management, plug-in management and deployment of four core management function.
  • Usually required libraries: http: //mvnrepository.com/

2.1, dependency management

Examples: reliance Service

<dependencies>
  <dependency>
          <groupId>com.shsxt</groupId>
          <artifactId>wc-service</artifactId>
          <version>0.0.1-SNAPSHOT</version>
  </dependency>
</dependencies>

And then copied to the pom.xml file, remember to write notes and dependencies in the project, it will depend on the preservation

  • The actual project belongs - Company name: groupId
  • artifactId: Module - the name of the project
  • In which the current version of the project: version

2.2, module management

We need to define a parent module as a set of POM POM polymerization in Maven. The labels may be used to define a set of POM submodules. parent POM does not have any actual build output. The parent POM in the build configuration and dependency configuration are automatically inherited by the child module.

E.g. pom.xml submodule configured as follows:

<modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.hbgc</groupId>
        <artifactId>base</artifactId>
        <version>1.0-SNAPSHOT</version>
        <!--父模块的pom.xml位置-->
        <relativePath>../base/pom.xml</relativePath>
    </parent>
    <groupId>com.hbgc</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdemo</name>
    <description>Demo project for Spring Boot</description>

2.3, plug-in management

Adding a plug similar to the adding dependency, specify plug by GAV, wherein for official maven plug can be omitted GroupId. E.g:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.0.0</version>
        </plugin>
    </plugins>
</build>

And version control relies similar, unified management of plug-in version of the project by reference properties and pluginManagement. Note pluginManagement elements need to be placed under the build elements.

E.g:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

2.4 Deployment Manager

In the idea, we can open maven project view. In the list of tested life cycle maven, packaging and installation

3. What is MVC? Talk about the benefits of stratification.

  • MVC is a framework, or is a design pattern, it is the mandatory application of the input, processing and output separately.

  • An application into three parts

    • Model
    • View
    • Controller
  • Schematic

    analysis:

    1. Model Model: (complete business logic: javaBean consists, in three components of MVC, the model has the largest processing task may be treated, for example, it means a database with objects such as EJBs and javabean Since the code applied to the model only. to be written once and can be reused multiple views, so a reduction of repetitive code.)
    2. View View (interface with the user interaction is responsible for the general interface is the HTML, css elements, of course, there are still some, like js, ajax, flex Some also belong to the view layer. No real processing takes place in the view layer, the data output mode is responsible, and to allow users to manipulate the .MVC can handle many different views of the application.)
    3. Controller The controller (reception request -> call model -> model and the process returns through the corresponding distribution data based on a result page)
  • MVC的优点

    1. 分工明确(开发人员可以只关注整个结构中的其中某一层):使用MVC可以把数据库开发,程序业务逻辑开发,页面开发分开,每一层都具有相同的特征,方便以后的代码维护。它使程序员(Java开发人员)集中精力于业务逻辑,界面程序员(HTML和JSP开发人员)集中精力于表现形式上。
    2. 松耦合(可以降低层与层之间的依赖):视图层和业务层分离,这样就允许更改视图层代码而不用重新编译模型和控制器代码,同样,一个应用的业务流程或者业务规则的改变只需要改动MVC的模型层即可。因为模型与控制器和视图相分离,所以很容易改变应用程序的数据层和业务规则。
    3. 复用性高(利于各层逻辑的复用):像多个视图能够共享一个模型,不论你视图层是用flash界面或是wap界面,用一个模型就能处理他们。将数据和业务规则从表示层分开,就可以最大化从用代码。
    4. 有利于标准化(有利于通过工程化、工具化产生管理程序代码)

4.Spring的两大核心技术是什么?

  • IOC(DI):Inverse of Controller,控制反转。

​ Dependency Inject ,依赖注入。

  • AOP(面向切面编程)

面向切面编程,简单地说就是在不改变原程序的基础上为代码段增加新功能,对代码段进行增强处理

5.什么是IOC/DI?

5.1、IOC(DI)

  • Inverse of Controller:控制反转,把创建对象的操作交给框架,所以又被称为依赖注入

    • 最基础的调用的对象是通过new一个对象出来,例如:People p=new People
    • 我们Spring框架中的IOC即改变这种方式的调用,将后面“new People”转换为xml文件去调用,即使用第三者调用
    • 举例说明
      • People p=new People 相当于两个人之前的直接调用,假如张三和李四,张三需要用到只有李四电脑里面的一份文件,张三就必须直接向李四要,假如李四的电脑坏了,那么张三也就拿不到这份文件,然而IOC就相当于张三叫王五去拿这份文件,李四将文件拷贝给了王五,在这过程中即使李四的电脑坏了,那么张三也能从王五那拿到文件,张三不会受到影响。这就是控制反转的最大好处。即用来降低程序代码之间的耦合度
  • Dependency Inject ,依赖注入。

    • setter方式注入
      创建学生类创建学生类,给予学生类一个学号,一个姓名的属性(此两个属性必须生成set方法,才能完成setter方式的注入)

      public class Student {
      
          //学号
          private int num;
          //姓名
          private String name;
      
          public void setNum(int num) {
              this.num = num;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      }

      在applicationContext.xml文件中赋值

      <!-- setter方法注入 -->
      <bean id="stu" class="Student" >
              <property name="num" value="1"></property>
              <property name="name" value="李四"></property>
      
      </bean>
    • 构造注入
      给学生类写个带参的构造函数(写完带参的构造函数后,必须再定义一个无参的构造函数)

      //构造器注入
      public class Student {
      
          public Student(){
      
      
          }
          public Student(int num,String name){
      
          }
          //学号
          private int num;
          //姓名
          private String name;
      }

      在applicationContext.xml文件中赋值

      <!-- 构造器注入 -->
          <bean id="st" class="Student">
              <constructor-arg>
                  <value type="java.lang.int">101</value>
              </constructor-arg>
              <constructor-arg> 
                  <value type="java.lang.String">张三</value>
              </constructor-arg>
          </bean>
    • 使用P命名空间注入
      在Student最基础类的基础上,给ApplicationContext.xml文件中导入一个头的包

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      
      
          xmlns:p="http://www.springframework.org/schema/p"
      
      </beans>

      之后再xml用bean标签赋值

      <bean id="stu" class="test.Student" p:name="张三" p:num="1001">
      </bean>

6.谈谈面向对象编程与面向接口编程的区别。

面向接口编程:

面向对象编程中不同的对象协作完成系统的各个功能,对象内部的实现由各个类自己实现。但是随着系统愈发杂,对象内部功能的实现已经不那么重要了(类自己内部实现),反之对象间的协作能力更为重要,这就是面向接口编程的思想
面向接口编程:

就是先将业务的逻辑线先提取出来,作为接口,而将具体的代码实现作为实现类来完成。当客户需变化时,只用更改接口下的实现类就可以
面向接口编程优点:

  • 降低耦合性,能极大限度解耦
  • 易于程序扩展
  • 易于程序维护
  • 抽象类和接口的选择
    在于使用动机,使用抽象类是为了代码的复用,而使用接口的动机是为了实现多态性与协作关系
    面向接口编程

  • 经典的面向接口编程例子-JDBC
    SUN公司提供了JDBC接口的规范,而不同的数据库厂商开发不同的数据库驱动
    可以理解为只有实现者和调用者都遵循"面向接口编程"这个准则,制定者的协调目的才能达到

7.什么是反射?说说反射是如何应用到spring框架上。

8.说说java 解析xml有哪些技术?

  • Dom解析
    是html和xml的应用程序接口(API),DOM文档对象模型,采用树形结构来完成对文档的解析,在解析时,会将整个文档加载到内存中然后形成"节点数",当文档内容过多或者需要解析的文档过多时,会造成服务器内存紧张
  • SAX解析
    流模型中的"推'模型分析方式。通过事件驱动,每发现-个节点就引发一个事件 ,事件推给事件处理器,通过回调方法完成解析工作,解析XML文档的逻辑需要应用程序完成
  • JDOM解析
    Java特定的文档对象模型。自身不包含解析器,使用SAX
  • DOM4J解析
    简单易用,采用Java集合框架,并完全支持DOM、SAX和JAXP, 提供了一完整的针对DOM和SAX的解析技术, 目前使用较为广泛
  • STAX解析
    流模型中的拉模型分析方式。提供基于指针和基于迭代器两种方式的支持,JDK1.6新特性

9.抽象类与接口的区别。

10.谈谈表单的同步提交与异步提交的区别

同步:提交请求->等待服务器处理->处理完毕返回,在此期间浏览器不能做别的事情
异步:请求通过事件触发->服务器处理(服务器任然可以做别的事情)->处理完毕
区别:
同步提交的时候客户端浏览器不能做别的事情,只能等待服务器处理;而异步提交的时候客户端浏览器任然可以做别的
可以理解为吃饭,同步就是我叫你去吃饭,你没听见,我继续叫你,知道你回答我了,一起去吃饭。
异步就是我给你发消息说我去吃饭了,然后我就先走了,你后面自己来

Guess you like

Origin www.cnblogs.com/godles/p/12334115.html