SringBoot-it楠老师

这篇博客详细介绍了SpringBoot中注入bean的多种方式,包括@Component和@Autowired注解、@Import以及@ConfigurationProperties的使用。此外,还讲解了YML配置文件的语法,配置文件与配置类的属性映射,以及web设置中的静态资源处理和错误页面配置。最后,博主探讨了拦截器和视图控制器的实现。
摘要由CSDN通过智能技术生成

SringBoot

spring注入bean有几种方式

(1)标签注入文件,这是之前spring的方式
(2)扫包:使用注解注入 @Component, @Configuration @Bean @Service @Controller @Repository @Import

例子:
实体类User

package com.zhou.entity;

import org.springframework.stereotype.Component;

// 把user对象注入到容器中
// 方式1:使用component
@Component
public class User {
   
    // 赋给值,方便测试getName
    private String name = "www.itnanls.cn";
//    private String name;

    public String getName() {
   
        return name;
    }

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

控制层,UserHandler

package com.zhou.controller;

import com.zhou.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

//@Controller// 不返回视图的话,使用RestController
@RestController
public class UserHandler {
   
    // 如果User类注入到容器中了,则在controller引入也可以使用User
    @Autowired
    private User user;
    
    @GetMapping("/hello")
    public String hello(){
   
       return user.getName();
    }
}

启动类

package com.zhou;

import com.zhou.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringbootDemo01Application {
   

    public static void main(String[] args) {
   
        SpringApplication.run(SpringbootDemo01Application.class, args);
    }
}

这个方式是使用@Component和@Autowired来注入

方式2:

package com.zhou.entity;

import org.springframework.stereotype.Component;

// 把user对象注入到容器中
// 方式1:使用component
//@Component
// 方式2:不使用@Componet,而是使用在启动类加个返回User的方法和@Bean
public class User {
   
    // 赋给值,方便测试getName
    private String name = "www.itnanls.cn";
//    private String name;

    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        this.name = name;
    }
}
package com.zhou.controller;

import com.zhou.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

//@Controller// 不返回视图的话,使用RestController
@RestController
public class UserHandler {
   
    // 如果User类注入到容器中了,则在controller引入也可以使用User
    @Autowired
    private User user;

    @GetMapping("/hello")
    public String hello(){
   
       return user.getName();
    }
}

启动类

package com.zhou;

import com.zhou.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringbootDemo01Application {
   

    public static void main(String[] args) {
   
        SpringApplication.run(SpringbootDemo01Application.class, args);
    }

    // 方式2:使用@Bean来注入,这里加了这个注入,那么User类中就可以不使用@Componet
    @Bean
    public User user(){
   
        return new User();
    }
}

测试成功
方式3:引入import

package com.zhou;

import com.zhou.entity.User
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值