一、@ImportResource
spring boot自动装配/自动配置
Spring 扥配置文件 默认会被spring boot自动给配置好。
如果要自己编写spring等配置文件,spring boot能否识别?
当然是可以的。
在resources目录下创建spring.xml文件。
<bean class="com.doublechen.helloworld.service.StudentService"></bean>
在主配置类application.java中:
@ImportResource(locations={"classpath:spring.xml"})
加入注解,标明配置文件就可以使用自己配置的文件了。
但是这种手动配置不推荐使用。我们可以使用创建配置类的形式代替写配置文件。
AppConfig.class:
package com.doublechen.helloworld.conf; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.doublechen.helloworld.service.StudentService; @Configuration public class AppConfig { @Bean public StudentService studentService(){ StudentService studentService = new StudentService(); return studentService;//相当于:<bean class="com.doublechen.helloworld.service.StudentService"></bean> } }
占位符表达式: