大数据正式28

来源:互联网 发布:日语聊天软件 编辑:程序博客网 时间:2024/06/07 14:02

大数据正式28

回顾上一节

  • IOC:控制反转(对象的创建)
  • 导入约束文件
  • 获取对象
    1. id
    2. class
  • 创建对象(3+1)
    • 构造函数
    • 静态工厂
    • 动态工厂
    • 实现FactoryBean接口【getObject、getObjectType、isSingleton】
  • Scope--控制单例多例(多例:prototype)
  • Lazy-init(默认为false--即单例非懒加载)
  • 对象的生命周期
    • init-method
    • destory-method
    • 过程
      1. 构造方法
      2. init
      3. 其他方法
      4. destory
  • DI依赖注入
    • set方法
    • Constructor方法
  • alias别名

Spring的简化配置

  • parent
    1. bean的属性------类间继承的时候(extends)
    2. 可选参数:继承的父类的bean的id属性值
  • abstract
    1. 设置为是否是抽象
    2. 作用:想不想让它创建
    3. 可选参数:true/false
      1. true不会实例化此对象
      2. false会实例化此对象
  • autowire【配置文件的手动注入用此方法来替换】
    1. 自动注入的规则(参数)
      1. byName
        • 原理
          1. 方法setName自动变形,将set去掉,再将首字母小写
          2. 得到name作为id,并去容器找相应的对象
          3. 根据id注入相应的数据,有则注入,没有则注null
      2. byType
        • 原理
          1. 通过属性的类型去spring容器中去找
          2. 找到一个注入;多个报错;没有为null
    2. 注意:bean的id-------属性的名称

spring的注解自动注入模式

  • 头配置

    <?xml version="1.0" encoding="UTF-8"?><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-3.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"></beans>

    <?xml version="1.0" encoding="UTF-8"?><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-3.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">    <context:annotation-config></context:annotation-config></beans>
  • 规则【@Autowired】【不用setter方法了~~】
    • 把属性名称作为id去spring容器中找(id---->属性),id找不到用type(类型)找

      @Autowiredprivate Dog dog;
  • 规则【@Resource】【不用setter方法了~~】
    • 也可以起到自动注入的作用
    • 可以指定名称(id)

      @Resource(name="dog1")private Dog dog;
    • 等效

      @Autowired@Qualifier(value="")
      比较项@Autowired@Resource相同自动注入自动注入name属性无有聚合工程(一个大工程中有很多小工程)可以不太适合

给类加注解

  • 流程
    1. 扫描包

      <!-- 开启包扫描 --><context:component-scan base-package="com.peng"></context:component-scan>
    2. 开启注解开关

      <!-- 开启注解开关 --><context:annotation-config></context:annotation-config>
    3. @Component注解
      • 相当于bean
      • id<=>类名首字母小写
      • 需要注解的@Autowired
  • 整体配置文件

    <?xml version="1.0" encoding="UTF-8"?><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-3.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">    <!-- 开启包扫描 -->    <context:component-scan base-package="com.peng"></context:component-scan>    <!-- 开启注解开关 -->    <context:annotation-config></context:annotation-config></beans>
  • 工作原理

    1. 创建容器对象,加载配置文件
    2. 要是发现扫描包,得到包下的类, 检测类上是否有注解,有则生成类对应的对象【如果注入类型为接口类型,会去找实现类(唯一实现类)--不唯一则报错】
    3. 类中有需要注入的则进行相应的注解
  • 类的其他注解
    • @Servive-----service层
    • @Repository---dao层
    • @Controller---servlet(web层)
    • @Component----万能注解【可作为任意层】

注解形式的mvc

  • 目录结构
  • 具体代码(注意:applicationContext.xml的约束文件可能版本不一样---注意修正环境)

    • dao

      • PersonDao

        package com.peng.dao;import com.peng.pojo.Person;public interface PersonDao {    void savePerson(Person p);}
      • PersonDaoImpl

        package com.peng.dao;import org.springframework.stereotype.Repository;import com.peng.pojo.Person;@Repository(value = "personDao")public class PersonDaoImpl implements PersonDao {    @Override    public void savePerson(Person p) {        System.out.println("保存了:" + p);    }}
    • pojo

      • Person

        package com.peng.pojo;import org.springframework.stereotype.Component;@Component(value = "person")public class Person {    private String name;    private Integer age;    private Person() {        super();    }    private Person(String name, Integer age) {        super();        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }}
    • service

      • PersonService

        package com.peng.service;import com.peng.pojo.Person;public interface PersonService {    void savePerson(Person p);}
      • PersonServiceImpl

        package com.peng.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Service;import com.peng.dao.PersonDao;import com.peng.pojo.Person;@Service(value = "serviceImpl")public class PersonServiceImpl implements PersonService {    @Autowired    @Qualifier(value = "personDao")    private PersonDao dao;    @Override    public void savePerson(Person p) {        if (null != dao) {            dao.savePerson(p);        }    }}
    • web

      • PersonServlet

        package com.peng.web;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Controller;import com.peng.pojo.Person;import com.peng.service.PersonService;@Controller(value="personServlet")public class PersonServlet {    @Autowired    @Qualifier(value = "serviceImpl")    private PersonService service;    public void doSomething(Person p) {        service.savePerson(p);    }}
    • test

      • Test

        package com.peng.web;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Controller;import com.peng.pojo.Person;import com.peng.service.PersonService;@Controller(value="personServlet")public class PersonServlet {    @Autowired    @Qualifier(value = "serviceImpl")    private PersonService service;    public void doSomething(Person p) {        service.savePerson(p);    }}
    • 配置文件

      • applicationContext.xml

        <?xml version="1.0" encoding="UTF-8"?><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-3.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">    <!-- 开启包扫描 -->    <context:component-scan base-package="com.peng"></context:component-scan>    <!-- 开启注解开关 -->    <context:annotation-config></context:annotation-config>    <!-- pojo对象 -->    <!-- dao层 -->    <!-- service层 -->    <!-- web层 --></beans>

简单心得

  • @Component@Repository@Service@Controller注解写在:能够创建对象的类的地方【接口这里暂时没写过】
  • @Autowired@Qualifier(value=“对应类的类名首字母小写/注解的value的内容”)注入对象:在需要对象的地方的zhuru
  • 推荐的写法【仅个人学到这的感觉】
    1. 类注解中----添加value属性
    2. 注入中------使用@Autowired和@Qualifier(value=“”)的组合方式
    3. 例子

      @Controller(value="personServlet") //这里的value属性public class PersonServlet {    @Autowired    @Qualifier(value = "serviceImpl")  //组合方式    private PersonService service;    }}

      <?xml version="1.0" encoding="UTF-8"?><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-3.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">    <!-- 开启包扫描 -->    <context:component-scan base-package="com.peng"></context:component-scan>    <!-- 开启注解开关 -->    <context:annotation-config></context:annotation-config></beans>

BeanNameAware接口

  • 名字转换规则
    • 取决于第二个字母
      • 第二个字母大写:首字母不变
      • 第二个字母小写:首字母小写

其他注解

  • 类的注解
    • @Scope(value="singleton/prototype")
    • @Lazy(value=false/true)------------- 懒加载-------注意这里的 value属性不加双引号
  • 方法的注解
    • @PostConstrict------初始化方法
    • @PreDestory-------销毁方法
  • 属性

    • 基本类型
      • @Value(value="具体的值")
    • 复杂类型【在配置文件中加】

      • 形式
        1. @Value(value="#{@id}")
        2. @Value(value="${prop_key}")
        3. @Value(value="")
      • 例子(list,map,set,propties)

        • 配置文件

        • POJO

        • 结果

之前的注解都写上的执行效果

  • 效果
    • 单例【执行init和destory】

      十二月 19, 2017 2:39:00 下午 org.springframework.context.support.AbstractApplicationContext prepareRefreshINFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ba778: startup date [Tue Dec 19 14:39:00 CST 2017]; root of context hierarchy十二月 19, 2017 2:39:01 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [applicationContext.xml]十二月 19, 2017 2:39:02 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletonsINFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1424146: defining beans [personDao,person,serviceImpl,personServlet,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy=================懒加载在此之后=============================构造函数================Person=====init==========保存了~~名称:张三,年龄:12十二月 19, 2017 2:39:02 下午 org.springframework.context.support.AbstractApplicationContext doCloseINFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4ba778: startup date [Tue Dec 19 14:39:00 CST 2017]; root of context hierarchy十二月 19, 2017 2:39:02 下午 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletonsINFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1424146: defining beans [personDao,person,serviceImpl,personServlet,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy=====Person=====destory==========
    • 多例【执行init,不执行destory】

      十二月 19, 2017 2:49:46 下午 org.springframework.context.support.AbstractApplicationContext prepareRefreshINFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ba778: startup date [Tue Dec 19 14:49:46 CST 2017]; root of context hierarchy十二月 19, 2017 2:49:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [applicationContext.xml]十二月 19, 2017 2:49:46 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletonsINFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1424146: defining beans [personDao,person,serviceImpl,personServlet,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy=================懒加载在此之后=============================构造函数================Person=====init==========保存了~~名称:张三,年龄:12十二月 19, 2017 2:49:46 下午 org.springframework.context.support.AbstractApplicationContext doCloseINFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4ba778: startup date [Tue Dec 19 14:49:46 CST 2017]; root of context hierarchy十二月 19, 2017 2:49:46 下午 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletonsINFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1424146: defining beans [personDao,person,serviceImpl,personServlet,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy

小结

  • IOC和DI
    • 配置文件
      • bean
        • 属性:
          • id【标识】
          • class【全类名】
          • factory-bean【工厂id】
          • factory-method【获取实例方法】
          • scope【是否单例】
          • lazy-init【懒加载】
          • init-method【初始化方法】
          • destory-Method【销毁方法】
          • autowire【byName,byType】
        • 子标签:
          • property【name,value,ref】
          • construction-arg【name,value,ref,index】
      • alias
    • 注解
      • 类的注解
        • 创建对象
          • @Component
          • @Repository
          • @Service
          • @Controller
        • 单例
          • @Scope
        • 懒加载
          • @Lazy
      • 方法的注解
        • @PostConstruct
        • @PreDestory
      • 属性
        • @Autowried+@Qualifier
        • @Value

补充