spring

作者: coderpjw | 来源:发表于2018-11-21 22:10 被阅读0次

    spring的maven依赖包

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.0.8.RELEASE</version>
            </dependency>
    

    XML基本的配置步骤

    1. 创建JavaBean
    2. 在xml中配置bean
      <bean id="myA" class="package.MyA"></bean>
    
      <bean id="myBean" class="package.MyBean">
        <!-- 注入其他类 2种方式 选择其中一种即可-->
        <!-- 通过set方法注入  -->
        <prototyper name="mya" ref="myA">
        <!-- 通过构造器注入 -->
        <constructor-arg index="0" ref="myA"></constructor-arg>
    
        <!-- 基本类型 -->
        <prototyper name="id" value="1"/>
        <prototyper name="name" value="张三"/>
        <!-- 集合类型 -->
        <!-- list -->
        <prototyper name="mylist">
          <list>
            <value>123</value>
          </list>
        </prototyper>
        <!-- map -->
        <prototyper>
          <map>
            <entry key="math" value="99"></entry>
          </map>
        </prototyper>
        <!-- properties类型 -->
        <prototyper name="props">
          <props>
            <prop key="jdbcUrl">jdbc:mysql://localhost:3306/stud</prop>
          </props>
        </prototyper>
      </bean>
    
    <!-- 通过Util引用其他bean的公共集合 
         需要在头部引入对象的配置
    -->
    <?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:util="http://www.springframework.org/schema/util"    
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util.xsd
            "
            >
    
      <!-- 集合类型 -->
      <util:list id="myList">
        <value>啊</value>
      </util:list>
      
      <!-- properties类型 -->
      <util:properties id="myconfig" location="classpath:xxx.properties"/>
    
      <bean id="myA" class="package.MyA">
        <prototype name="favs">
          <ref bean="myList"/>
        </prototype>
        <prototype name="myjdbc" ref="myconfig">
      </bean>
    </beans>
    
    1. 启动spring容器
    ApplicationContext ctx = new ClassPathXmlApplicationContext("config.xml");
    
    Object obj = ctx.getBean("beanId",Object.class);
    

    spring表达式

    读取其他bean的属性值
    #{bean的id。bean的属性}(调取对应bean的get方法)
    访问list
    访问map
    访问properties id。key

    通过注解配置

    xml中配置context组件扫描,以下添加了context组件和util组件

    <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:util="http://www.springframework.org/schema/util"    
        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/util 
            http://www.springframework.org/schema/util/spring-util.xsd
            "
            >
     <context:component-scan base-package="packageName"/>
    

    相关注解

    @component
    @service
    @controller
    @repository
    @postconstruct @predestroy
    @scope @lazy

    • @autowire @qualifier
      该注解支持set 和 构造器 注入
      可将autowired添加到set方法前面
      直接将注解写到属性前面 但是不会执行set方法

    • @inject @named
      已经被废弃的注解

    • @resource
      只支持set方式
      可将该注解添加到属性前,使用name属性指定要注入的bean的id 默认按照bytype方式注入

    • @value("#{id.key}")
      读取配置文件的属性值 通过spring表达式赋值
      直接在属性前赋值 在()中注入基本类型的值
      也可以加到set方法前面

    相关文章

      网友评论

          本文标题:spring

          本文链接:https://www.haomeiwen.com/subject/faphqqtx.html