@ImportResource和@PropertySource兩個註解的區別

先看第一種方法;@ImportResource

@ImportResource:註解的文件格式和內容應該是xml文件。並且xml內容中必須包含以下信息。
註解:@ImportResource(“classpath:Beans.xml”)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" >
        
    <context:property-placeholder location="classpath:/config.properties"/>
    
</beans>

第二種方法更加的簡單:@PropertySource
只需要加屬性的classpath
如:@PropertySource(“classpath:config.properties”)

application.properties的內容如下

spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true

#jndi數據源名稱
spring.datasource.jndi-name=SpringBootDemoDataSource

#oracle
#spring.datasource.password=password
#spring.datasource.username=sinospntest
#spring.datasource.url=jdbc:oracle:thin:@172.20.223.252:1521:ORCL
#spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

總結:
@PropertySource 用於引入*.Properties或者 .yml 用於給javabean注入值
@ImportResource 用於引入.xml 類型的配置文件 在spring boot中已經被配置類替代
@PropertySource 一般用在javabean的類名上
@ImportResource一般用於啓動類上

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章