spring第二天

    一、基于注解的IOC配置

        1.1写在最前

            学习基于注解的IOC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能是一样的,都是降低程序间的耦合。只是配置的形式不一样。

            关于实际的开发中到底使用xml还是注解,每家公司有着不同的使用习惯。所以这两种配置方式我们都需要掌握。

1.2环境搭建

            1.2.1第一步:拷贝必备jar包到工厂的lib目录。

            1.2.2第二步:在类的跟路径下创建一个任意名称的xml文件(不能是中文)

                基于注解整合时,导入约束时需要多导入一个context名称空间下的约束。

                给配置文件导入约束:

                    <?xml version="1.0" encoding="UTF-8"?>

                    <!-- 导入schema 

                    约束的位置在:

                        ..\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html

                        文件中。

                    注意:要导入schema约束

                    -->

                    <beans xmlns="http://www.springframework.org/schema/beans"

                          xmlns:context="http://www.springframework.org/schema/context"

                           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

                                      http://www.springframework.org/schema/context/spring-context.xsd ">

                    </beans>

1.2.3第三步:使用@Component注解配置管理的资源

                /**

                *    客户的业务层实现类

                */

                @Component(value="customerService")

                public class CustomerServiceImpl implements ICustomerService{

                    @Override

                    public void saveCustomer(){

                        System.out.println("执行了保存客户");

                    }

                }

1.2.4第四步在spring的配置文件中开启spring对注解ioc的支持

                <!-- 告知spring框架在,在读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器中 -->

                <context:compoent-scan base-package="com.changemax"></context:compoent-scan>

1.3常用注解

            1.3.1用于创建对象的

                相当于:<bena id="" class="">

1.3.1.1@Component

                    作用:

                        把资源让spring来管理。相当于在xml中配置了一个bean

                    属性:

                        value:指定bean的id。如果不指定value属性,默认bean的id是当前类的类名。首字母小写。

1.3.1.2@Controller @Service @Repository

                    他们三个注解都是针对了一个的衍生注解,它们的作用及属性都是一模一样的。

                    它们只不过是提供了更加明确的语义化。

                        @Controller:一般用于表现层的注解。

                        @Service:一般用于业务层的注解。

                        @Repository:一般用于持久层的注解。

细节:如果注解中有且只有一个属性要赋值是时,且名称是value,value在赋值是可以不写的。

1.3.2用于注入数据的:

                相当于:<property name="" ref="">

                        <property name="" value="">

1.3.2.1@Autowired

                    作用:

                        自动按照类型注入。当使用注解注入属性时,set方法可以省略。它只能注入其他的bean类型。当有多个类型匹配时,使用要注入的对象变量名作为bean的id,在spring容器查找,找到了也可以注入成功。找不到就报错。

1.3.2.2@Qualifier

                    作用:

                        在自动按照类型注入的基础之上,再按照Bean的id注入。它在给字段注入时不能独立使用,必须和@Autowire一起使用;但是给方法参数注入时,可以独立使用。

                    属性:    

                        value:指定bean的id

1.3.2.3@Resource

                    作用:

                        直接按照Bean的id注入。它也只能注入其他bean类型

                    属性:

                        name:指定bean的id

1.3.2.4@Value

                    作用:

                        注入基本数据类型和String类型数据的。

                    属性:

                        value:用于指定值。

1.3.3用于改变作用范围的:

                相当于:<bean id="" class="" scope="">

1.3.3.1@Scope

                    作用:

                        指定bean的作用范围

                    属性:

                        value:指定范围的值。

                                取值:singleton prototype request session globalsession

1.3.4和生命周期相关的:(了解)

                相当于:<bean id="" class="" init-methdo="" destroy-method=""/>

1.3.4.1@PosConstruct

                    作用:

                        用于指定初始化方法

1.3.4.2@PreDestroy

                    作用:

                        用于指定销毁方法。

1.3.5代码示例:

                业务层代码:

                    /**

                    *客户的业务层接口

                    */

                    public interface ICustomerService{

                        //保存客户

                        void saveCustomer();

                    }

/**

                     * 客户的业务层实现类

                     */

                    //作用就相当于在xml中配置了一个bean标签,该注解有value属性,含义是bean的id。

                    //不写的时候,默认的id是:当前类名,且首字母小写。即:customerServiceImpl

                    @Component(value="customerService")

                    @Scope(value="singleton")

                    public class CustomerServiceImpl implements ICustomerService {

                    //    @Autowired

                    //    自动按照数据类型注入,拿着当前变量的数据类型在spring的容器中找,找到后,给变量赋值。

                    //    当有多个类型匹配时,会使用当前变量名称customerDao作为bean的id,继续在容器中找。

                    //    找到了,也能注入成功。找不到就报错。

                    //    @Qualifier(value="customerDao2")//在自动按照类型注入的基础之上,再按照id注入

                        @Resource(name="customerDao2")//直接按照bean的id注入

                        private ICustomerDao customerDao = null;

                        

                        @Value("com.mysql.jdbc.Driver")//注入基本类型和String类型数据

                        private String driver;

@Override

                        public void saveCustomer() {

                            System.out.println(driver);

                            customerDao.saveCustomer();    

                        }

                    }

持久层代码:

                    /**

                     * 客户的持久层接口

                     */

                    public interface ICustomerDao {

                        /**

                         * 保存客户

                         */

                        void saveCustomer();

                    }

/**

                     * 客户的持久层实现类11111111111111111111

                     */

                    @Repository("customerDao1")

                    public class CustomerDaoImpl implements ICustomerDao {

@Override

                        public void saveCustomer() {

                            System.out.println("保存了客户111111111111111111");

                        }

                    }

/**

                     * 客户的持久层实现类222222222222222222222222

                     */

                    @Repository("customerDao2")

                    public class CustomerDaoImpl2 implements ICustomerDao {

@Override

                        public void saveCustomer() {

                            System.out.println("保存了客户2222222222222222222");

                        }

                    }

测试类代码:

                    public class Client {

                        public static void main(String[] args) {

                            //1.获取容器

                            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

                            //2.根据id获取对象

                            ICustomerService cs = (ICustomerService) ac.getBean("customerService");                cs.saveCustomer();

                        }

                    }

配置文件:

                    <?xml version="1.0" encoding="UTF-8"?>

                    <!-- 我们导入约束时,除了昨天的那部分之外,还要单独导入一个context名称空间 -->

                    <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 

                                     http://www.springframework.org/schema/context/spring-context.xsd">

                        <!-- 告知spring框架在通过读取配置文件创建容器时,扫描的包,并根据包中类的注解创建对象-->

                        <context:component-scan base-package="com.itheima"></context:component-scan>

                    </beans>

1.3.6关于Spring注解和XML的选择问题

                注解的优势:

                    配置简单,维护方便(我们找到类,就相当于找到了对应的配置)

                XML的优势:

                    修改时,不用改源码。不涉及重新编译和部署。

Spring管理Bean方式的比较:

                    比较内容            基于XML配置                基于注解配置

                    Bean定义            <bean id="" class=""/>     @Component 衍生类@Repository、@Service、@Controller

Bean名称            通过id或name指定            @Component("person")

Bean注入            <property>或者通过p命名空间 @Autowired 按类型注入、@Qualifier 按名称注入

生命过程、Bean作用范围 init-method、destroy-method 范围 scope属性    @PostConstruct初始化、@PreDestroy销毁、@Scope设置作用范围

适合场景            Bean来自第三方,使用其他    Bean的实现类由用户自己开发

1.4Spring管理对象细节

            基于注解的spring IOC配置中,bean对象的特点和基于XML配置是一模一样的。

            写到此处,基于注解的IOC配置已经完成,但是大家都发现了一个问题:我们依然离不开spring的xml配置文件,那么能不能不写这个bean.xml,所有配置都用注解来实现呢?

                可以。

1.5spring的纯注解配置

            1.5.1待改造的问题

                我们发现,之所以我们现在离不开xml配置文件,是因为我们有一句很关键的配置:

                    <!-- 告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器中 -->

                    <context:component-scan base-package="com.changemax"></context:component-scan>

                如果他能通过注解配置,那么就可以脱离xml而配置文件了

1.5.2使用注解配置要扫描的包

                创建一个新类:SpringConfiguration.java

                    /**

                     * 客户的业务层实现类

                     */

                    @Configuration//表明当前类是一个配置类

                    @ComponentScan(basePackages = "com.itheima")//配置要扫描的包

                    public class SpringConfiguration {

                    }

那么新的问题又来了,我们如何获取容器呢?

                    public class Client {

                        public static void main(String[] args) {

                            //1.获取容器:由于我们已经没有了xml文件,所以再用读取xml方式就不能用了。

                            //这时需要指定加载哪个类上的注解

                            ApplicationContext ac = 

                                new AnnotationConfigApplicationContext(SpringConfiguration.class);

                            //2.根据id获取对象

                            ICustomerService cs = (ICustomerService) ac.getBean("customerService");

                            cs.saveCustomer();

                        }

                    }

            1.5.3新注解说明

                1.5.3.1@Configuration

                    作用:

                        用于指定当前类是一个spring配置类,当创建容器时会从该类上加载注解。获取容器时需要使用AnnotationApplicationContext(有@Configuration注解的类.class)。

                    属性:

                        value:用于指定配置类的字节码

示例代码:

                        /**

                         * 用于初始化spring容器的配置类

                         */

                         @Configuration

                         public class SpringConfiguration{

}

                1.5.3.2@ConponentScan

                    作用:

                        用于指定spring在初始化容器时要扫描的包。作用和在spring的xml配置文件中的:

                            <context:component-scan base-package="com.changemax"/>是一样的。

                    属性:

                        basePackages:用于指定要扫描的包。和该注解中的value属性作用一样。

1.5.3.3@PropertySource

                    作用:

                        用于加载.properties文件中的配置。例如我们配置数据源时,可以把连接数据库的信息写到properties配置文件中,就可以使用此注解指定properties配置文件的位置。

                    属性:

                        value[]:用于指定properties文件位置。如果是在类路径下,需要写上classpath:

示例代码:

                        配置:

                            public class JdbcConfig{

                                @Value("${jdbc.driver}")

                                private String driver;

@Value("${jdbc.url}")

                                private String url;

@Value("${jdbc.username}")

                                private String username;

@Value("${jdbc.password}")

                                private String password;

@Bean(name="dataSource")

                                public DataSource getDataSorce(){

                                    BasicDataSource ds = new BasicDataSource();

                                    ds.setDriverClassName(driver);

                                    ds.setUrl(url);

                                    ds.setUsername(username);

                                    ds.setPassword(password);

                                    return ds;

                                }    

                            }

jdbc.properties文件:

                                jdbc.driver=com.mysql.jdbc.Driver

                                jdbc.url=jdbc:mysql://localhost:3306/sql_test

                                jdbc.username=root

                                jdbc.password=123456

注意:

                                在spring4.3以前都需要提供一个占位符配置器:

                                    PropertySourcesPlaceholderConfigurer

                                而在spring4.3以后,则不需提供。

                                提供的方式如下:(在SpringConfiguration或JdbcConfig中配置均可)

@Bean

                                public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){

                                    return new PropertySourcesPlaceholderConfigurer();

                                }

1.5.3.4@Import

                    作用;

                        用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration注解。当然,写上也没有问题。

                    属性:

                        value[]:用于指定其他配置类的字节码。

示例代码:

                        @Configuration

                        @ComponentScan(basePackages="com.changemax.spring")

                        public class Configuration A{

}

@Configuration

                        @PropertySource("classpath:info.properties")

                        public class Configuration B{

}

1.5.3.5@Bean

                    作用:

                        该注解只能写在方法上,表面使用此方法创建一个对象,并且放入spring容器。它就相当于我们在xml配置中介绍的factory-bean和factory-method.

                    属性:

                        name:给当前@Bean注解方法创建的对象指定一个名称(即bean的id)。

示例代码:

                        public DataSource createDataSource() thorws Exception{

                            ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();

                            comboPooledDataSource.setUser("root");

                            comboPooledDataSource.setPassword("1234");

                            comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");

                            comboPooledDataSource.setJdbcUrl("jdbc:mysql:///spring_ioc");

                            return comboPooledDataSource;

}

二、Spring整合Junit

        2.1准备测试环境

            2.1.1创建业务层接口实现类

                /**

                 * 客户的业务层接口

                 */

                public interface ICustomerService {

/**

                     * 查询所有客户

                     * @return

                     */

                    List<Customer> findAllCustomer();

                    

                    /**

                     * 保存客户

                     * @param customer

                     */

                    void saveCustomer(Customer customer);

                }

/**

                 * 客户的业务层实现类

                 */

                public class CustomerServiceImpl implements ICustomerService {

private ICustomerDao customerDao;

                    

                    public void setCustomerDao(ICustomerDao customerDao) {

                        this.customerDao = customerDao;

                    }

@Override

                    public List<Customer> findAllCustomer() {

                        return customerDao.findAllCustomer();

                    }

@Override

                    public void saveCustomer(Customer customer) {

                        customerDao.save(customer);

                    }

}

2.1.2创建持久层接口实现类

                /**

                 * 客户的持久层接口

                 */

                public interface ICustomerDao {

                    

                    /**

                     * 查询所有客户

                     * @return

                     */

                    List<Customer> findAllCustomer();

/**

                     * 保存客户

                     * @param customer

                     */

                    void save(Customer customer);

                }

/**

                 * 客户的持久层实现类

                 */

                public class CustomerDaoImpl implements ICustomerDao {

@Override

                    public List<Customer> findAllCustomer() {

                        System.out.println("查询了所有客户");

                        return null;

                    }

@Override

                    public void save(Customer customer) {

                        System.out.println("保存了客户");

                    }

                }

2.1.3导入junit的jar包

2.1.3编写测试类

                /**

                 * 测试客户的业务层和持久层

                 */

                public class CustomerServiceTest {

private ICustomerService customerService;

                    

                    @Test

                    public void testFindAll(){

                        customerService.findAllCustomer();

                    }

                    

                    @Test

                    public void testSave(){

                        Customer c = new Customer();

                        c.setCustName("传智学院 ");    

                        customerService.saveCustomer(c);

                    }

                }

2.2使用xml配置步骤

            2.2.1xml文件中的配置

                <?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">

                    <!-- 把资源交给spring来管理 -->

                    <bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl"></bean>

                    

                    <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">

                        <property name="customerDao" ref="customerDao"></property>

                    </bean>

                </beans>

2.2.2第一步:拷贝整合junit的必备jar包到lib目录

                此处需要注意:导入jar包时,需要导入一个spring中aop的jar包。

2.2.3第二步:使用@RunWith注解替换原有运行器

                @RunWith(SpringJUnit4ClassRunner.class)

                public class CustomerServiceTest {

                    

                    private ICustomerService customerService;

                    

                    @Test

                    public void testFindAll(){

                        customerService.findAllCustomer();

                    }

                    

                    @Test

                    public void testSave(){

                        Customer c = new Customer();

                        c.setCustName("传智学院 ");    

                        customerService.saveCustomer(c);

                    }

                }

2.2.4第三步:使用@ContextConfiguration指定spring配置文件的位置

                @RunWith(SpringJUnit4ClassRunner.class)

                @ContextConfiguration(locations={"classpath:bean.xml"})

                public class CustomerServiceTest {

private ICustomerService customerService;

                    

                    @Test

                    public void testFindAll(){

                        customerService.findAllCustomer();

                    }

                    

                    @Test

                    public void testSave(){

                        Customer c = new Customer();

                        c.setCustName("传智学院 ");    

                        customerService.saveCustomer(c);

                    }

                }

2.2.5第四步:使用@Antowired给测试类中的变量注入数据

                @RunWith(SpringJUnit4ClassRunner.class)

                @ContextConfiguration(locations={"classpath:bean.xml"})

                public class CustomerServiceTest {

@Autowired

                    private ICustomerService customerService;

                    

                    @Test

                    public void testFindAll(){

                        customerService.findAllCustomer();

                    }

                    

                    @Test

                    public void testSave(){

                        Customer c = new Customer();

                        c.setCustName("传智学院 ");    

                        customerService.saveCustomer(c);

                    }

                }

2.3使用纯注解配置步骤

            2.3.1第一步:拷贝整合junit的必备jar包到lib目录

                此处需要注意的是,导入jar包时,需要导入一个spring中aop的jar包。

2.3.2第二步;把资源都用注解管理

                @Service("customerService")

                public class CustomerServiceImpl implements ICustomerService {

@Autowired

                    private ICustomerDao customerDao;

@Override

                    public List<Customer> findAllCustomer() {

                        return customerDao.findAllCustomer();

                    }

@Override

                    public void saveCustomer(Customer customer) {

                        customerDao.save(customer);

                    }

                }

/**

                 * 客户的持久层实现类

                 */

                @Repository("customerDao")

                public class CustomerDaoImpl implements ICustomerDao {

@Override

                    public List<Customer> findAllCustomer() {

                        System.out.println("查询了所有客户");

                        return null;

                    }

@Override

                    public void save(Customer customer) {

                        System.out.println("保存了客户");

                    }

                }

2.3.3第三步:

                使用注解配置方式创建spring容器

                    @Configuration

                    @ComponentScan(basePackages={"com.itheima"})

                    public class CustomerServiceTest {

@Autowired

                        private ICustomerService customerService;

                        

                        @Test

                        public void testFindAll(){

                            customerService.findAllCustomer();

                        }

                        

                        @Test

                        public void testSave(){

                            Customer c = new Customer();

                            c.setCustName("传智学院 ");    

                            customerService.saveCustomer(c);

                        }

                    }

            2.3.4第四步:使用RunWith注解和ContextConfiguration注解配置

                @RunWith(SpringJUnit4ClassRunner.class)

                @ContextConfiguration(classes={CustomerServiceTest.class})

                @Configuration

                @ComponentScan(basePackages={"com.itheima"})

                public class CustomerServiceTest {

@Autowired

                    private ICustomerService customerService;

                    

                    @Test

                    public void testFindAll(){

                        customerService.findAllCustomer();

                    }

                    

                    @Test

                    public void testSave(){

                        Customer c = new Customer();

                        c.setCustName("传智学院 ");    

                        customerService.saveCustomer(c);

                    }

                }

2.4为什么不把测试类配到xml中

            原因一:当我们在xml中配置了一个bean,spring加载配置文件创建容器时,就会创建对象。

            原因二:测试类只是我们在测试功能时使用,而在项目中它并不参与程序逻辑,也不会解决需求上的问题,所以创建完了,并没哟使用,那么存在容器中就会造成资源的浪费。

            所以基于以上两点,不应该把测试配置到xml1文件中。

开放源代码的设计层面框架Spring——day02的更多相关文章

  1. 开放源代码的设计层面框架Spring——day01

    spring第一天     一.Spring概述         1.1spring概述             1.1.1spring介绍                 Spring是分层的Jav ...

  2. 开放源代码的设计层面框架Spring——day04

    spring第四天     一.Spirng中的JdbcTemplate         1.1JbdcTemplate概述             他是spring框架中提供的一个对象,是对原始Jd ...

  3. 开放源代码的设计层面框架Spring——day03

    spring第三天     一.AOP的相关概念         1.1AOP概述             1.1.1什么是AOP                 AOP:全称是Aspext Orie ...

  4. Hibernate(开放源代码的对象关系映射框架)

    Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自 ...

  5. JavaEE之Hibernate(开放源代码的对象关系映射框架)

    Hibernate(开放源代码的对象关系映射框架) 1.简介 Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全 ...

  6. [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World

    来源:http://blog.csdn.net/zhshulin/article/details/37956105?utm_source=tuicool&utm_medium=referral ...

  7. 开源倾情奉献:基于.NET打造IP智能网络视频监控系统(一)开放源代码

    本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. 开源倾情奉献系列链接 开源倾情奉献:基于.NET打造IP智能网络视频监控系统(一)开放源代码 开源倾 ...

  8. Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(转发)

    [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 来源:http://blog.csdn.net/zhshulin/article/de ...

  9. 第二个基础框架 — spring — xml版,没用注解 — 更新完毕

    1.什么是spring? 老规矩:百度百科一手 这上面说得太多了,我来提炼一下: spring就是一个轻量级的控制反转( IOC ) 和 面向切面编程( AOP ) 的容量框架.总的来说:本质就是对j ...

随机推荐

  1. 未能加载文件或程序集“System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项

    楼主创建项目的时候选择的是5.2.4的版本,但是后来改成了5.0.0于是出现了这个错误,解决的方法倒也简单 将View文件夹下 web.config文件中 以下两处 版本改成当前版本就行了

  2. jquery 同步加载

    jquery在前端展示时,如果需要从服务器获取信息然后在更新,需要设置同步加载. async属性设置为:false即可. $.ajax({ url : 'test.php', type : 'post ...

  3. vue和angular的区别:

    相同: 1.数据绑定:vue和angular绑定都可以用{{}} 2.都支持内置指令和自定义指令 3.都支持内置过滤器和自定义过滤器. 区别: 1.学习成本和API 设计:vue相比于angular来 ...

  4. mysqlbinlog 工具分析binlog日志

    MySQL的binlog 日志对于生产环境非常有用,任何时间对数据库的修改都会记录在binglog中:当数据发生增删改,创建数据库对象都会记录到binlog中,数据库的复制也是基于binlog进行同步 ...

  5. IDEA Can't Update No tracked branch configured for branch master or the branch doesn't exist.

    IDEA Can't Update No tracked branch configured for branch master or the branch doesn't exist.To make ...

  6. 【css】一行或者多行文字垂直水平居中

    1.方法一:使用css3弹性盒子(兼容IE10及以上浏览器,firefox,chrome,safari 5.1.7不支持) <!DOCTYPE html> <html> < ...

  7. 【Java】+SOFA

    https://www.jianshu.com/p/e3dca8d5e9ee sofa脑图

  8. DOM中获取宽高、位置总结

    原生JS 一.文档.窗口的宽高和位置 // 获取屏幕的宽高 window.screen.height | window.screen.width // 屏幕可用工作区宽高 window.screen. ...

  9. 【动态规划】 EditDistance

    思路参考: https://www.cnblogs.com/littlepanpc/p/7895810.html 代码参考:https://leetcode.com/problems/edit-dis ...

  10. 第二部分之Redis服务器(第十四章)

    Redis服务器复制和多个客户端建立网络连接,处理客户端发送的命令请求,在数据库中保存客户端执行命令所产生的数据. 一,命令请求的执行过程 客户端向服务器发送命令请求 set key value 服务 ...