一、@import的使用
@Import 可以把某个Bean导入到Spring 框架里。
@Import({UserService.class})
在SpringBoot的入口加上@Import的注解,将需要使用的类导入进去(不需在service层加上@Component,@Service,@Controller的注解了)
@SpringBootApplication
@Import({UserService.class})
public class Demo1Application {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Demo1Application.class);
UserService userService =applicationContext.getBean(UserService.class);
userService.talk();
缺点:多个类的注入非常不便捷,有变动时也不好管理
解决:定义一个config类来管理
二、@Import导入自定义的Config类
写一个configuration
public class MyAutoConfigration {
@Bean
public UserService userService(){
return new UserService();
}
}
然后在入口导入
@Import({MyAutoConfigration.class})
----------------------------------------------------------------------------------------------------------------------
@Import 不仅可以直接导入目标Bean,也能导入配置类并把配置类的Bean也导入到Spring 框架
----------------------------------------------------------------------------------------------------------------------
三、自定义@EnableXXX
enable就是开启什么的意思
创建一个MyEnableAutoConfiguration 通过@import导入自定义的Config类
//这里要加上这两个注解,否则MyEnableAutoConfiguration注解不会生效,虽然@Import注解本身也有这两个注解。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(MyAutoConfiguration.class)
public @interface MyEnableAutoCofiguration {
}
然后直接在入口处加上注解
@MyEnableAutoCofiguration
缺点:在自定义的MyEnableAutoConfiguration里需要导入多个config类,维护麻烦。
解决:通过配置的方式来应变
四、接口ImportSelector
*在SpringBoot的自动化配置和@EnableXXX(功能性注解)都有ImportSelector接口的存在
*接口其主要作用是收集需要导入的配置类。
创建一个ImportSelector的实现类
public class MyAutoConfigurationSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
String[] className = new String[]{"com.wx.demo2.configuration.MyAutoConfiguration"};
//返回之前写的MyAutoConfigration类的全类名
return className;
}
}
修改MyEnableAutoConfiguration
@Import(MyAutoConfigurationSelector.class)
缺点:能在Selector的selectImports方法里维护多个class,但还是没有达到通过配置文件的方式来维护。
五、通过配置文件类维护需要导入的class
在resources里创建META-INF新建一个spring.factories,将config的全路径存入
EnableAutoConfiguration=com.wx.demo2.configuration.MyAutoConfiguration
修改MyAutoConfigurationSelector,采用流的方式来读取配置文件
public class MyAutoConfigurationSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
String [] classNames = null;
try {
// 获取资源输入流
InputStream inputStream = ClassLoader.getSystemResourceAsStream("META-INF/spring.factories");
// 获取文件里的key,value数据
Properties properties = new Properties();
properties.load(inputStream);
// 获取配置文件里的自动配置需要加载的类
String value = properties.getProperty("EnableAutoConfiguration");
// 通过逗号取出所有的类
classNames = value.split(",");
} catch (IOException e) {
e.printStackTrace();
}
return classNames;
}
}
---------------------------------------------------------------------------------------------------------------------@Controller和@ResponseBody(加在方法/类上面)一起使用,和@RestController的作用相同。
1.@Controller类中的方法可以直接通过返回String跳转到jsp、ftl、html等模版页面。
2.在方法上加@ResponseBody注解,也可以返回实体对象。
3.@RestController类中的所有方法只能返回String、Object、Json等实体对象,不能跳转到模版页面。
@resource可替换@Autowired和@Qulifier按照名称注入
------------------------------------------------------
在springboot中配置跨域时,出现
When allowCredentials is true, allowedOrigins cannot contain the special value “*” since that cannot be set on the “Access-Control-Allow-Origin” response header. To allow credentials to a set of origins, list them explicitly or consider using “allowedOriginPatterns” instead.
浏览器显示500服务器报错
需要将配置类中的corsConfiguration.addAllowedOrigin("*");修改成为corsConfiguration.addAllowedOriginPattern("*");