SpringBoot @Bean注解

  • 时间:
  • 浏览:
  • 来源:互联网

SpringBoot中,我们可以使用@Bean将自定义的一个类注入到spring容器中,让spring容器去管理bean,其相当于之前的配置文件中的<bean id="" class=""/>

直接贴代码,我们先建立一个配置类,类似之前的配置xml,然后在其中将自己的bean交给spring去管理。

package com.mayun.study.config;

import com.mayun.study.bean.Monster;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class BeanConfig {
    @Bean
    public Monster monster(){
        return new Monster();
    }
}

当然,Monster类是我们自定义的

package com.mayun.study.bean;

public class Monster {

    private Integer id = 10 ;
    private String nickname = "牛魔王";
    private String skill = "芭蕉扇";
    public Monster() {

        System.out.println("monster 创建..");
    }
    public Monster(Integer id, String nickname, String skill) {
        //System.out.println("Integer id, String nickname, String skill被调用");
        this.id = id;
        this.nickname = nickname;
        this.skill = skill;
    }

    public Monster( String nickname, String skill,Integer id) {

        this.id = id;
        this.nickname = nickname;
        this.skill = skill;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public String getSkill() {
        return skill;
    }
    public void setSkill(String skill) {
        this.skill = skill;
    }
    @Override
    public String toString() {
        return "Monster [id=" + id + ", nickname=" + nickname + ", skill="
                + skill + "]";
    }


}

这样Monster这个类就交给spring容器去管理了,那么怎么取呢

第一种:直接在其他需要的类中使用注解@Autowired注入

第二种:比较原始的,手动从spring容器中获取:

这里编写一个获取的工具类(网上直接拿过来的)

package com.mayun.study.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (null == SpringUtil.applicationContext) {
            SpringUtil.applicationContext = applicationContext;
        }
        System.out.println("========ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext="+SpringUtil.applicationContext+"========");
    }

    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过name获取 Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }


}

然后直接在测试类中使用工具类获得这个bean

package com.mayun.study.controller;

import com.mayun.study.bean.Monster;
import com.mayun.study.common.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {



//    @Autowired
//    Monster monster;

    @GetMapping("/application")
    public String application(){
        Monster monster = (Monster) SpringUtil.getBean("monster");
        monster.setNickname("sssssss");

        Monster monster1 = (Monster) SpringUtil.getBean("monster");
        System.out.println(monster + "hascode==" + monster.hashCode());
        System.out.println(monster1 + "hascode==" + monster1.hashCode());
        return "hello world";
    }
}

 

本文链接http://element-ui.cn/article/show-17478.aspx