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

  1. /**
  2. * 方式一:直接new
  3. * @throws Exception
  4. */
  5. @Test
  6. public void test1() throws Exception {
  7. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
  8. String[] beanDefinitionNames = context.getBeanDefinitionNames();
  9. for (String beanDefinitionName : beanDefinitionNames) {
  10. System.out.println(beanDefinitionName);
  11. }
  12. }

直接new

方式二:注入:Runwith ContextConfigration

  1. /**
  2. * 方式二:注解方式 springtest注入
  3. * @author Lenovo
  4. */
  5. @RunWith(SpringJUnit4ClassRunner.class)
  6. @ContextConfiguration("classpath:applicationContext-xml.xml")
  7. public class SpringTestTest {
  8. @Autowired
  9. private ApplicationContext applicationContext;
  10.  
  11. @Test
  12. public void test2() throws Exception {
  13. for (String beanDefinitionName : applicationContext.getBeanDefinitionNames()) {
  14. System.out.println(beanDefinitionName);
  15. }
  16. }
  17. }

注解方式

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>

  1. /**
  2. * @Configuration 加上此注解就相当于一个spring配置文件(applicationContext.xml)
  3. * @author Lenovo
  4. */
  5. @Configuration
  6. public class IocConfig {
  7. /**
  8. * @Bean 就相当于创建了一个bean
  9. * 等同于 <bean id="myBean" class="....MyBean"/>
  10. * @return
  11. */
  12. @Bean
  13. public MyBean myBean() {
  14. return new MyBean();
  15. }
  16. }

基本的配置类

扫描包:

bean扫描(@ComponentScan/ComponentScans)

  1. /**
  2. * 注解方式配置bean
  3. * 1.打注解
  4. * 2.扫描包
  5. *
  6. * @author Lenovo
  7. */
  8. @Configuration
  9. /**扫描父类包
  10. @ComponentScan("cn.itsource._05componentScan")
  11. */
  12.  
  13. /**扫描多个包的方式
  14. @ComponentScans(value = {
  15. @ComponentScan("cn.itsource._05componentScan.controller"),
  16. @ComponentScan("cn.itsource._05componentScan.service")
  17. })*/
  18.  
  19. //排除指定注解或者包含指定注解的bean
  20. @ComponentScans(value = {
  21. /*@ComponentScan(value = "cn.itsource._05componentScan", excludeFilters = {
  22. //排除指定的注解
  23. @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
  24. })*/
  25. //包含指定的注解
  26. @ComponentScan(value = "cn.itsource._05componentScan", includeFilters = {
  27. @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Service.class})
  28. }, useDefaultFilters = false )//关闭默认全部扫描includeFilters才生效)
  29. })
  30. public class IocConfig {
  31.  
  32. }

ComponentScan扫描包

Bean详情:

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

bean详情

@Condition按条件注入:

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

  1. /**
  2. * windows操作系统才能获取bean
  3. * @author Lenovo
  4. */
  5. public class WindowsCondition implements Condition {
  6. @Override
  7. public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
  8. //获取类加载器
  9. ClassLoader classLoader = conditionContext.getClassLoader();
  10. //获取spring容器
  11. ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
  12. //获取注册器
  13. BeanDefinitionRegistry registry = conditionContext.getRegistry();
  14. //获取本机环境
  15. Environment environment = conditionContext.getEnvironment();
  16. String osName = environment.getProperty("os.name");
  17. System.out.println("Windows:" + osName);
  18. return osName.contains("Windows");
  19. }
  20. }

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

  1. @Bean
  2. @Conditional(value = WindowsCondition.class)
  3. public MyBean myBeanWindows() {
  4. return new MyBean();
  5. }

@Import导入bean:

创建bean的方式

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

方式2:@Bean 别人的bean

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

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

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

  1. /**
  2. * 选择器,
  3. * @author Lenovo
  4. */
  5. public class MyImportSelector implements ImportSelector {
  6. /**
  7. * 你要注册类全限定名数组
  8. * @param annotationMetadata
  9. * @return
  10. */
  11. @Override
  12. public String[] selectImports(AnnotationMetadata annotationMetadata) {
  13. //可以做逻辑判断
  14. return new String[]{"cn.itsource._08import_.PurpleColor", "cn.itsource._08import_.GrayColor"};
  15. }
  16. }

importSelector选择器

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

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

ImportBeanDefinitionRegistar注册bean

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

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

  1. public class PersonFactoryBean implements FactoryBean<Person> {
  2. @Override
  3. public Person getObject() throws Exception {
  4. return new Person();
  5. }
  6.  
  7. @Override
  8. public Class<?> getObjectType() {
  9. return Person.class;
  10. }
  11.  
  12. @Override
  13. public boolean isSingleton() {
  14. return true;
  15. }
  16. }

factoryBean创建bean

3. springBoot入门

3.1 步骤

一 创建项目

parent

dependency

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.5.RELEASE</version>
  5. </parent>
  6. <dependencies>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-web</artifactId>
  10. </dependency>
  11. </dependencies>

二 创建springboot项目并且启动

1)任意类加上@SpringBootApplication

2)Main函数启动springboot的应用

  1. @SpringBootApplication
  2. public class App {
  3. public static void main(String[] args) {
  4. SpringApplication.run(App.class, args);
  5. }
  6. }

三 写一个Contorller来测试

HelloConroller

  1. @Controller
  2. public class HelloController {
  3.  
  4. @RequestMapping("/hello")
  5. @ResponseBody
  6. public String hello() {
  7. return "hello springBoot!";
  8. }
  9. }

四 访问页面

3.2 打包

1.导入插件

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. <configuration>
  7. <mainClass>配置类全限定名</mainClass>
  8. </configuration>
  9. <executions>
  10. <execution>
  11. <goals>
  12. <goal>repackage</goal>
  13. </goals>
  14. </execution>
  15. </executions>
  16. </plugin>
  17. </plugins>
  18. </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. 创建dynamics CRM client-side (十二) - HTML Web Resource

    HTML Web Resource是我们经常使用的一个功能. 第一步, 我们先创建好一个HTML. 接下来,我们要在web resource中创建新的html web resource. 我们在tex ...

  2. selenium,滚到页面底部的方法

    你可以用 execute_script方法来处理这个. 调用原生javascript的API,这样你想滚到哪里就能滚到哪里.   下面的代码演示了如何滚到页面的最下面:   driver.execut ...

  3. [NOI2005]维护数列(区间splay)

    [NOI2005]维护数列(luogu) 打这玩意儿真是要了我的老命 Description 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际输入文 ...

  4. 超链接a标签的伪类选择器问题,Link标签与visited标签的失效问题(问题介绍与解决方法)。

    <!DOCTYPE html>< html>< head>     <meta charset="utf-8" />     < ...

  5. 通信协议之Modbus协议(一)

    Modbus通信协议: 简介:Modbus协议是应用于电子控制器上的一种通用语言 通过此协议,控制器相互之间,控制器经由网络(例如以太网) 和其他设备之间可以通信,他已经成为一种通用工业标准,有啦它 ...

  6. 死磕java(7)

    http://www.cnblogs.com/liunanjava/p/4296045.html 自己写的例子 package com.sougn.trynew; public abstract cl ...

  7. Codeforces 1296F Berland Beauty

    题目链接:http://codeforces.com/problemset/problem/1296/F 思路: 1————2————3————4————5————6 1->3 2 2-> ...

  8. css-position:absolute, relative 的用法

      static(静态) 没有特别的设定,遵循基本的定位规定,不能通过z-index进行层次分级.就无法通过top,left ,bottom,right 定位.(static 为默认值)  relat ...

  9. Codeforces_327_C

    http://codeforces.com/problemset/problem/327/C 等比求和相加,有mod的出现,所以要算逆元. #include<iostream> #incl ...

  10. HDU_1494_dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1494 能量用0-14表示,dp[i][j]表示走到第i段,所剩能量j的最小时间. #include<ios ...