SpringBoot Java类配置文件方式

Java类来配置文件

这个是XML来配置 Bean

<?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">

</beans>

Java类方式配置如下

@Data
public class Student{
    private int age;
    private String name;
}
@Configuration // 表示这个类是配置类
public class Config {
    @Bean // 把返回的对象 存入Spring容器中
    public Student getStudent(){
        return Student();
    }
}

使用这个类

@Controller
public class GameController {

    @Autowired  //自动类名为id 在容器中寻找类
    private Student student;

  @RequestMapping("/test")
    public String test(Model model){     
        System.out.println(student.name);
        return "login";
    }
}
posted @ 2023-03-25 11:20  起始者  阅读(48)  评论(0)    收藏  举报