程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

Spring框架配置使用的流程

发布于2021-05-29 19:40     阅读(1107)     评论(0)     点赞(23)     收藏(5)


加载配置文件

在这里插入图片描述

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>javase</groupId>
    <artifactId>0527</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Spring 需要的依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <!-- 日志需要的依赖 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

初始化(根据配置项,进行初始化,比如定义的bean进行实例化)

在这里插入图片描述

application.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">


    <!-- 通过bean标签定义bean对象,Spring Bean容器是通过id来进行管理的  -->
    <!-- id相当于bean的名称,Spring可以通过id找到bean对象, 如果没有提供id 会生成自动id
         默认是单例模式,只会实例化一个对象 -->
    <!-- 通过无参的构造方法,创建一个对象,如果该类型没有无参的构造方法就会报错 -->
    <bean id="Josvin" class="java.lang.String"></bean>
    <bean  class="java.lang.String"><!-- 在这里就会生成一个id 为string 的字符串对象-->

    <!---->

    </bean>


</beans>

使用(根据容器获取到Bean对象)

在这里插入图片描述


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @ClassName Main
 * @Description :TODO
 * @Author Josvin
 * @Date 2021/05/27/19:39
 */

public class Main {
    public static void main(String[] args) {
        // spring 开启容器的方式:ApplicationContext   应用上下文(可以配置并管理bean对象,等)               父类引用指向子类对象
        // ClassPathXmlApplicationContext  :根据ClassPath路径,指定一个xml文件(配置文件),
        // 根据配置文件完成配置工作(如bean的实例化)
        ApplicationContext context = new
                ClassPathXmlApplicationContext("applications.xml");
        new String();
        // 通过Bean的名称获取bean对象,bean名称就是xml中的bean的id
        String str = (String) context.getBean("Josvin");
        String str2 = (String) context.getBean("java.lang.String#0");
        System.out.println(str);
        System.out.println(str2);

        // 通过类型获取bean对象,如果该类型有多个对象就会报错,只支持一个该类型的对象
        //context.getBean(String.class);

    }
}

在这里插入图片描述

有参构造

在这里插入图片描述
在这里插入图片描述

定义一个自己的类

package Josvin;

/**
 * @ClassName Duck
 * @Description :TODO
 * @Author Josvin
 * @Date 2021/05/27/21:09
 */
public class Duck {
    private String name;
    private Integer age;

    public Duck(String s) {
    }

    @Override
    public String toString() {
        return "Duck{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

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

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

初始化

在这里插入图片描述

使用

在这里插入图片描述
在这里插入图片描述

如何在实例化时注入其属性

有构造方法:constructor-arg(构造方法注入)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

无构造方法(属性注入)

在这里插入图片描述
在这里插入图片描述

原文链接:https://blog.csdn.net/weixin_45532227/article/details/117337643



所属网站分类: 技术文章 > 博客

作者:天花灯

链接:http://www.javaheidong.com/blog/article/207085/6f9b29c5bda8e5ad0112/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

23 0
收藏该文
已收藏

评论内容:(最多支持255个字符)