定義bean:


純注解開發模式:
用SpringConfig類來代替applicationContext.xml組態檔,利用注解@configuration代表了xml里的基本配置,在SpringConfig類中再加一個注解@ComponentScan("指定包名")替換組態檔的掃描,
加載Spring配置類初始化Spring容器需要更換如下:
//AnnotationConfigApplicationContext加載Spring配置類初始化Spring容器
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);加載配置類

使用@Scope定義bean作用范圍
@Scope("singleton")表示單例
@Scope("prototype")表示非單例
@PostConstruct設定bean的初始化方法
@PostConstruct
@PreDestroy設定bean的銷毀方法
@PreDestroy
@Autowired替換set方法做裝配


使用@Value實作注入
1.簡單型別:@Value("值")
2.如果使用@Value獲取properties組態檔里的值,則需要在SpringConfig類中加個@PropertySource({"組態檔名"}),然后在@Value()輸入${變數名}即@Value(${變數名})
第三方管理bean
新建一個JdbcConfig類存放第三方bean

然后

簡單型別用成員變數

參考型別用方法形參


Spring整合Junit
@RunWith設定專用的類運行器
@ContextConfiguration指定spring的背景關系配置類

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/509392.html
標籤:其他
