Spring(一)

简介: Spring(一)

各位小伙伴,大家好!点个关注,更多精彩等着你哦!!

今天,勇哥我想跟大家分享有关Spring的入门小知识IOC容器,这是Spring入门系列课程,听说优秀的程序员都会点赞关注!

一、什么是IOC?

IOC即控制反转,将对象管理交给容器来管理,由容器帮我们查找并注入依赖的对象,对象中的属性行为等,对象只能被动的接收依赖对象,依赖对象的获取被反转了。IOC可以管理所有轻量级的JavaBean组件,提供的底层服务包括组件的生命周期管理、配置和组装服务、AOP支持,以及建立在AOP基础上的声明式事务服务等等。

IOC的作用:可降低组件之间的耦合度,便于软件之间的解耦。IOC容器提供众多服务,如事务管理等。当我们使用容器管理事务时,开发程序员就不再需要手工控制事务,这样就可以减少不必要的麻烦了,进一步提高工作效率。

如以下所举的列子:图中的中介就相当于Sping中的容器管理IOC,对象房东就交给容器来管理。

二、引入依赖注入。

这是我们一定会疑问,该如何使用Spring? 接下来,我教大家如何用maven导入项目所需的依赖包及创建一个项目。

1.首先用maven创建一个小项目,在pom.xml引入依赖包。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>org.example</groupId>
    <artifactId>spring2</artifactId>
    <version>1.0-SNAPSHOT</version>
 
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>
    </dependencies>
</project>

2.然后在项目中先创建一个包,在包中再创建Woman类

package demo1;
 
public class Woman {
    private String name;
    private int    age;
    private String outlook;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public String getOutlook() {
        return outlook;
    }
 
    public void setOutlook(String outlook) {
        this.outlook = outlook;
    }
 
    @Override
    public String toString() {
        return "Woman{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", outlook='" + outlook + '\'' +
                '}';
    }
}

3.在创建一个Man类

public class Man {
    private String name;
    private int  age;
    private double  height;
    private double  weight;
    private int     fund;
    private Woman woman;
 
 
    public void proposal(){
        System.out.println("这名男生名叫"+name+" \n年龄为"+age+"。是一个拥有资产为:"+fund+"的富豪。向一名为:"+
                woman.getName()+woman.getAge()+woman.getOutlook()+"的小姐姐求婚了");
 
    }
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public double getHeight() {
        return height;
    }
 
    public void setHeight(double height) {
        this.height = height;
    }
 
    public double getWeight() {
        return weight;
    }
 
    public void setWeight(double weight) {
        this.weight = weight;
    }
 
    public int getFund() {
        return fund;
    }
 
    public void setFund(int fund) {
        this.fund = fund;
    }
 
    public String getInterest() {
        return interest;
    }
 
    public void setInterest(String interest) {
        this.interest = interest;
    }
 
    private String  interest;
 
    @Override
    public String toString() {
        return "Man{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", height=" + height +
                ", weight=" + weight +
                ", fund=" + fund +
                ", interest='" + interest + '\'' +
                '}';
    }
 
    public void setWoman(Woman woman) {
        this.woman = woman;
    }
 
    public Woman getWoman() {
        return woman;
    }
}

4.最后再创建一个测试类。

package demo1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class SpringUser {
    public static void main(String[] args) {
        //ClassPathXmlApplicationContext是ApplicationContext的实现类
        //ApplicationContext是BeanFactory的子接口
        ApplicationContext context=new
                ClassPathXmlApplicationContext("classpath:ApplicationContext.xml");
        Woman xiaoyu = (Woman) context.getBean("xiaoyu");
        System.out.println(xiaoyu);
         Man yong=(Man) context.getBean(Man.class);
        System.out.println(yong);
        System.out.println(xiaoyu==yong.getWoman());
 
    }
}

5.在xml文件中配置Bean,其中包括基本属性。

bean元素:该元素用来描述需要spring容器管理对象

class属性:被管理对象的完整类名

id属性:与name属性一模一样,名称不可重复,及被管理对象的名字

name属性:给被管理的对象起名字,获得对象时可用于来获取属性值,getBean("name值")

   

<?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">
 
    <!--suppress SpringFacetInspection -->
    <bean id="xiaoyu" class="demo1.Woman">
        <property name="name"  value="小宇"/>
        <property name="age" value="18"/>
        <property name="outlook" value="沉鱼落雁"/>
    </bean>
    <bean id="yong" class="demo1.Man">
        <property name="name" value="小华"/>
        <property name="age"  value="20"/>
        <property name="fund" value="1000000000"/>
        <property name="height"  value="178"/>
        <property name="interest" value="爱打篮球,喜欢科研,热爱ai"/>
        <property name="weight" value="105"/>
        <property name="woman" ref="xiaoyu"/>
    </bean>
</beans>

最后,祝你们都有所收获,喜欢记得点个关注,更多精彩等着你哦!!


相关文章
|
SQL 运维 数据库
12-TDengine数据迁移:导入与导出
12-TDengine数据迁移:导入与导出
3028 0
12-TDengine数据迁移:导入与导出
|
存储 网络协议 Unix
网络基础:socket套接字
网络基础:socket套接字
217 0
|
10月前
|
监控 Java 开发者
什么是 Spring Boot?
什么是 Spring Boot?
2204 6
|
JavaScript Linux 开发工具
开源项目:使用 Atom-Electron 和 Vue.js 制作的简单 RSS 阅读器!!
开源项目:使用 Atom-Electron 和 Vue.js 制作的简单 RSS 阅读器!!
|
JSON 安全 数据格式
详解python pickle中的反序列化漏洞
今天我们来聊聊Python里的反序列化攻击。先来看看什么是序列化和反序列化。简单来说,序列化就是把数据结构转换成字节流,这样我们就可以把数据保存到文件里或者通过网络传输。反序列化则是把这些字节流再转换回原来的数据结构。 在Python里,常用的模块之一就是Pickle。它可以帮我们很方便地进行序列化和反序列化操作。比如,你可以把一个复杂的Python对象序列化保存下来,等需要用的时候再反序列化回来。 反序列化攻击的概述 反序列化过程有漏洞:如果我们反序列化了一个不可信的数据源,那就可能引发反序列化攻击。攻击者可以在序列化的数据里嵌入恶意代码,当你反序列化这个数据时,这些恶意代码就会被执
|
SQL 关系型数据库 MySQL
MySQL-在线处理大表数据 & 在线修改大表的表结构
MySQL-在线处理大表数据 & 在线修改大表的表结构
500 0
|
JavaScript 测试技术 API
如何从 Vue 2 无痛升级到 Vue 3,一文搞定!
如何从 Vue 2 无痛升级到 Vue 3,一文搞定!
|
设计模式 Java
小谈设计模式(26)—中介者模式
小谈设计模式(26)—中介者模式
|
数据可视化 jenkins Java
Jenkins之pipeline语法——2023.07
Jenkins之pipeline语法——2023.07
485 0
|
前端开发 数据格式
再次接触老朋友react+ant design table合并单元格
再次接触老朋友react+ant design table合并单元格
323 0