SpringBoot中如何进行Bean配置

 时间:2024-10-26 07:30:58

SpringBoot中如何进行Bean配置

工具/原料

SpringBoot

intellijidea

方法/步骤

1、Bean配置。在使用Spring进行开发配置的时候有两类配置:*.xml配置文件、配置的Bean(@Configure),于是在SpringBoot的开发的时间里面,为了继续崇尚所谓的“零配置”,提供有一种简单的支持,也就是说如果现在你真的有配置需要通过*.xml文件编写,但是又不想出现配置文件的话,这个时候最简单的做法就是使用Bean的方式进行类的配置。前提:该配置程序的Bean所在的包必须是程序启动类所在包的子包之中,这样才可以自动扫描到。

2、下面准备一个不是很正规的程序,建立一个业务接口,而后定义这个接口的子类:packagecom.gwolf.service;publicinterfaceIMessageService{publicStringinfo();}

SpringBoot中如何进行Bean配置

3、创建一个子类实现IMessageService接口:packagecom.gwolf.service.impl;importcom.gwolf.service.IMessageService;importorg.springframework.stereotype.Service;@ServicepublicclassMessgeServiceImplimplementsIMessageService{@OverridepublicStringinfo(){return"www.baidu.com";}}

SpringBoot中如何进行Bean配置

4、在控制器MessageController中注入IMessageService:p瞢铍库祢ackageco罪焐芡拂m.gwolf.controller;importch.qos.logback.core.net.SyslogOutputStream;importcom.gwolf.service.IMessageService;importcom.gwolf.util.controller.AbstractBaseController;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;importjavax.annotation.Resource;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;@RestControllerpublicclassMessageControllerextendsAbstractBaseController{@ResourceprivateIMessageServicemessageService;@RequestMapping(value="/",method=RequestMethod.GET)publicStringidnex(){returnthis.messageService.info();}@RequestMapping(value="/echo",method=RequestMethod.GET)publicStringecho(Stringmid){System.out.println("访问:"+super.getMessage("member.add.action"));returnsuper.getMessage("welcome.msg",mid);}}

SpringBoot中如何进行Bean配置

5、建立一个测试类:packagecom.gwolf.t娣定撰钠est;importcom.gwolf.StartS禊诬娱飑pringBootMain;importcom.gwolf.controller.HelloController;importcom.gwolf.controller.MessageController;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importorg.springframework.test.context.web.WebAppConfiguration;importjavax.annotation.Resource;@SpringBootTest(classes=StartSpringBootMain.class)@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfigurationpublicclassTestMessageController{@ResourceprivateMessageControllermessageController;@TestpublicvoidtestIndex(){System.out.println(this.messageController.idnex());}@TestpublicvoidtestEcho(){System.out.print(this.messageController.echo("www.baidu.com"));}}

SpringBoot中如何进行Bean配置

6、下面利用以上的程序来了解缬方焱蜱一下什么叫做Bean配置,为了清楚的发现Bean的特点删除掉业务实现子类中鹚兢尖睁的“@Service”注解,也就是说这个对象现在无法直接注入,于是下面在启动类所在包的子包里面建立一个配置程序类:com.gwolf.config.ServiceConfig。packagecom.gwolf.config;importcom.gwolf.service.IMessageService;importcom.gwolf.service.impl.MessgeServiceImpl;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@Configuration//此处为配置项publicclassServiceConfig{@Bean//此处返回的是一个Spring的配置Bean,与xml的<bean>等价publicIMessageServicegetMessageService(){//方法名称随便写returnnewMessgeServiceImpl();}}

SpringBoot中如何进行Bean配置

7、现在再次验证程序是否能够正常执行:

SpringBoot中如何进行Bean配置

8、此时采用了自动扫描Bean的模式来进行相关对象的配置。SSM或SSH开发框架出现的时间比较长,现在迁移到SpringBoot之中,那么说如果你现在已经有一个非常完善的xml配置文件出现了,那么难道还需要将整个的xml配置文件转化为Bean配置吗?为了防止这类情况出现,SpringBoot也支持有配置文件的读取。这样就完成了SpringBoot中如何进行Bean配置。

jsp页面如何查看列表列表 ppt如何删除选择显示悬浮工具栏 膳食房膳食纤维为什么可以降血脂、预防冠心病? 资源部署及交付流程概述 有哪些免费实现网站评论的第三方评论系统插件
热门搜索
马桑果图片 美女空姐图片 动漫设计培训 169美女图片网 少女外阴图片