IOC操作Bean管理注解方式(完全注解开发)
生活随笔
收集整理的這篇文章主要介紹了
IOC操作Bean管理注解方式(完全注解开发)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
IOC操作Bean管理注解方式(完全注解開發(fā))
?
(1)創(chuàng)建配置類,替代xml配置文件
需要讓Spring 把一個普通的類認為是配置類
?結構圖:
?
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 {}測試文件:
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();}}測試結果:
?
應用:SpringBoot中做開發(fā)
?
自此,我們已經(jīng)完成了Spring的IOC控制依賴反轉的部分學習入門
總結
以上是生活随笔為你收集整理的IOC操作Bean管理注解方式(完全注解开发)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql5.6 pt-query-di
- 下一篇: Linux的实际操作:用户管理(用户添加