Spring(二):快速上手Spring

15 篇文章 0 订阅

先导入jar包

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

1.编写实体类

package com.Devin.pojo;

public class Hello {
    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

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

2.配置xml:命名为beans.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来创建对象,在spring这些都称为Bean
        类型 变量名 = new 类型()
        Hello hello = new Hello();

        id:变量名
        class:new 的对象(全限定类名)
        property:相当于给对象当中的属性设置一个值!
        -->

        <bean id="hello" class="com.Devin.pojo.Hello">
            <property name="str" value="Spring"/>
        </bean>


</beans>

3.测试

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTast {
    public static void main(String[] args) {
        /*
        获取spring的上下文对象!
         */
        //获取ApplicationContext
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //我们的对象现在都在spring中的管理了,我们要使用,直接去里面取出来就可以了
        Object hello = context.getBean("hello");
        System.out.println(hello.toString());
    }
}

IOC创建对象方式

1.使用无参构造方法创建对象(默认的)

1.实体类:

public class User {

   private String name;

   public User() {
       System.out.println("user无参构造方法");
  }

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

2.配置xml:命名为beans.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
       http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="user" class="com.Devin.pojo.User">
       <property name="name" value="Devin"/>
   </bean>

</beans>

3.测试:

@Test
public void test(){
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   //在执行getBean的时候, user已经创建好了 , 通过无参构造
   User user = (User) context.getBean("user");
   //调用对象的方法 .
   user.show();
}
2.使用有参构造创建对象方式:
2.1:下标赋值:
<!--有参构造第一种方法:下标赋值:constructor-arg index=""-->
<bean id="user" class="com.Devin.pojo.User">
	<!-- index指构造方法 , 下标从0开始 -->
    <constructor-arg index="0" value="Devin学Java"/>
</bean>
2.2:类型赋值:
<!--类型赋值:constructor-arg type=""-->
<bean id="user" class="com.Devin.pojo.User">
    <constructor-arg type="java.lang.String" value="Devin"/>
</bean>
2.3:直接通过参数名赋值:

重点掌握

<bean id="user" class="com.Devin.pojo.User">
	 <!-- name指参数名 -->
    <constructor-arg name="name" value="书榜学Java"/>
</bean>

Spring配置

5.1 别名:alisa
<!--别名,如果添加了别名,我们可以通过别名去访问-->
<alias name="user" alias="JING"/>
5.2 Bean的配置
<!--
    id:bean 的唯一标识,相当于我们的对象名
    class bean对象所对应的全限定类名:包名+类名
    name: 别名 ,比alisa更强大 ,可以起多个,中间可以用逗号,分号,空格分隔都可以
    -->
<bean id="user" class="com.Devin.pojo.User" name="u1,u2 u3;u4">
    <constructor-arg name="name" value="书榜学Java"/>
</bean>
5.3 import

主要用于项目合并文件使用

创建一个主xml文件,把下面的xml文件导入到主xml文件,在运行时,直接运行住xml文件就可以

//获取
        ApplicationContext context = new ClassPathXmlApplicationContext("主.xml");
<beans>
    <import resource="CTIContext.xml" />
    <import resource="customerContext.xml" />
    <import resource="customerServingContext.xml" />
    <import resource="manageContext.xml" />
    <import resource="routineContext.xml" />
    <import resource="systemContext.xml" />

本文章来源于B站(狂神说) B站地址:https://space.bilibili.com/95256449

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值