springboot基础

1.什么是springboot

springBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品:

2.什么是spring注解式编程

Spring两大核心:基于工厂模式IOC(DI)和基于动态代理AOP。
其中IOC(DI)是指控制器反转(依赖注入),原来要使用某个类对象实例是必须自己创建,使用spring后就不需要自己创建,由spring创建,需要时直接从spring中获取并且有依赖关系是会spring会通过反射自动注入。
AOP就是不影响正常执行过程的前后加入额外的逻辑。比如权限,日志等,该执行的业务逻辑正常执行知识可以进行权限的判断核日志记录。

3.spring的xml配置实现IOC(依赖注入)

3.1创建配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <bean id="myBean" class="cn.itsource.spring_boot._01xmlIoc.MyBean"></bean>

</beans>

3.2创建一个MyBean的类

public class MyBean {
}

3.3创建测试类

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class XmlIocTest {
    @Test
     public void testXmlIoc()throws Exception{
        //将bean文件放到applicationContext容器中
        ClassPathXmlApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器中获取到bean
        MyBean bean = applicationContext.getBean(MyBean.class);
        System.out.println(bean);
    }
}

4.springboot是实现IOC(依赖注入)

使用springboot就不需要再配置xml了,直接使用注解的方式创建注解类代替以前的xml.

4.1使用注解

1.创建MyBean类
2.创建注解类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Configuration   相当于添加spring注解,表示此类为注解类,相当于applicationContext.xml
 */
@Configuration
public class ApplicationConfig {
    /**
     * @Bean   表示spring的bean定义标签,放在方法上,表示bean返回的实例交给spring容器管理
     * 相当于 <bean id ="mybean"
     * @return
     */
    @Bean
    public MyBean myBean(){
        MyBean myBean = new MyBean();
        return myBean;
    }
}

3.创建测试类

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AnnoIocTest {
     @Test
      public void testAnnoIoc()throws Exception{
         //将bean文件放到applicationContext容器中
         AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
         //从容器中获取bean
         MyBean bean = applicationContext.getBean(MyBean.class);
         System.out.println(bean);
     }
}

4.2注解扫描

1.创建MyBean

/**
 * 使用注解扫描
 */
@Component
public class MyBean {
}

2.创建注解类

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 添加注解类
 * @ComponentScan  IOC组件自动扫描的注解   可以跟basePackages,扫描具体的包
 */
@Configuration
@ComponentScan
public class ApplicationConfig {
}

3.创建测试类

public class ScanIocTest {
     @Test
      public void testScanIoc()throws Exception{
         //将bean文件放到applicationContext容器中
         AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
         //从容器中获取bean
         MyBean bean = applicationContext.getBean(MyBean.class);
         System.out.println(bean);
     }
}

4.3配置注解类的一些id、name、lazy、scop等

1.创建MyBean类

public class MyBean {
    private void init() {
        System.out.println("我被创建了!!!");
    }

    private void destroy() {
        System.out.println("我被销毁了!!!");
    }
}

2.创建注解类

	 *  bean的id : 就是方法的名字
     *  bean的name: 可通过@Bean.name属性指定
     *  lazy-init :通过标签指定 @Lazy 标签指定
     *  bean的scope : @Scope("prototype") 标签指定
@Configuration
public class ApplicationConfig {
    @Bean(name = "myBean2",initMethod = "init",destroyMethod = "destroy")
    //懒加载
    //@Lazy
    //@Scope("prototype")  指定标签
    public MyBean myBean(){
        MyBean myBean = new MyBean();
        return myBean;
    }
}

3.创建测试类

/**
 * spring扫描注入实例对象
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationConfig.class)
public class BeanInfoTest {
    /**
     * 依赖注入
     */
    @Autowired
    private MyBean myBean;
     @Test
      public void testBeanInfo()throws Exception{
         System.out.println(myBean);
      }
}

4.4注解类中对象的引入

1.创建MyBean

public class MyBean {
    private String username;
    private OtherBean otherBean;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public OtherBean getOtherBean() {
        return otherBean;
    }

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

2.创建一个otherBean
3.创建注解类

@Configuration
public class ApplicationConfig {
    //定义MyBean
    @Bean
    public MyBean myBean(OtherBean otherBean){
        MyBean myBean = new MyBean();
        myBean.setUsername("打飞机!!!");
        System.out.println(myBean.getUsername());
        //传参
        myBean.setOtherBean(otherBean);
        //调方法
        //myBean.setOtherBean(otherBean());
        //自动注入  ?
        return myBean;
    }
    //定义otherBean
    @Bean
    public OtherBean otherBean(){
        OtherBean otherBean = new OtherBean();
        return otherBean;
    }
}

4.创建测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationConfig.class)
public class MyBeanTest {
    @Autowired
    private MyBean myBean;
    @Autowired
    private OtherBean otherBean;
     @Test
      public void test()throws Exception{
         System.out.println(myBean);
         System.out.println(otherBean);
         System.out.println(myBean.getOtherBean());
      }
}

4.5根据不同的操作系统生成不同的bean

1.创建MyBean

public class MyBean {
    private String system;

    public String getSystem() {
        return system;
    }

    public void setSystem(String system) {
        this.system = system;
    }
}

2.创建注解类

/**
 * @Conditional   根据不同的操作系统选车不同的bean
 */
@Configuration
public class ApplicationContext {
    @Bean
    @Conditional(value = WindowBeanConditional.class)
    public MyBean windowsBean(){
        MyBean myBean = new MyBean();
        myBean.setSystem("windows");
        return myBean;
    }
    @Bean
    @Conditional(value = LinuxBeanConditional.class)
    public MyBean linuxBean(){
        MyBean myBean = new MyBean();
        myBean.setSystem("linux");
        return myBean;
    }
}

3.创建相应的实现类实现condition
windows相关的实现类

public class WindowBeanConditional implements Condition {

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
       //获取操作系统的名字
        Environment environment = conditionContext.getEnvironment();
        String systemName = environment.getProperty("os.name");
        if (systemName.toUpperCase().contains("WINDOWS")){
            return true;
        }
        return false;
    }
}

linuxxaing相关的实现类

/**
 * 根据系统环境创建bean
 */
public class LinuxBeanConditional implements Condition {

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
       //获取操作系统的名字
        Environment environment = conditionContext.getEnvironment();
        String systemName = environment.getProperty("os.name");
        if (systemName.contains("linux")){
            return true;
        }
        return false;
    }
}

4.创建测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationContext.class)
public class ConditionTest {
    @Autowired
    private MyBean myBean;
     @Test
      public void testConditionTest()throws Exception{
         System.out.println(myBean.getSystem());
      }
}

4.6一个注解类中引入其他的注解类(import)

1.创建MyBean和OtherBean
MyBean

public class MyBean {
    private OtherBean otherBean;

    public OtherBean getOtherBean() {
        return otherBean;
    }

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

OtherBean

public class OtherBean {
}

2.创建注解类
MyBean的注解类

**
 * import 可以导入其他配置类,配置类之间的导入
 */
@Configuration
@Import(ApplicationOtherConfig.class)
public class ApplicationConfig {
    @Autowired
    private OtherBean otherBean;
    @Bean
    public MyBean myBean(){
        MyBean myBean = new MyBean();
        myBean.setOtherBean(otherBean);
        return myBean;
    }
}

OtherBean的注解类

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

3.创建测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationConfig.class)
public class ImportTest {
    @Autowired
    private MyBean myBean;
    @Autowired
    private OtherBean otherBean;
    
     @Test
      public void testImport()throws Exception{
         System.out.println(myBean);
         System.out.println(otherBean);
         System.out.println(myBean.getOtherBean());
         System.out.println(demoBean);
      }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值