day2 springBoot学习笔记--动力节点

动手试试呗:1.创建一个Tiger类

package com.rll.vo;

public class Tiger {
    private String name;
    private Integer age;

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

2.新建一个config.properties文件

 3.要用中文的话就要设置file-->settings-->

 4.完善config.properties文件

 5.在Tiger类对象上加注解@Component("tiger")

- 在每个属性上加上@Value("${tiger.属性名}")

6.然后在SpringConfig.java里面的类名上面加上@PropertySource(value="文件名")

这里要找注解,就得扫描组件

@ComponentScan(basePackages = "com.rll.vo")

so:

 

package com.rll.config;

import com.rll.vo.Student;
import org.springframework.context.annotation.*;

/*
* Configuration:表示当前类是作为配置文件使用的,就是用来配置容器的
*    位置:在类的上面
*
*    SpringConfig这个类就相当于beans.xml
*
* */

@Configuration
@ImportResource(value = "classpath:applicationContext.xml")
@PropertySource(value = "classpath:config.properties")
@ComponentScan(basePackages = "com.rll.vo")
public class SpringConfig {

    /*
    *创建方法,方法的返回值是对象。在方法的上面加入@Bean
    *方法的返回值对象就注入到容器中
    *
    * @Bean:把对象注入到spring容器中
    * 作用相当于<bean>
    * 位置:方法的上面
    *
    * 说明:@Bean,不指定对象的名称,默认是方法名是 id
    * */

    //不指定对象的名称,默认的方法名是id
    @Bean
    public Student createStudent(){
        Student s1= new Student();
        s1.setAge(90);
        s1.setName("zyy");
        s1.setSex("man");
        return s1;
    }

    /*
    * 指定对象在容器中的名称(等价于:指定<bean>的id属性)
    *@Bean的name属性,指定对象的名称(id)
    * */
    @Bean(name = "ListStudent")
    public Student makeStudent(){
        Student s1= new Student();
        s1.setAge(90);
        s1.setName("lisi");
        s1.setSex("man");
        return s1;
    }
}

测试一下呗

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值