配置文件的作用

Spring配置文件作用就是用来配置对象的,在Spring启动的时候会把这些对象加载到IOC容器里,配置对象就是配置对象的属性值

配置属性值就两种方法

  • 构造函数初始化
  • setter函数设置

两种方式配置的时候可以显示配置(ref)也可以隐式配置(autowire)

Spring配置文件的类型

通过XML进行配置

分两部,写xml文件,启动代码里引用这个配置文件

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="jackma" class="com.tyq.dto.User">
        <property name="name" value="jackma" />
        <property name="age" value="55" />
        <property name="dog" ref="jm" />
     </bean>

    <bean id="jm" class="com.tyq.dto.Dog">
        <property name="name" value="jack" />
        <property name="breed" value="金毛" />
        <property name="age" value="2" />
    </bean>
    
    <!--构造函数的自动装配-->
    <!-- Definition for textEditor bean -->
    <bean id="textEditor" class="com.tutorialspoint.TextEditor">
      <constructor-arg  ref="spellChecker" />
      <constructor-arg  value="Generic Text Editor"/>
    </bean>

    <!-- Definition for spellChecker bean -->
    <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
    </bean>
    
     <!--setter方法的自动装配-->
     <!-- ref装配 -->
     <bean id="textEditor" class="com.tutorialspoint.TextEditor">
       <property name="spellChecker" ref="spellChecker" />
       <property name="name" value="Generic Text Editor" />
     </bean>

     <!-- byName的自动装配,省去了ref -->
     <bean id="textEditor" class="com.tutorialspoint.TextEditor" 
      autowire="byName">
      <property name="name" value="Generic Text Editor" />
     </bean>
    
     <!-- byType的自动装配,省去了ref -->
     <bean id="textEditor" class="com.tutorialspoint.TextEditor" 
      autowire="byType">
      <property name="name" value="Generic Text Editor" />
     </bean>

    <!-- bean用来被装配奥 -->
    <bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
    </bean>


</beans>

启动程序

public class test {
    public static void main(String args[]){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) context.getBean("jackma");
        System.out.println(user);
    }
}

springboot里要强行使用xml配置文件可以使用@ImportResource引入,当然不推荐了,就是说一下无用的知识

通过XML开启注解配置,使用@Component注解配置bean

分三步:配置文件开启注解使用注解启动项目

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

   <context:annotation-config/>
   <!-- bean definitions go here -->

</beans>

使用注解

注解分为两类:

  • 定义bean的注解
  • @Component(@Repository、@Service、@Controller)
  • 对bean进行配置的注解
  • @autowire 作用范围:字段、setter方法、构造器,spring自带的注解
  • 默认按照类型(by-type)装配,默认情况下要求依赖对象必须存在。
  • 如果允许依赖对象为null,需设置required属性为false,即@Autowire(required=false)
  • 如果使用按照名称(by-name)装配,需结合@Qualifier注解使用,即
  • @Resource 作用范围:字段、setter方法, 是jdk注释,jdk注释还有很多别的这里只讲和属性配置相关的
  • 默认按照名称(by-name)装配,名称可以通过name属性指定。
  • 当注解在字段上时,默认取name=字段名称装配。
  • 当注解在setter方法上时,默认取name=属性名称装配。
  • 当按照名称(by-name)装配未匹配时,按照类型(by-type)装配。

启动项目

public class test {
    public static void main(String args[]){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) context.getBean("jackma");
        System.out.println(user);
    }
}

使用Java注解进行配置

使用java注解进行配置,就是完全替换掉了xml文件,所以配置都通过代码形式进行配置。这种配置方式兼容了第二种注解配置方式,也就是说有两套配置方式并存

  • 通过@Configuration 和 @Bean已经@Import注解来配置——属于java注解
  • 通过@ComponentScan(s) 和@Component(@Repository、@Service、@Controller) 、@autowire配合来完成bean注册——属于spring注解

配置文件 ConfigB.class

@ComponentScans(value = 
        {@ComponentScan(value = "io.mieux.controller"),
        @ComponentScan(value = "io.mieux.service")})
@Configuration
public class BeanConfig {
   @Bean 
   public HelloWorld helloWorld(){
      return new HelloWorld();
   }
}

启动项目

public static void main(String[] args) {
   ApplicationContext ctx = 
   new AnnotationConfigApplicationContext(ConfigB.class);
   // now both beans A and B will be available...
   A a = ctx.getBean(A.class);
   B b = ctx.getBean(B.class);
}

扩展,完全走向注解配置了,初始值怎么赋呢(yaml和properities配置文件登场)

通过标题也就知道了,springboot里或者spring里的配置文件因为注解的原因,现在都从原来要配置bean和初始化参数升级到了只需要配置初始化参数的程度了

@Value注解 参考链接

  • @Value(“常量”) 常量,包括字符串,网址,文件路径等
  • @Value(“${}” : default_value) 读取配置文件
  • @Value(“#{}”? : default_value) 读取注入bean的属性

@ImportResource

  • 需要导入依赖
  • 配置在类顶部和@Component同级别@ConfigurationProperties(prefix=“user”)(指定前缀,会自动为这个类匹配所有前缀值)

@PropertiySource注解

  • @PropertySource({“classpath:user.properties”}),来完成外部文件的导入,需要配合@ConfigurationProperties使用。

对于引用初始变量文件的一些注意事项

@value和@ConfigurationPropertie的区别 @ConfigurationProperties:是和JavaBean的所有属性绑定;@Value是一个一个属性绑定。
@ConfigurationProperties:不支持spring表达式的写法;@Value支持spring的表达式的写法 ,#{12+13}
@ConfigurationProperties:支持复杂类型的绑定;@Value不支持复杂类型的绑定。

配置文件格式yaml和properites区别

  • yaml格式严格,也丰富简洁,空格缩进,:赋值,支持中文注释
  • proeritiese文件格式单一:只支持键值对,=赋值,中文会乱码
  • 如果你同时有properties和yaml,yaml会先执行,properties里面的属性将覆盖yaml的。
  • 推荐使用yaml
配置文件优先级

spring指定配置文件 spring配置文件的作用是什么_spring指定配置文件

–file:./config/     文件路径config目录—>最高优先级
–file:./       文件路径根目录—>其次
–classpath:/config/ 类路径config目录—>再其次
–classpath:/     类路径根目录—>最低优先级