Spring 基础系类一

Spring学习

1: Spring理念
2:Spring 为了解决企业应用级复杂问题的架构,本身是一个大杂烩,整合了现有的基础框架
3:Spring 就是一个非入侵式轻量级的控制反转(IOC)和面向切面编程(AOP)的框架
<!-- Spring 就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架 

//pom.xml 中一引入spring基础库
-->
  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.15.RELEASE</version>
        </dependency>
    </dependencies>

  • SpringBoot
    • 一个快速开发的脚手架
    • 基于SpringBoot可以快速开发单个微服务
    • 约定大于配置
  • SpringCloud
    • SpringCloud是基于SpringBoot实现的

市面上都在使用SpringBoot进行快速开发,学习SpringBoot的前提,需要掌握Spring 以及SpringMVC,承上启下的作用

Spring: 配置十分繁琐,人称:“配置地狱”,使用SpringBoot解决

Spring核心组成

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bnN1lcCm-1627027625229)(Spring%E5%AD%A6%E4%B9%A0.assets/20180806221835285.bmp)]

spring 学习官网 https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core

IOC理论推导


ioc 控制反转:对象由Spring来控制,管理,配置

// UserDao接口
// UserDaoImpl实现
//UserService业务接口
//UserServiceImpl业务接口实现

public interface UserDao {
    void getUser();
}


public class UserDaoImpl implements UserDao{
    @Override
    public void getUser() {
        System.out.println("获取默认用户数据");
    }
}


public class UserDaoMysqlImpl implements UserDao{
    @Override
    public void getUser() {
        System.out.println("获取mysql数据库用户数据");
    }
}

public interface UserService {
    void getUser();
}



public class UserServiceImpl implements UserService{
    private UserDao userDao;
    public void setUser(UserDao userDao){
        this.userDao=userDao;
    }
    @Override
    public void getUser() {
         userDao.getUser();
    }
}

Spring 实例化对象


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());

        UserService service=new UserServiceImpl();
        ((UserServiceImpl)service).setUser(new UserDaoImpl());
        service.getUser();
    }
}
<?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">
    <!-- Spring 来创建对象
    id =context 获取对象的变量名
    class=实例化对象的类
    name 类中的属性名
    value 属性的值
    -->
    <bean id="hello" class="pojo.Hello">
            <property name="string" value="spring"></property>
    </bean>
</beans>
package pojo;

public class Hello {
    String string;

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "string='" + string + '\'' +
                '}';
    }
}

最终通过 Spring 实例化所有类

<?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">
    <!-- Spring 来创建对象
    id =context 获取对象的变量名
    class=实例化对象的类
    name 类中的属性名
    value 属性的值
    -->
    <bean id="hello" class="pojo.Hello">
            <property name="string" value="spring"></property>
    </bean>
    <bean id="user" class="dao.UserDaoImpl"/>
    <bean id="user_sql" class="dao.UserDaoMysqlImpl"/>

    <bean id="user_service_impl" class="service.UserServiceImpl">
        <property name="user" ref="user_sql"/>
    </bean>


</beans>

以上Spring都是使用类的无参构造器

当Hello中有 有参构造时

package pojo;

public class Hello {
    String string;
    int age;

    Hello(int age,String string){
        this.age=age;
        this.string=string;
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "string='" + string + '\'' +
                '}';
    }
}

<?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">
    <!-- Spring 来创建对象
    id =context 获取对象的变量名
    class=实例化对象的类
    name 类中的属性名
    value 属性的值
    -->
    <bean id="hello" class="pojo.Hello">
        <constructor-arg name="string" value="spring"></constructor-arg>
        <constructor-arg name="age" value="11"/>
    </bean>
    <bean id="user" class="dao.UserDaoImpl"/>
    <bean id="user_sql" class="dao.UserDaoMysqlImpl"/>

    <bean id="user_service_impl" class="service.UserServiceImpl">
        <property name="user" ref="user_sql"/>
    </bean>


</beans>

Spring配置


别名
<?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">
    <!-- Spring 来创建对象
    id =context 获取对象的变量名
    class=实例化对象的类
    name 类中的属性名
    value 属性的值
    -->
    <bean id="hello" class="pojo.Hello" name="hello3">
        <constructor-arg name="string" value="spring"></constructor-arg>
        <constructor-arg name="age" value="11"/>
    </bean>
    <bean id="user" class="dao.UserDaoImpl"/>
    <bean id="user_sql" class="dao.UserDaoMysqlImpl"/>

    <bean id="user_service_impl" class="service.UserServiceImpl">
        <property name="user" ref="user_sql"/>
    </bean>
    <alias name="hello" alias="hello2"
/>

</beans>
  //获取spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Hello hello= (Hello) context.getBean("hello2");
        Hello hello1= (Hello) context.getBean("hello3");
        System.out.println( hello.toString());
        System.out.println( hello1.toString());

import

当项目中团队开发有多个 spring 的ioc 实例化对象的xml,可以使用import 导入合并到applicationContext.xml中

<?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">
    <!-- Spring 来创建对象
    id =context 获取对象的变量名
    class=实例化对象的类
    name 类中的属性名
    value 属性的值
    -->
<import resource="beans.xml"/>
</beans>
 //获取spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Hello hello= (Hello) context.getBean("hello3");
        System.out.println( hello.toString());

依赖注入


构造器注入
Set方式注入(重点)

复杂的类的主入

package pojo;

import java.util.*;

public class Student {
    private String name;
    private String age;
    private String sex;
    private String[] books;
    private List<String> hobbits;
    private Map<String,String> cards;
    private Set<String> course;
    private Properties info;

    public Hello getHello() {
        return hello;
    }

    public void setHello(Hello hello) {
        this.hello = hello;
    }

    private Hello hello;

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbits() {
        return hobbits;
    }

    public void setHobbits(List<String> hobbits) {
        this.hobbits = hobbits;
    }

    public Map<String, String> getCards() {
        return cards;
    }

    public void setCards(Map<String, String> cards) {
        this.cards = cards;
    }

    public Set<String> getCourse() {
        return course;
    }

    public void setCourse(Set<String> course) {
        this.course = course;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                ", books=" + Arrays.toString(books) +
                ", hobbits=" + hobbits +
                ", cards=" + cards +
                ", course=" + course +
                ", info=" + info +
                ", hello=" + hello +
                '}';
    }
}

<?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="student" class="pojo.Student">
        <property name="hello" ref="hello"/>
        <property name="age" value="11"/>
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>京瓶梅</value>
            </array>
        </property>
        <property name="hobbits">
            <list>
                <value>游泳</value>
                <value>下棋</value>
                <value>跑步</value>
            </list>
        </property>
        <property name="cards">
            <map>
                <entry key="银行卡" value="12333333333333333"></entry>
                <entry key="身份证" value="4444444444444444444444444"></entry>
            </map>
        </property>
        <property name="course">
            <set>
                <value>数学</value>
                <value>英语</value>
                <value>物理</value>
                <value>化学</value>
            </set>
        </property>
        <property name="name" value="zhangsan"/>
        <property name="sex" value="man"/>
        <property name="info">
            <props>
                <prop key="学号">20211122</prop>
                <prop key="宿舍">西苑2栋201</prop>
            </props>
        </property>
    </bean>
</beans>

执行测试类

  //获取spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Hello hello= (Hello) context.getBean("hello3");
        System.out.println( hello.toString());

        UserServiceImpl service= (UserServiceImpl) context.getBean("user_service_impl");
        service.getUser();
        Student student= (Student) context.getBean("student");
        System.out.println( student.toString());

最终输出

Hello{string='spring'}
获取mysql数据库用户数据
Student{name='zhangsan', age='11', sex='man', books=[红楼梦, 西游记, 水浒传, 京瓶梅], hobbits=[游泳, 下棋, 跑步], cards={银行卡=12333333333333333, 身份证=4444444444444444444444444}, course=[数学, 英语, 物理, 化学], info={学号=20211122, 宿舍=西苑2栋201}, hello=Hello{string='spring'}}

拓展式注入

p命名空间: xmlns:p=“http://www.springframework.org/schema/p”

属性值注入

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

c命名空间

构造方法注入

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

bean的作用域

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SPaujyzV-1627027625232)(Spring%E5%AD%A6%E4%B9%A0.assets/1625645820826.png)]

sigleton 单例模式

默认就是单例模式

prototype 原型模式

每次获取的对象都是一个新对象

其余模式

只能在web开发中使用

bean的自动装配


  • 自动装配是Spring满足bean依赖的一种方式
  • Spring会在上下文中自动寻找,并自动给bean装配属性
  • 在Spring中装配的三种方式
    • 在xml中显示装配(上面的例子就是)
    • 在java中显示配置
    • 隐士的自动装配bean(重要)
隐士自动装配

自动装配student中的Hello 类

byName方式
<bean id="student" class="pojo.Student" autowire="byName">
<?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="student" class="pojo.Student" autowire="byName">
        <property name="age" value="11"/>
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>京瓶梅</value>
            </array>
        </property>
        <property name="hobbits">
            <list>
                <value>游泳</value>
                <value>下棋</value>
                <value>跑步</value>
            </list>
        </property>
        <property name="cards">
            <map>
                <entry key="银行卡" value="12333333333333333"></entry>
                <entry key="身份证" value="4444444444444444444444444"></entry>
            </map>
        </property>
        <property name="course">
            <set>
                <value>数学</value>
                <value>英语</value>
                <value>物理</value>
                <value>化学</value>
            </set>
        </property>
        <property name="name" value="zhangsan"/>
        <property name="sex" value="man"/>
        <property name="info">
            <props>
                <prop key="学号">20211122</prop>
                <prop key="宿舍">西苑2栋201</prop>
            </props>
        </property>
    </bean>
</beans>
byType
    <bean id="student" class="pojo.Student" autowire="byType">
<?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="student" class="pojo.Student" autowire="byType">
        <property name="age" value="11"/>
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>京瓶梅</value>
            </array>
        </property>
        <property name="hobbits">
            <list>
                <value>游泳</value>
                <value>下棋</value>
                <value>跑步</value>
            </list>
        </property>
        <property name="cards">
            <map>
                <entry key="银行卡" value="12333333333333333"></entry>
                <entry key="身份证" value="4444444444444444444444444"></entry>
            </map>
        </property>
        <property name="course">
            <set>
                <value>数学</value>
                <value>英语</value>
                <value>物理</value>
                <value>化学</value>
            </set>
        </property>
        <property name="name" value="zhangsan"/>
        <property name="sex" value="man"/>
        <property name="info">
            <props>
                <prop key="学号">20211122</prop>
                <prop key="宿舍">西苑2栋201</prop>
            </props>
        </property>
    </bean>
</beans>
使用注解的方式自动装配(重点)
  • 使用须知

    • 导入约束

    • 配置注解支持 context:annotation-config/

      <?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"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
              https://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context
              https://www.springframework.org/schema/context/spring-context.xsd">
      
          <!-- 开启注解支持-->
          <context:annotation-config/>
      
      </beans>
      
    • @autowired 注解使用

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mhqvFCZi-1627027625234)(Spring%E5%AD%A6%E4%B9%A0.assets/1625649391565.png)]

去掉hello 类的xml中autowired

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
    <bean id="student" class="pojo.Student" >
        <property name="age" value="11"/>
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>京瓶梅</value>
            </array>
        </property>
        <property name="hobbits">
            <list>
                <value>游泳</value>
                <value>下棋</value>
                <value>跑步</value>
            </list>
        </property>
        <property name="cards">
            <map>
                <entry key="银行卡" value="12333333333333333"></entry>
                <entry key="身份证" value="4444444444444444444444444"></entry>
            </map>
        </property>
        <property name="course">
            <set>
                <value>数学</value>
                <value>英语</value>
                <value>物理</value>
                <value>化学</value>
            </set>
        </property>
        <property name="name" value="zhangsan"/>
        <property name="sex" value="man"/>
        <property name="info">
            <props>
                <prop key="学号">20211122</prop>
                <prop key="宿舍">西苑2栋201</prop>
            </props>
        </property>
    </bean>
</beans>

使用注解开发


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值