SpringBoot

SpringBoot

SpringBoot可以简化配置,不在写繁琐的xml

Spring注解配置Bean
1.导入Spring的包
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.12.RELEASE</version>
 </dependency>
2.实体类
public class MYBean {
}
3.配置类

@Configuration 告诉Spring这是一个配置类
@Bean 打在方法上,将这个方法返回的对象交给Spring管理

@Configuration//告诉Spring这是一个注解类,相当于以前的applicationContext.xml
public class ApplicationConfig {
    @Bean//将这个方法返回的对象都交给Spring管理
    public MYBean myBean(){
        //将这个bean交给Spring管理
        MYBean bean = new MYBean();
        return bean;
    }
}
4.测试
public class AnnoTest {
    @Test
    public void test()throws Exception{
        //加载配置的类 获取容器对象
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        //通过容器获取bean
        MYBean bean = applicationContext.getBean(MYBean.class);
        System.out.println(bean);
    }
}
@ComponentScan扫描bean
1.实体类

@Component//表示这个类是一个主类

@Component//表示这个类是一个主类
public class MyBean {
}
2.配置类

@ComponentScan(value=“包全限定名”)可以设置扫描包
不设置value就默认扫描当前包及其子包

@Configuration//表示这个是配置类
//@ComponentScan(value="包全限定名")可以设置扫描包
@ComponentScan//spring ioc 开启扫描  扫描当前包及其子包,
public class ApplicationConfig {
}
bean的生命周期以及懒加载配置
1.实体类

生命周期方法名要与配置类中的方法名一致

public class MyBean {
    public void init(){
        System.out.println("创建Bean.....");
    }
    public void destroy(){
        System.out.println("销毁Bean.....");
    }
}
2.配置类
@Configuration//表示这是一个配置类
public class ApplicationConfig {
    /**
     bean的id:  方法名 myBean
     bean的name:   @Bean(name = "myBean2")
     bean的初始方法,销毁方法  :  @Bean(initMethod = "init",destroyMethod = "destory")
     bean的scope:     @Scope("prototype") 作用域
     bean的lazy-init : @Lazy  懒加载
     */
    @Bean(name = "myBean1",initMethod = "init",destroyMethod = "destroy")
//    @Scope("prototype")
    @Lazy
    public MyBean myBean(){
        //将返回的对象交给spring管理
        return new MyBean();
    }
}
3.测试

引入Spirng自己的测试包,否则不能正常执行销毁方法

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.3.12.RELEASE</version>
</dependency>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationConfig.class)
public class configbeanTest {

   /* 可以注入容器对象来获取bean
    @Autowired
    private ApplicationContext applicationContext;
    */

   //也可以直接注入实体类的bean
   @Autowired
   private MyBean myBean;
    @Test
    public void test()throws Exception{
        //加载配置的类
        //AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        //通过容器获取bean
       // MyBean bean = applicationContext.getBean(MyBean.class);
        System.out.println(myBean);
    }
}
依赖注入
1.实体类
public class OtherBean {
}
public class MyBean {

    private String name;
    private OtherBean otherBean;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public OtherBean getOtherBean() {
        return otherBean;
    }

    public void setOtherBean(OtherBean otherBean) {
        this.otherBean = otherBean;
    }
}
2.配置类
1.1通过方法注入
@Configuration
public class ApplicationConfig {
    @Bean
    public OtherBean otherBean(){
        return new OtherBean();
    }
    @Bean
    public MyBean myBean(){

        MyBean myBean = new MyBean();

        //不能用直接myBean.setOtherBean(new OtherBean);的方式,因为只是设置到了MyBean中,没有放进Spring容器

        //调用方法设置
        myBean.setOtherBean(otherBean());
        return myBean;
    }
}
1.2通过传参注入
@Configuration
public class ApplicationConfig {

    @Bean
    public OtherBean otherBean(){
        return new OtherBean();
    }

    @Bean
    public MyBean myBean(OtherBean otherBean){

        MyBean myBean = new MyBean();

        //不能用直接myBean.setOtherBean(new OtherBean);的方式,因为只是设置到了MyBean中,没有放进Spring容器
        
        //传参来设置otherBean
        myBean.setOtherBean(otherBean);

        return myBean;
    }
}
条件标签
1.实体类
public class MyBean {
}
2.自定义条件类

自定义类实现Condition接口,覆写方法,自定义条件

public class WindowsCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {

        //判断是否为windows系统

        String osName = conditionContext.getEnvironment().getProperty("os.name");
        //如果是windows系统就返回true
        if (osName.equals("Windows 10")) {
            return true;
        }
        return false;
    }
}

3.配置类
@Conditional 打在方法上,满足条件才会创建bean

@Configuration
//@Conditional(value = WindowsCondition.class) 也可以打在类上,则这个类里面的方法都要满足该条件才会创建bean
public class ApplicationConfig {

    @Conditional(value = WindowsCondition.class)//如果条件成立才会创建Bean
    @Bean
    public MyBean myBean(){
        return new MyBean();
    }
}
@Import导入

1.实体类

public class MyBean {
}
public class OtherBean {
}

2.配置类
@Import导入其他配置类到主配置类中

@Configuration
@Import(OtherConfig.class)//@Import 将其他配置类导入到主配置类中
public class ApplicationConfig {
    @Bean
    public MyBean myBean(){
        return new MyBean();
    }
}
@Configuration
public class OtherConfig {

    @Bean
    public OtherBean otherBean(){
        return new OtherBean();
    }
}
导入选择器
1.实体类
public class MyBean {
}
public class OtherBean {
}
2.自定义导入选择器

自定义类实现ImportSelector,将实体类的全限定名,放在选择器中创建bean

//自定义类实现ImportSelector
public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {

        return new String[]{"cn.itsource._08import_selector.MyBean","cn.itsource._08import_selector.OtherBean"};
    }
}
3.配置类

@Import导入对应的选择器字节码文件

@Configuration
@Import(value = MyImportSelector.class)
public class ApplicationConfig {
}
导入注册器
1.实体类
public class MyBean {
}
public class OtherBean {
}
2.自定义类实现

自定义类实现ImportBeanDefinitionRegistrar

//导入注册器
public class MyImportRegister implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {

        //将类注册到spring的根容器中
        RootBeanDefinition myBeanDefinition = new RootBeanDefinition(MyBean.class);
        RootBeanDefinition otherBeanDefinition = new RootBeanDefinition(OtherBean.class);

        //将注册的bean交给spring管理
        beanDefinitionRegistry.registerBeanDefinition("myBean", myBeanDefinition);
        beanDefinitionRegistry.registerBeanDefinition("otherBean", otherBeanDefinition);
    }
}
3.配置类

@Import导入对应的自定义注册器字节码文件

@Configuration
@Import(value = MyImportRegister.class)
public class ApplicationConfig {
}

SpringBoot入门

SpringBoot创建项目
1.使用在线工具创建

https://start.spring.io/

2.idea

在这里插入图片描述

3.手动创建java项目
SpringBoot简单的web程序
1.导入依赖
<parent>
        <groupId>org.springframework.boot </groupId>
        <artifactId>spring-boot-starter-parent </artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web </artifactId>
            <!--没有版本号-->
        </dependency>
    </dependencies>
2.配置类
//@SpringBootApplication 包含了下面三个标签 具有它们的功能

/*@Configuration//表示这是一个配置类
@EnableAutoConfiguration//自动配置  里面有一个选择器,会去扫描jar包,加载META-INF/spring.factories文件自动配置类
@ComponentScan//扫描对应的组件 扫描当前包及其子包*/
@SpringBootApplication
public class ApplicationConfig {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfig.class, args);
    }
}
3.Controller层
@RestController//组合标签,包含了@Controller @ResponseBody
public class Example {
	@RequestMapping("/hello")
	String home() {
		return "Hello World!!!";
	}
}

通过http://localhost:8080/hello访问

parent - spring-boot-starter-parent 的作用?
  • SpringBoot的父工程,用来对jar包进行管理

  • <dependencies> : 导入依赖,如果一个父工程使用 dependencies导入依赖 , 那么这个dependencies里面的所有依赖都会被子模块直接继承使用 ,也就是放所有子模块公共的jar包 ,test包 等。

  • <dependencyManagement>: 如果一个父工程使用 dependencyManagement 导入依赖 , 那么这个dependencyManagement里面的所有依赖,是不能直接被子模块使用的。如果子项目要使用 dependencyManagement里面的jar包 ,就需要在子模块的< dependencies >去导入jar包,但是,版本号不用写,使用父工程的版本号;用来管理版本号 , 除了所有子模块都能用到的包以外的其他包放到 dependencyManagement

spring-boot-starter-web 包的作用?
  • 用来整合web层的一个依赖(SpringMvc,日志,tomcat,json,自动配置包)等等包;
  • starter机制就是会把后面内容相关的依赖整合在一起(eg:如果是web,就会整合Spring-mvc相关的东西);
@RestController的作用?
  • 是一个组合标签,包括了@Controller和@ResponseBody
@EnableAutoConfiguration的作用?
  • EnableAutoConfiguration中有一个选择器AutoConfigurationImportSelector;它会去扫描classpath的jar包,加载META-INF/spring.factories文件来自动配置类
SpringApplication.run的作用?
  • 启动SpringBoot应用,
  • 准备应用数据和环境,
  • 创建加载容器,
  • 加载注册bean(初始化bean)
  • 解析配置类,
  • 做自动配置,
  • 项目打包到内嵌的tomcat,
  • 运行项目
Tomcat 是哪儿来的?
  • SpringBoot内嵌tomcat , 就是通过 spring-boot-starter-web 包导入了tomcat
项目结构(jar)
  • SpringBoot默认的项目打包方式就是 jar
前端控制器,视图解析器等等相关配置为什么没有手动配?
  • 自动配置已经完成了配置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值