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:表示当前类是作为配置文件使用的。 就是用来配置容器的
 *       位置:在类的上面
 *
 *  SpringConfig这个类就相当于beans.xml
 */
@Configuration
public class SpringConfig {

    /**
     * 创建方法,方法的返回值是对象。 在方法的上面加入@Bean
     * 方法的返回值对象就注入到容器中。
     *
     * @Bean: 把对象注入到spring容器中。 作用相当于<bean>
     *
     *     位置:方法的上面
     *
     *     说明:@Bean,不指定对象的名称,默认是方法名是 id
     *
     */
    @Bean
    public Student createStudent(){
        Student s1  = new Student();
        s1.setName("张三");
        s1.setAge(26);
        s1.setSex("男");
        return s1;
    }


    /***
     * 指定对象在容器中的名称(指定<bean>的id属性)
     * @Bean的name属性,指定对象的名称(id)
     */
    @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

    <bean id="myCat" class="com.bjpowernode.vo.Cat">
        <property name="name" value="tom猫"/>
        <property name="age" value="2" />
        <property name="cardId" value="uw532423422"/>
    </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就能正常使用了,不然’东北老虎‘这个字段就会乱码

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

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值