智慧小区网站建设,平面设计网页设计师招聘,做网站优化的协议书,莱芜58同城网Import注解是Spring框架中的注解之一#xff0c;用于导入其他配置类或者组件
Import注解的作用有以下几点#xff1a; 导入其他配置类#xff1a;可以使用Import注解导入其他的配置类#xff0c;将其加入到当前配置类中#xff0c;从而可以共享配置信息 导入其他组件Import注解是Spring框架中的注解之一用于导入其他配置类或者组件
Import注解的作用有以下几点 导入其他配置类可以使用Import注解导入其他的配置类将其加入到当前配置类中从而可以共享配置信息 导入其他组件可以使用Import注解导入其他的组件将其加入到当前的容器中从而可以使用这些组件。 导入自动配置类在Spring Boot中可以使用Import注解导入自动配置类从而启用自动配置。
举例
package com.springboot;import ch.qos.logback.core.db.DBHelper;
import com.springboot.bean.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;import java.util.Arrays;//User为我们创建的一个组件
Import({User.class,DBHelper.class,MyConfig.class})
SpringBootApplication
public class MainApplication {public static void main(String[] args) {ConfigurableApplicationContext run SpringApplication.run(MainApplication.class,args);//通过getBeanNamesForType方法获取组件的类型String s Arrays.toString(run.getBeanNamesForType(User.class));System.out.println(s);//获取第三方jar包提供的自动装配类DBHelperDBHelper dbHelperrun.getBean(DBHelper.class);System.out.println(dbHelper);//获取我们创建的其他配置类---MyConfigMyConfig myConfigrun.getBean(MyConfig.class);System.out.println(myConfig);}
}输出
//user01为我们在MyConfig配置类中添加的组件类型为User,名称为user01
//com.springboot.bean.User为我们创建的组件User
[user01, com.springboot.bean.User]
ch.qos.logback.core.db.DBHelper7da10b5b
com.springboot.MyConfig219f4597