Spring入门(七)@ImportResource和@PropertySource

这是spring加载配置项的注解。

先看第一种方法;

@ImportResource:注解的内容应该是xml文件。并且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>

然后config.properties的内容

jdbc.url = jdbc:mysql://localhost:8080/demo
jdbc.username = your username
jdbc.password = your password

注解:@ImportResource("classpath:Beans.xml") 

第二种方法更加的简单:@PropertySource

只需要加属性的classpath

如:@PropertySource("classpath:config.properties")

猜你喜欢

转载自blog.csdn.net/qq_40883132/article/details/81477694