SpringBoot入门(1)

1 SpringBoot 的基本概念

Spring Boot是其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

1.1 Spring 三大特性回顾

  1. IOC 控制反转

以前我们需要对象是通过 new ,现在我们把bean交给Spring管理,不再需要自己来new ,这就是控制反转

  1. DI 依赖注入

有Spring容器将需要的bean 和与它有依赖关系的bean注入到我们需要的类中,交给我们来使用

  1. AOP 面向切面编程

Spring 中涉及到事务时,代码难以抽取而且冗余,通过切面的方式可以实现同时给不同的对象配置好事务

1.2 配置类,Bean注解

@Configuration() 注解吧 用来定义配置类 相当于是:applicationContext.xml
@Bean注解用来定义bean,将返回的bean交给Spring容器来管理

@Configuration
public class MyConfig {
    @Bean
    public MyBean creatBean(){
        return new MyBean();
    }
}

1.3 @ComponentScan扫描bean

@ComponentScan() 后面的括号填写需要扫描的包,括号不写内容默认扫描本包及子包 它会自动去找到这些包中的组件如
@Component @Service @Controller

@Component
public class MyBean {
}

@Configuration
@ComponentScan("cn.itsource._02scan")
public class MyConfig {

}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyConfig.class)
public class TestBean {
   @Autowired
   private MyBean myBean;
   @Test
   public void test1(){

      System.out.println(myBean);
		//这里注入成功
   };
}

1.4 @Scope 定义单例或多例&@Lazy 懒加载

@Scope(“prototype”)多例 @Scope(“singleton”)单例
生命周期方法:initMethod=‘方法名’ , destroyMethod=‘方法名’

@Component
public class MyBean {
    @Autowired
    private OtherBean otherBean;

    public MyBean() {
    }

    public MyBean(OtherBean otherBean) {
        this.otherBean = otherBean;
    }

    public OtherBean getOtherBean() {
        return otherBean;
    }

    public void setOtherBean(OtherBean otherBean) {
        this.otherBean = otherBean;
    }
    public void init(){
        System.out.println("正在初始化");
    }
    public void destroy(){
        System.out.println("正在销毁中");
    }
}
----------------------配置类---------------------------
@Configuration
public class MyConfig {

    @Bean(initMethod = "init",destroyMethod = "destroy")
    public MyBean creatBean(){
        MyBean myBean = new MyBean();
        myBean.setOtherBean(creatOtherBean());
        return  myBean;
    }
   @Bean
    public OtherBean creatOtherBean(){
        return new OtherBean();
   }
}

1.5 @Conditional-按照条件注册

@Conditional (value=’’)在一个类或方法上配置,Value = true 才会执行下面的注入

@Bean
    @Conditional(value = MyCondition.class)
    public MyBean windowsMyBean(){
        return new MyBean("windowMyBean");
    }
    @Bean
    @Conditional(value = LinuxCondition.class)
    public MyBean linuxMyBean(){
        return new MyBean("linuxMyBean");
    }

1.6 有依赖关系的bean的注入方式

  1. 可以通过在配置类中调用另一个bean的创建方法
public class MyConfig {
    @Bean
    public MyBean creatBean(){
        return new MyBean(otherBean());
    }
    @Bean
    public MyBean creatBean(){
        return new MyBean(otherBean());
    }
    @Bean
    public OtherBean  otherBean(){
        return new OtherBean();
    }
}
  1. 通过构造器传参的方式注入依赖对象
public class MyConfig {
   @Bean
    public MyBean creatBean(OtherBean otherBean){
        return new MyBean(otherBean);
    }
    @Bean
    public MyBean creatBean(){
        return new MyBean(otherBean());
    }
    @Bean
    public OtherBean  otherBean(){
        return new OtherBean();
    }
}
  1. 直接注入在到类中
@Component
public class MyBean {

    @Autowired
    private OtherBean otherBean;



    public MyBean(OtherBean otherBean) {
        this.otherBean = otherBean;
    }

    public MyBean() {
    }

    public OtherBean getOtherBean() {
        return otherBean;
    }

    public void setOtherBean(OtherBean otherBean) {
        this.otherBean = otherBean;
    }
}

1.7 Import 导入Bean或者导入配置类

  1. 当有多个配置类时,通过import来导入需要的配置类
@Configuration
//import 主要用来引入其他的配置类   相当于<import:/>
@Import(OtherConfig.class)
public class MyConfig {
}
  1. 导入Selector选择器
public class MyImportSelector implements ImportSelector {
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[]{"cn.itsource._07import_selector.OtherBean",
                            "cn.itsource._07import_selector.MyBean"
                    };
    }
}
--------------------------------------------------------------------------------
@Configuration
//导入选择器  ImportSelector
@Import(MyImportSelector.class)
public class MyConfig {
   

}
  1. 导入Bean的注册器 Registor
public class MyRegistor implements ImportBeanDefinitionRegistrar {
    //BeanDefinitionRegistry :bean的注册器
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(MyBean.class);
        //注册bean 要走这里
        System.out.println("过客。。。");
        beanDefinitionRegistry.registerBeanDefinition("myBean", rootBeanDefinition);
    }
}
-------------------------------------------------------------------------------------
@Configuration
//导入Bean 注册器
@Import(MyRegistor.class)
public class MyConfig {

}

1.8 使用FactoryBean来注册组件

public class MyFactoryBean implements FactoryBean {
    public Object getObject() throws Exception {

        return new MyBean();
    }

    public Class<?> getObjectType() {
        return MyBean.class;
    }

    public boolean isSingleton() {
        return true;
    }
}

1.9 bean继承这两个接口 InitializingBean, DisposableBeanspo 获得生命周期的方法

public class MyBean implements InitializingBean, DisposableBean {
    public MyBean() {
        System.out.println("创建bean时调用");
    }

    public void destroy() throws Exception {
        System.out.println("销毁bean时执行");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化后执行");
    }
}

1.9.1 @PostConstruct与@PreDestroy注解定义初始化和销毁bean执行的方法

@Component
public class MyBean {
    //同过注解定义初始化执行的勾子方法和销毁时执行的方法
    @PostConstruct
    public void init(){
        System.out.println("在创建bean前执行的方法");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("一边销毁一边执行");
    }
}

1.9.2 BeanPostProcessor

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String s) throws BeansException {
        System.out.println("在加载bean前打印");
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String s) throws BeansException {
        System.out.println("在加载bean后面打印");
        return bean;
    }
}

2.0 SpringBoot 入门程序 hello world

  1. 创建maven项目
  2. 导入下面的依赖
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
  </parent>
...
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  1. 创建配置类
@SpringBootApplication
public class ApplicationConfig {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfig.class);
    }
}
  1. 编写Controller
@Controller
public class Example {

	@RequestMapping("/")
	@ResponseBody
	String home() {
		return "Hello World!";
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值