javaConfig&springBoot入门

1. javaConfig基础

1.1 为什么要学习javaConfig

因为:Springboot原理基于它的!!!(为学习springBoot打下基础)

1.2 Java 的 bean 配置(JavaConfig)出现历史

spring1.x:xml配置

spring2.x:注解配置(打注解,扫描注解)

spring3.x-4.x javaconfig&springboot

Spring5.x

2. JavaConfig操作

2.1 spring测试方式

方式一:new ClassPathXmlApplicationContext

/**
* 方式一:直接new
* @throws Exception
*/
@Test
public void test1() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
String[] beanDefinitionNames = context.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName);
}
}

直接new

方式二:注入:Runwith ContextConfigration

/**
* 方式二:注解方式 springtest注入
* @author Lenovo
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-xml.xml")
public class SpringTestTest {
@Autowired
private ApplicationContext applicationContext; @Test
public void test2() throws Exception {
for (String beanDefinitionName : applicationContext.getBeanDefinitionNames()) {
System.out.println(beanDefinitionName);
}
}
}

注解方式

2.2 xml配置

<bean id="myDate" class="java.util.Date">

2.3 注解配置

1. 打注解

2. 扫描包
<context:component-scan base-package=”包的全限定名”/>

2.4 javaconfig配置

基本:

配置类:@Configration 代替了xml配置文件

@Bean 代替了<bean>

/**
* @Configuration 加上此注解就相当于一个spring配置文件(applicationContext.xml)
* @author Lenovo
*/
@Configuration
public class IocConfig {
/**
* @Bean 就相当于创建了一个bean
* 等同于 <bean id="myBean" class="....MyBean"/>
* @return
*/
@Bean
public MyBean myBean() {
return new MyBean();
}
}

基本的配置类

扫描包:

bean扫描(@ComponentScan/ComponentScans)

/**
* 注解方式配置bean
* 1.打注解
* 2.扫描包
*
* @author Lenovo
*/
@Configuration
/**扫描父类包
@ComponentScan("cn.itsource._05componentScan")
*/ /**扫描多个包的方式
@ComponentScans(value = {
@ComponentScan("cn.itsource._05componentScan.controller"),
@ComponentScan("cn.itsource._05componentScan.service")
})*/ //排除指定注解或者包含指定注解的bean
@ComponentScans(value = {
/*@ComponentScan(value = "cn.itsource._05componentScan", excludeFilters = {
//排除指定的注解
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
})*/
//包含指定的注解
@ComponentScan(value = "cn.itsource._05componentScan", includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Service.class})
}, useDefaultFilters = false )//关闭默认全部扫描includeFilters才生效)
})
public class IocConfig { }

ComponentScan扫描包

Bean详情:

//给value给bean取名如果没有设置默认就是方法名
@Bean(value = "myBean")
//singleton:单例 prototype:多例
@Scope(value = "singleton")
//懒加载(只对单例模式有用) 你用的时候才给你加载
@Lazy

bean详情

@Condition按条件注入:

1.条件类创建(实现Condition接口)

/**
* windows操作系统才能获取bean
* @author Lenovo
*/
public class WindowsCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
//获取类加载器
ClassLoader classLoader = conditionContext.getClassLoader();
//获取spring容器
ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
//获取注册器
BeanDefinitionRegistry registry = conditionContext.getRegistry();
//获取本机环境
Environment environment = conditionContext.getEnvironment();
String osName = environment.getProperty("os.name");
System.out.println("Windows:" + osName);
return osName.contains("Windows");
}
}

2.@Condition添加到类或者方法上面

@Bean
@Conditional(value = WindowsCondition.class)
public MyBean myBeanWindows() {
return new MyBean();
}

@Import导入bean:

创建bean的方式

方式1:@ComponentScan+注解(@Controller+@Service+@Repository+@Compont)-自己创建的bean

方式2:@Bean 别人的bean

方式3:@Import(快速向容器中注册一个bean)

1)@Import(要导入的组件),名称就是累的全限定名

2)ImportSelector:导入选择器,返回需要导入组件类的全限定名数组-springboot底层用的多

/**
* 选择器,
* @author Lenovo
*/
public class MyImportSelector implements ImportSelector {
/**
* 你要注册类全限定名数组
* @param annotationMetadata
* @return
*/
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
//可以做逻辑判断
return new String[]{"cn.itsource._08import_.PurpleColor", "cn.itsource._08import_.GrayColor"};
}
}

importSelector选择器

3)ImportBeanDefinitionRegistrar:通过bean定义注册器手动项目spring中容器中注册

/**
* 通过bean定义注册器手动项目spring中容器中注册
* @author Lenovo
*/
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
BeanDefinitionRegistry beanDefinitionRegistry) {
beanDefinitionRegistry.registerBeanDefinition("redColor", new RootBeanDefinition(RedColor.class));
}
}

ImportBeanDefinitionRegistar注册bean

@Import(value = {GreenColor.class, YellowColor.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})

方式4:FactoryBean的方式,返回的是getObject的类实例-和其他框架集成是用的多

public class PersonFactoryBean implements FactoryBean<Person> {
@Override
public Person getObject() throws Exception {
return new Person();
} @Override
public Class<?> getObjectType() {
return Person.class;
} @Override
public boolean isSingleton() {
return true;
}
}

factoryBean创建bean

3. springBoot入门

3.1 步骤

一 创建项目

parent

dependency

<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>

二 创建springboot项目并且启动

1)任意类加上@SpringBootApplication

2)Main函数启动springboot的应用

@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

三 写一个Contorller来测试

HelloConroller

@Controller
public class HelloController { @RequestMapping("/hello")
@ResponseBody
public String hello() {
return "hello springBoot!";
}
}

四 访问页面

3.2 打包

1.导入插件

 <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>配置类全限定名</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

2.打包

3. 运行

窗口运行:java -jar xxx.jar

后台运行: nohup java -jar XXX.jar &  只linux

javaConfig&springBoot入门的更多相关文章

  1. SpringBoot入门(三)——入口类解析

    本文来自网易云社区 上一篇介绍了起步依赖,这篇我们先来看下SpringBoot项目是如何启动的. 入口类 再次观察工程的Maven配置文件,可以看到工程的默认打包方式是jar格式的. <pack ...

  2. SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1

    在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...

  3. SpringBoot入门基础

    目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...

  4. SpringBoot入门示例

    SpringBoot入门Demo SpringBoot可以说是Spring的简化版.配置简单.使用方便.主要有以下几种特点: 创建独立的Spring应用程序 嵌入的Tomcat,无需部署WAR文件 简 ...

  5. Spring全家桶系列–[SpringBoot入门到跑路]

    //本文作者:cuifuan Spring全家桶————[SpringBoot入门到跑路] 对于之前的Spring框架的使用,各种配置文件XML.properties一旦出错之后错误难寻,这也是为什么 ...

  6. springboot入门之一:环境搭建(续)

    在上篇博客中从springboot的入门到运行一个springboot项目进行了简单讲述,详情请查看“springboot入门之一”.下面继续对springboot做讲述. 开发springboot测 ...

  7. 【Java】SpringBoot入门学习及基本使用

    SpringBoot入门及基本使用 SpringBoot的介绍我就不多说了,核心的就是"约定大于配置",接下来直接上干货吧! 本文的实例: github-LPCloud,欢迎sta ...

  8. SpringBoot入门(五)——自定义配置

    本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...

  9. SpringBoot入门(四)——自动配置

    本文来自网易云社区 SpringBoot之所以能够快速构建项目,得益于它的2个新特性,一个是起步依赖前面已经介绍过,另外一个则是自动配置.起步依赖用于降低项目依赖的复杂度,自动配置负责减少人工配置的工 ...

随机推荐

  1. SSM前后端分离/不分离对比Demo

    之前某些原因,整理了一个小的Demo,用于演示.个人认为在SSM前后端不分离的基础上在前端处理上比较麻烦一点之后就是注解的使用.总结一些对比,仅是自己掌握的,不够严谨,不足之处请大佬批评指正. 路由控 ...

  2. selenium,测试套件的使用

    学习 selenium-webdriver 已经一段时间了,最近学习到,测试用例的批量执行,和测试套件的使用,有点自己的理解,不晓得对不对,希望大家指正!   写一个测试用例 baidu.py   c ...

  3. Python读取字典(Dictionary)内数据的方法

    读取json后,数据类型为字典,对字典内数据的提取又有不同的方法,根据不同的字典类型 上图可以看到有”[]”,”{}” python语言最常见的括号有三种,分别是:小括号( ).中括号[ ]和大括号也 ...

  4. [ZJOI2008]树的统计(树链剖分)

    [ZJOI2008]树的统计(luogu) Description 一棵树上有 n 个节点,编号分别为 1 到 n,每个节点都有一个权值 w.我们将以下面的形式来要求你对这棵树完成一些操作: I. C ...

  5. 什么是LakeHouse?

    1. 引入 在Databricks的过去几年中,我们看到了一种新的数据管理范式,该范式出现在许多客户和案例中:LakeHouse.在这篇文章中,我们将描述这种新范式及其相对于先前方案的优势. 数据仓库 ...

  6. SSH免密登录设置步骤

    1.配置公钥:执行ssh-keygen即可生成SSH钥匙,一路回车即可 ssh-keygen 2.上传公钥到服务器:执行 ssh-copy-id -p port user@remote,可以让远程服务 ...

  7. static静态变量在c++类中的应用实例

    这个static 如果写在类中,那么就可以得到一个局部的静态变量,也就是说可以实现在类内保存某个特殊值不随函数释放而消失的作用.应用中由于赋初值的位置不对而报错,错误提示为:“无法解析外部符号 ... ...

  8. Spring AOP源码分析--代理方式的选择

    能坚持别人不能坚持的,才能拥有别人未曾拥有的.关注编程大道公众号,让我们一同坚持心中所想,一起成长!! 年前写了一个面试突击系列的文章,目前只有redis相关的.在这个系列里,我整理了一些面试题与大家 ...

  9. 14、 NAT

    私有IP地址段:10.0.0.0-10.255.255.255/8172.16.0.0-172.31.255.255/12192.168.0.0-192.168.255.255/16 NAT的必要性: ...

  10. how to convert wstring to string

    #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <local ...