生活随笔
收集整理的這篇文章主要介紹了
@ImportResource()注解的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
@ImportResource注解用于導入Spring的配置文件,讓配置文件里面的內容生效;(就是以前寫的springmvc.xml、applicationContext.xml) Spring Boot里面沒有Spring的配置文件,我們自己編寫的配置文件,也不能自動識別; 想讓Spring的配置文件生效,加載進來;@ImportResource標注在一個配置類上。 注意!這個注解是放在主入口函數的類上,而不是測試類上
不使用@ImportResource()注解,程序根本不能對我們spring的配置文件進行加載,所以我們需要將spring配置文件加載到容器里。
package com. yangzhenxu. firstspringboot ; import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
import org. springframework. context. annotation. ImportResource ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ; @ImportResource ( locations
= "classpath:applicationContext.xml" )
@SpringBootApplication
@RestController
public class FirstSpringbootApplication { public static void main ( String [ ] args
) { SpringApplication . run ( FirstSpringbootApplication . class , args
) ; } }
package com. yangzhenxu. firstspringboot ; import com. yangzhenxu. firstspringboot. bean. Person ;
import javafx. application. Application ;
import org. junit. jupiter. api. Test ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. boot. test. context. SpringBootTest ;
import org. springframework. context. ApplicationContext ; @SpringBootTest
class FirstSpringbootApplicationTests { @Autowired ApplicationContext applicationContext
; @Test void testapplication ( ) { Object a
= applicationContext
. getBean ( "dog1" ) ; System . out
. println ( a
) ; } }
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > < bean id = " dog1" class = " com.yangzhenxu.firstspringboot.bean.Dog" > < property name = " name" value = " zhangxue" /> < property name = " age" value = " 27" /> </ bean> </ beans>
總結
以上是生活随笔 為你收集整理的@ImportResource()注解的使用 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。