JavaWeb进阶-Spring学习笔记(持续更新)

    科技2022-07-12  136

    大家好,我是一只学弱狗,记录学习的点点滴滴!

    优质文章
    一张黄图的故事JavaSE练习项目我是菜鸟、我小试牛刀linux指令太多记不住?小白看这篇就够了!
    优质专栏
    数据库就该这样学爪哇外步篇

    Spring

    依赖注入

    IOC创建对象方式

    <?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 https://www.springframework.org/schema/util/spring-util.xsd"> <!-- 构造函数,下标赋值 --> <bean id="user" class="com.kuang.pojo.User"> <constructor-arg index="0" value="乐乐1"/> </bean> <!-- 构造函数:第二种方式,通过类型创建 --> <!-- <bean id="user" class="com.kuang.pojo.User">--> <!-- <constructor-arg type="java.lang.String" value="乐乐2"/>--> <!-- </bean>--> <!-- 构造函数:直接通过对象名 --> <bean id="user" class="com.kuang.pojo.User"> <constructor-arg name="name" value="乐乐3"/> </bean> <!-- 最常用的方式 --> <!-- <bean id="manager" class="com.kuang.pojo.Manager">--> <!-- <property name="name" value="乐乐"/>--> <!-- </bean>--> <!-- 别名,也可以通过name设置 --> <alias name="user" alias="uuser"/> <!-- id:bean的唯一标识符 class:bean对象所对应的全类名 name:别名,而且name可以同时取多个别名 --> <bean id="manager" class="com.kuang.pojo.Manager" name="manager2,manager3"> <property name="name" value="乐乐乐"/> </bean> <!-- 将多个bean文件导入到一起 --> <import resource="beans.xml"/> </beans>

    常见类型注入

    <bean id="student" class="com.kuang.pojo.Student"> <!-- 普通值注入 --> <property name="name" value="张三"/> <!-- Bean注入 --> <property name="address" ref="address"/> <!-- 数组注入 --> <property name="books"> <array> <value>红楼梦</value> <value>西游记</value> <value>水浒传</value> <value>三国演义</value> </array> </property> <!-- list注入 --> <property name="hobbys"> <list> <value>吃饭</value> <value>睡觉</value> <value>打豆豆</value> </list> </property> <!-- map注入 --> <property name="card"> <map> <entry key="身份证" value="123456789"/> <entry key="银行卡" value="987654321"/> </map> </property> <!-- set注入 --> <property name="games"> <set> <value>王者荣耀</value> <value>英雄联盟</value> </set> </property> <!-- property注入 --> <property name="info"> <props> <prop key="学号">123456</prop> <prop key="性别"></prop> </props> </property> <!-- null注入 --> <property name="wife"> <null/> </property> </bean>

    c命名和p命名注入

    <?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- p命名空间注入,可以直接注入属性的值--> <bean id="user" class="com.kuang.pojo.User" p:name="乐乐乐乐" p:age="18"/> <!-- c命名空间注入,通过构造器注入:construct--> <bean id="user2" class="com.kuang.pojo.User" c:name="可可" c:age="18" scope="prototype"/> </beans>

    Bean的作用域

    <!-- singleton prototype 其他的request、session、application在web开发中使用 -->

    自动装配

    使用byType和byName实现自动装配

    <!-- byName:会自动在容器上下文中查找,和自己对象set方法后面的字段名对应的beanid,区分大小写 byType:会自动在容器上下文中查找,和自己对象属性类型相同的beanid 必须保证类型全局唯一 --> <bean id="cat" class="com.kuang.pojo.Cat"/> <bean id="dog" class="com.kuang.pojo.Dog"/> <bean id="people" class="com.kuang.pojo.People" autowire="byType"> <property name="name" value="乐乐"/> </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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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/beans/spring-aop.xsd"> <context:annotation-config/> </beans>

    @Autowired:直接在属性上使用即可,也可以在set方式上使用;首先按照类型查询,其次按照id查询(即set方法后的字段名称),若仍发生冲突,则报错!

    @Qualifier(value = “”):可以显示的指定Bean的id值

    @Resource:和Autowired类似,先根据类型在ioc容器中查找,如存在多个,则按照set方法后的字段名称去查找,如仍在不存在,则报错,这时,可以指定其name值来装配,故我们可以看作时@Autowired和@Qualifier的结合

    Processed: 0.011, SQL: 8