SpringBoot第一章----javaconfig和xml关系

SpringBoot第一章----javaconfig和xml关系,第1张

SpringBoot第一章----javaconfig和xml关系

动力节点springboot视频教程-专为springboot初学者打造的教程_哔哩哔哩_bilibili你的三连就是录制视频的动力!一定不要忘记收藏、点赞、投币哦~本套视频基于SpringBoot2.4版本讲解。教程从细节入手,每个事例先讲解pom.xml中的重要依赖,其次application配置文件,最后是代码实现。让你知其所以,逐步让掌握SpringBoot框架的自动配置,starter起步依赖等特性。 为什么SpringBoot是创建Spring应用,必须了解spring-boot-starhttps://www.bilibili.com/video/BV1XQ4y1m7ex?p=7&spm_id_from=pageDriver以上是博主对动力节点关于本章节的的知识总结,如若侵权请联系博主删除本博客。本博客只用于博主复习和各位了解掌握,谢谢。

目录

一、为什么要用springboot

二、以下案例的结构关系

三、浅谈JavaConfig

四、@importResource和@PropertyResource

1、@importResource

2、@PropertyResource


一、为什么要用springboot

1. 为什么要使用 Spring Boot

   因为Spring, SpringMVC 需要使用的大量的配置文件 (xml文件)

   还需要配置各种对象,把使用的对象放入到spring容器中才能使用对象

   需要了解其他框架配置规则。

2. SpringBoot 就相当于 不需要配置文件的Spring+SpringMVC。 常用的框架和第三方库都已经配置好了。

   拿来就可以使用了。

3. SpringBoot开发效率高,使用方便多了

二、以下案例的结构关系

 

三、浅谈JavaConfig

javaConfig: 使用java类作为xml配置文件的替代, 是配置spring容器的纯java的方式。 在这个java类这可以创建java对象,把对象放入spring容器中(注入到容器), 

使用两个注解:

1)@Configuration : 放在一个类的上面,表示这个类是作为配置文件使用的。

2)@Bean:声明对象,把对象注入到容器中。

例如:我们可以创建一个类叫SpringConfig,用这个类来代替以往在xml写bean的功能

package com.bjpowernode.config;

import com.bjpowernode.vo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class SpringConfig {

    
    @Bean
    public Student createStudent(){
        Student s1  = new Student();
        s1.setName("张三");
        s1.setAge(26);
        s1.setSex("男");
        return s1;
    }


    
    @Bean(name = "lisiStudent")
    public Student makeStudent(){
        Student s2  = new Student();
        s2.setName("李四");
        s2.setAge(22);
        s2.setSex("男");
        return s2;
    }
}
四、@importResource和@PropertyResource 1、@importResource

@importResource作用导入其他的xml配置文件, 等于把指定路径的xml文件的bean都导入了@importResource所在的类中

1)我们在com.bjpowernode.vo目录下创建实体类cat

//cat的实体类
package com.bjpowernode.vo;

public class Cat {

    private String cardId;
    private String name;
    private Integer age;

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

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

2)在Resource目录下创建了一个applicationContext.xml文件且写了一个bean

    
        
        
        
    

然后再com.bjpowernode.config的springconfig类中添加上一行

@importResource(value = "classpath:applicationContext.xml")

3)测试结果

  @Test
    public void test04(){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        Cat cat  = (Cat) ctx.getBean("myCat");
        System.out.println("cat=="+cat);
    }

 

 当我们直接从springConfig中获取mycat的bean对象时候,是可以直接获取的

2、@PropertyResource

@PropertyResource: 读取properties属性配置文件。 使用属性配置文件可以实现外部化配置在程序代码之外提供数据。

1)在Resouce目录下创建config.properties

tiger.name=东北老虎
tiger.age=3

2)在com.bjpowernode.vo目录下创建Tiger实体类

package com.bjpowernode.vo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("tiger")
public class Tiger {

    @Value("${tiger.name}")
    private String name;
    @Value("${tiger.age}")
    private Integer age;

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

 注意,这里在Tiger上面加入@Component("tiger")是为了后面可以与javaConfig这个类关联,因为后面有一个@ComponentScan来扫描这个实体类,@Value学过mybatis我们都知道是直接复制给name和age

3)关联起来

在SpringConfig类上面加入指明他们的关系就可以了

@PropertySource(value = "classpath:config.properties")
@ComponentScan(basePackages = "com.bjpowernode.vo")

4)使用

使用方法和前面一样,建立一个test来测试,此时SpringConfig已经有了tiger这个bean了

    @Test
    public void test05(){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
       Tiger tiger = (Tiger) ctx.getBean("tiger");
        System.out.println("tiger=="+tiger);
    }

 5)结果

注意一点:我们在使用config.properties前需要前往setting->file encodings把里面全设置成utf-8

 改成这样,config.properties就能正常使用了,不然’东北老虎‘这个字段就会乱码

好了,第一章就这么多了,后续我会持续更新,谢谢大家观看

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5692635.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存