本文共 5194 字,大约阅读时间需要 17 分钟。
Spring Boot是其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
1.1xml形式
public class Myben(){ }
1.2 通过注解方式
/** * Spring的配置类 相当于是:applicationContext.xml * @Configuration :Spring的配置标签,标记改类是Spring的配置类 */@Configurationpublic class ApplicationConfig { /** * @Bean : Spring的bean的定义标签 ,标记方法返回的对象交给Spring容器管理 */ @Bean public MyBean myBean(){ return new MyBean(); }}
1.3ComponentScan&ComponentScans自动扫描
@Componentpublic class MyBean { }
/** * * @ComponentScan会扫描 @Component @Controller @Service 注解的实体类 value默认不写, * 表示扫描本包下面的所有有该类注解的实体类 * */@Configuration@ComponentScan(value = { "cn.itsource._03_bean_componentscan"}, //排除资源 excludeFilters = { @ComponentScan.Filter( //通过注解排除 type = FilterType.ANNOTATION , //排除有@Controller注解的类 value = Controller.class ) })public class ApplicationConfig { }
@Bean(name="" , initMethod ="" ,destroyMethod="" ) //@Scope("prototype") //@Lazy public MyBean myBean(){ return new MyBean(); }
/** * @Bean : Spring的bean的定义标签 ,标记方法返回的对象交给Spring容器管理 * bean的id: 方法名 myBean * bean的name:可以通过 @Bean标签的name属性指定 * 生命周期方法:initMethod , destroyMethod * 单利多利: @Scope("prototype") */ @Bean(initMethod = "init" , destroyMethod = "destroy") public MyBean myBean(OtherBean otherBean){ MyBean myBean = new MyBean(); myBean.setName("猪儿虫"); //myBean.setOtherBean(otherBean()); myBean.setOtherBean(otherBean); return myBean ; } @Bean //@Lazy public OtherBean otherBean(){ return new OtherBean(); }
Conditional注解帖在bean的定义方法上来判断,如果不满足条件就不会定义bean
1.在Bean的定义方法帖@Conditional
@Bean @Conditional(value = MyCondition.class) public MyBean windowsMyBean(){ return new MyBean("windowMyBean"); } @Bean @Conditional(value = LinuxCondition.class) public MyBean linuxMyBean(){ return new MyBean("linuxMyBean"); }
public class MyCondition implements Condition { /** * 匹配方法,返回值决定是否满足条件 */ public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { //获取系统环境 String systemName = context.getEnvironment().getProperty("os.name"); if("Windows 10".equals(systemName)){ return true; } return false; }}
1.直接导入Bean或者配置类
@Configuration@Import(ApplicationOtherConfig.class) //导入其他的配置类public class ApplicationConfig
2.导入ImportSelector
public class MyImportSelector implements ImportSelector { //选择导入,该方法返回我们需要导入的类的全限定名 public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{ "cn.itsource._07_import_selector.MyBean", "cn.itsource._07_import_selector.OtherBean"}; }}
@Configuration@Import(MyImportSelector.class) //导入选择器public class ApplicationConfig
1.定义FactoryBean
public class MyBeanFactoryBean implements FactoryBean{ public MyBean getObject() throws Exception { return new MyBean(); } public Class getObjectType() { return MyBean.class; } public boolean isSingleton() { return true; }}
2.配置 MyBeanFactoryBean的bean定义
@Configurationpublic class ApplicationConfig { @Bean public MyBeanFactoryBean myBean(){ return new MyBeanFactoryBean(); }}
3.测试
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes ={ ApplicationConfig.class})public class AnnoTest { @Autowired private MyBean myBean; @Test public void test(){ System.out.println(myBean);
1.pom文件
... org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE org.springframework.boot spring-boot-starter-web
2.启动类
@SpringBootApplicationpublic class ApplicationConfig { public static void main(String[] args) { SpringApplication.run(ApplicationConfig.class); }}
3.编写Controller
@Controllerpublic class Example { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; }}
spring-boot-starter-parent :SpringBoot的父工程,帮我们管理了很多的基础jar包
spring-boot-starter-web :SpringBoot和SpringMvc整合的jar包,并且导入了日志,tomcat,等等相关的jar包
RestController : Controller+ResponseBody
@EnableAutoConfiguration : 开启自动配置功能
SpringApplication.run : 启动SpringBoot应用
jar :SpringBoot应用默认打jar包
SpringBootApplication:包括三个标签组成
@SpringBootConfiguration - @Configuration : Spring的配置标签
@EnableAutoConfiguration :开启自动配置
@ComponentScan :组件自动扫描
dependencyManagement> 该标签下的jar包,默认是不能被子项目直接使用的 , 他只有声明的功能 , 如果只项目想用这里标签里面的jar包 ,需要显示的写出来 ,而版本号使用父工程的。达到版本号统一管理的效果dependencies> 这个标签下面的jar包默认会被子项目直接继承直接使用
转载地址:http://vwfdi.baihongyu.com/