01_快速上手Spring

官网 : http://spring.io/
官方下载地址 : https://repo.spring.io/libs-release-local/org/springframework/spring/
GitHub : https://github.com/spring-projects

1 spring入门程序

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
    </dependencies>

在这里插入图片描述

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

2 IOC创建对象方式

2.1通过无参构造方法来创建

    <bean id="userMysqlImpl" class="com.zs.dao.UserMySqlDaoImpl"/>
    <bean id="userOrcalImpl" class="com.zs.dao.UserOrcalDaoImpl"/>

2.2通过有参构造方法来创建

    <bean id="user" class="com.zs.pojo.User">
       <constructor-arg name="name" value="zs"/>
    </bean>

3 Spring简单配置

3.1配置别名

<alias name="hello" alias="hello2"/>
    <bean id="hello" name="hello2, hello3, hello4" class="com.zs.Hello">
        <property name="userName" value="赵帅"/>
    </bean>

3.2 通过import,将到多个配置文件导入为一个文件

在这里插入图片描述

4 DI

<?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">
       <bean id="address" class="com.zs.pojo.Address"/>
       <bean id="student" class="com.zs.pojo.Student">
          
       <property name="name" value="zs"/>
<!--           private Address address;-->
       <property name="address" ref="address"/>
<!--           private String[] books;-->
       <property name="books">
           <array>
               <value>西游记</value>
               <value>红楼梦</value>
               <value>水浒传</value>
           </array>
       </property>
<!--           private List<String> hobbys;-->
       <property name="hobbys">
           <list>
               <value>听歌</value>
               <value>看电影</value>
               <value>爬山</value>
           </list>
       </property>
<!--           private Map<String,String> card;-->
       <property name="card">
           <map>
               <entry key="中国邮政" value="456456456465456"/>
               <entry key="建设" value="1456682255511"/>
           </map>
       </property>
<!--           private Set<String> games;-->
       <property name="games">
           <set>
               <value>LOL</value>
               <value>BOB</value>
               <value>COC</value>
           </set>
       </property>
<!--           private String wife;-->
       <property name="wife">
           <null/>
       </property>
<!--           private Properties info;-->
       <property name="info">
           <props>
               <prop key="学号">20190604</prop>
               <prop key="性别"></prop>
               <prop key="姓名">小明</prop>
           </props>
       </property>
       </bean>
</beans>
Student(name=zs, address=Address(address=null), books=[西游记, 红楼梦, 水浒传], hobbys=[听歌, 看电影, 爬山], card={中国邮政=456456456465456, 建设=1456682255511}, games=[LOL, BOB, COC], wife=null, info={学号=20190604, 性别=男, 姓名=小明})

5 使用注解自动装配

在这里插入图片描述
在spring4之后,想要使用注解形式,必须得要引入aop的包:org.springframework:spring-aop:5.2.0.RELEASE
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值