spring 注解加载properties文件 纯注解驱动

19 篇文章 0 订阅
13 篇文章 0 订阅

spring 注解加载properties文件

名称: @PropertySource
类型:类注解
位置:类定义上方
作用:加载properties文件中的属性值

范例:
@propertysource (value = “glasapath: filename.propertiea”)
publid class classname{
@value ("${propertiesAttributelamel }")
private suring attributeName;

说明:
◆不支持通a格式,一旦加载,所有sprina控制的bean中均可使用对应属值

相关属性 :
value (默认) :设置加载的properties文件名
ignoreResourceNotFound:如果资源未找到,是否忽略,默认为false

jdbc.properties文件

jdbc.userName=root
jdbc.password=itheima

dao层定义的变量名

  @Value("${jdbc.userName}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;

用@PropertySource解析文件的

@Component
@Primary
@PropertySource(value={"classpath:jdbc.properties","classpath:abc.properties"},ignoreResourceNotFound = true)
public class BookDaoImpl implements BookDao {
    @Value("${jdbc.userName}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;


    public void save() {
        System.out.println("book dao running...1 "+userName+" "+password);
    }
}

value={“classpath:jdbc.properties”,“classpath:abc.properties”
这是用来获取文件的,通过classpath:jdbc.properties得到指定文件名称的文件,可以在value的大括号内加载多个properties文件

ignoreResourceNotFound = true 这个的作用是当加载了不存在的properties文件,就将这个文件的classpath路径忽略掉,使系统不在找不到文件时报错。设置为true就是在使ignoreResourceNotFound 的功能生效,为false或者不写ignoreResourceNotFound 就使将它关闭

纯注解驱动

纯注解驱动:就是使注解完全取代spring配置文件applicationContext.xml文件的作用


纯注解格式,

名称: @Confiquration, @Componentscan
类型:类注解
位置:类定义上方
作用:设置当前类为spring核心配置加载类

范例:
@configuration
@Comnonentscan (“scanPadkageName”)
public class springconfiqclassName

说明:
核心配合类用于替换spring核心配置文件,
此类可以设置空的,
不设置变量与属性
bean扫描工作使用注解@ComponentScan替代

但spring的配置文件还要留下

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--启动注解驱动,指定对应扫描的路径,也就是资源所在的包-->
    <context:component-scan base-package="com.itheima"/>

    <!--<bean id="userService" class="com.itheima.service.impl.UserServiceImpl" />-->

    <!--<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" />-->

    <!--<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl" />-->

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!-- ..... -->
    </bean>
</beans>

这就要写一个配置类来配置对应的spring的配置文件

@Configuration
@Import({JDBCConfig.class})
@ComponentScan("com.itheima")
@Order(2)
public class SpringConfig {

    @Bean("b1")
    public String getB1(){
        System.out.println("b1");
        return "";
    }}

@Configuration 就代表整个spring配置文件

@ComponentScan(“com.itheima”)代表注解驱动
这里的com.itheima就是要使用驱动的路径
它对应的就是配置文件的这句话context:component-scan base-package=“com.itheima”
@ComponentScan的参数(“com.itheima”)就是base-package=“com.itheima”

在应用程序也要发生变化

ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class, SpringConfig2.class);

创建的是AnnotationConfigApplicationContext对象
SpringConfig.class, SpringConfig2.class参数是spring配置类的类字节码类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

普希托夫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值