Spring笔记(一)环境搭建,ioc

spring:

官网

https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#spring-core

一、下载JAR包

https://maven.springframework.org/release/org/springframework/spring/4.3.9.RELEASE/
以4.3.9版本为例(spring-framework-4.3.9.RELEASE-dist.zip)

开发spring至少需要使用的jar(5个+1个):
spring-aop. jar开发AOP特性时需要的JAR
spring-beans. jar处理Bean的jar
spring-context. jar处理spring_上下文的jar
spring-core. jarspring核心jar
spring- expression. jarspring表达式
三方提供的日志jar
commons- logging. jar日志

或者直接一个spring-webmvc

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.2.0.RELEASE</version>
</dependency>

二、编写配置文件

为了编写时有一-些提示、自动生成一 些配置信息,可以增加支持spring的插件:	spring tool suite

(services.xml)配置文件👇
实际上就是上面get,如果把get方法删除则会报错
创建bean就相当于new了一个对象,默认调用无参

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- services -->
    <!--使用Spring创建对象,在Spring中称为Bean
    
        类型   变量名 =  new  类型();
        HeLlo  hello =  new  Hello();
            id       =  变量名
            class    =  new的对象; .
        property    相当于给对象中的属性设置一个值!

    -->
    <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
        <property name="accountDao" ref="accountDao"/>
        <property name="itemDao" ref="itemDao"/>
        <!-- additional collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions for services go here -->

</beans>

三、使用容器

不再需要new

public class MyTest {
    public static void main(String[] args) {
        //获取spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
}

IoC:

控制反转

一、概念

1、before

UserDao 👉 UserDaoImpl 👉 UserService 👉 UserServiceImpl 👉 main

-------------👉MysqlDaoImpl👉 UserService 👉 UserServiceImpl 👉 main

在这里插入图片描述

必须修改UserService

2、after

在这里插入图片描述
在这里插入图片描述
用户增加需求模块,不需改修改底层代码

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--默认使用无参构造-->
<!--    <bean id="user1" class="User">-->
<!--        <property name="name" value="zs"/>-->
<!--    </bean>-->

    <!--使用有参构造-->
    <!--下标赋值-->
    <bean id="user2" class="User">
        <constructor-arg index="0" value="张三"/>
    </bean>

    <!--通过类型赋值(不实用)-->
    <bean id="user3" class="User">
        <constructor-arg type="java.lang.String" value="张三"/>
    </bean>

    <!--参数名赋值(主要)-->
    <bean id="user4" class="User">
        <constructor-arg name="name" value="张三"/>
    </bean>
</beans>

Spring配置

一、别名

    <!--别名-->
    <alias name="user2" alias="new"/>

有了别名,用别名也能调用

二、Bean的配置

    <!--
        id:bean 的唯一标识符,也就是相当于我们学的对象名
        class: bean 对象所对应的全限定名: 包名 + 类型
        name:也是别名,而且name可以取多个别名(,;都可以)
    -->
    <bean id="user2" class="User" name="newuser,u2;u3">
        <constructor-arg index="0" value="张三2"/>
    </bean>

三、import

一般用于团队开发,将多个配置文件导入合并成一个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值