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配置类的类字节码类型