栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

向Spring容器中添加组件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

向Spring容器中添加组件

如何向Spring容器中添加组件
SpringBoot推荐使用@Configuration+@Bean
@configuration 用在类上方,声明这个类是一个spring配置类,执行的功能是代替spring的配置文
件,相当与spring.xml中的 标签
@Bean 用在方法上方,@bean一般与@configuration配合使用,用在@configuration类中的获取实例
的方法上方。相当于spring.xml中的 标签
@Bean 常用参数:
name/value :用于指定Bean的名称,可以指定多个。如果省略不写,bean的名称就是方法名称。
@Bean 常搭配的注解
@Scope(“value”) :用于指定bean的Scope 值可以为 prototype singleton(默认) request
session
@Description(“value”) :指定Bean的描述 可以看作是一种注释
package com.gx.springbootdemoinitializr;
import com.gx.springbootdemoinitializr.vo.Child;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.context.annotation.Scope;
@Configuration
public class BeanConfig {
@Bean //name(value) 用于指定Bean的名称,可以指定多个。如果省略不写,bean的名称就是方法
名称。
@Scope(“singleton”)//指定bean的Scope 值可以为 prototype singleton(默认)
request session
@Description(“这是描述”)//指定Bean的描述 可以看作是一种注释
public Child mChild(){
Child child=new Child();
child.setName(“杰克”);
child.setAge(10);
return child;
}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/915519.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号