IOC操作Bean管理注解方式(完全注解开发)
生活随笔
收集整理的這篇文章主要介紹了
IOC操作Bean管理注解方式(完全注解开发)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
IOC操作Bean管理注解方式(完全注解開(kāi)發(fā))
?
(1)創(chuàng)建配置類,替代xml配置文件
需要讓Spring 把一個(gè)普通的類認(rèn)為是配置類
?結(jié)構(gòu)圖:
?
SpringConfig類代碼如下:
package com.lbj.spring5.comfig;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;@Configuration //作為配置類,替代xml文件 @ComponentScan(basePackages = {"com.lbj"}) //以數(shù)組形式寫入基本掃描的包 public class SpringConfig {}測(cè)試文件:
package com.lbj.spring5.testdemo;import com.lbj.spring5.comfig.SpringConfig; import com.lbj.spring5.service.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring5Demo1 {@Testpublic void testService02(){ // 換配置類ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);UserService userService = context.getBean("userService", UserService.class);System.out.println(userService);userService.add();}}測(cè)試結(jié)果:
?
應(yīng)用:SpringBoot中做開(kāi)發(fā)
?
自此,我們已經(jīng)完成了Spring的IOC控制依賴反轉(zhuǎn)的部分學(xué)習(xí)入門
總結(jié)
以上是生活随笔為你收集整理的IOC操作Bean管理注解方式(完全注解开发)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql5.6 pt-query-di
- 下一篇: android设备报警推送,Fireba