博客
关于我
springBoot快速入门
阅读量:437 次
发布时间:2019-03-06

本文共 4420 字,大约阅读时间需要 14 分钟。

给maven 的settings.xml配置文件的profiles标签添加

jdk‐1.8
true
1.8
1.8
1.8
1.8

 

★ idea中是自动集成springBoot插件

★ eclipse 需要安装一个spring ide

新建一个空白项目

 

新建一个maven quick-start模块

pom文件内容:

导入spring boot相关的依赖与Maven插件

org.springframework.boot
spring‐boot‐starter‐parent
2.2.2.RELEASE
org.springframework.boot
spring‐boot‐starter‐web
org.springframework.boot
spring‐boot‐maven‐plugin

 

启动代码

package com.demo.boot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * SpringBoot启动类: * 1、使用@SpringBootApplication * 2、提供应用程序的入口执行 * */@SpringBootApplicationpublic class App {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

追根溯源,Spring4演示(不再需要写配置文件)

Pom

4.0.0
com.boot.spring
spring4-demo
1.0-SNAPSHOT
spring4-demo
http://www.example.com
UTF-8
1.8
1.8
org.springframework
spring-context
4.3.14.RELEASE
junit
junit
4.11
test
maven-clean-plugin
3.0.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.7.0
maven-surefire-plugin
2.20.1
maven-jar-plugin
3.0.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2

User

 

package com.boot.spring;import org.springframework.stereotype.Component;@Componentpublic class User {}

 

APP类(注解方式)

 

package com.boot.spring;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Hello world! * */public class App {    public static void main( String[] args ) {        AnnotationConfigApplicationContext context =                new AnnotationConfigApplicationContext("com.boot.spring");        User user = context.getBean(User.class);        System.out.println(user);        context.close();    }}

 

使用@Configuration注解方式

 

package com.boot.spring;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class UseConfig {    @Bean("user")    public User createUser() {        return new User();    }}

 

使用@Configuration注解方式后的app类

package com.boot.spring;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Hello world! * */public class App {    public static void main( String[] args ) {        AnnotationConfigApplicationContext context =                new AnnotationConfigApplicationContext("com.boot.spring");        //@Component方式创建的对象,name默认为类名的首字母小写        User user = context.getBean("user",User.class);        //@Configuration方式创建的对象,name默认为注解了 @Bean的方法名createUser,可以自定义名称:@Bean("user")        //spring-boot的创建方式就是基于此实现的        User user1 = context.getBean("user",User.class);        System.out.println(user+","+user1);        //打印内容:com.boot.spring.User@5e57643e,com.boot.spring.User@5e57643e        //单例实现方式:通过key-name值相同,对象相同的方式来实现        UseConfig useConfig = context.getBean(UseConfig.class);        System.out.println(useConfig);        //打印内容:com.boot.spring.UseConfig$$EnhancerBySpringCGLIB$$84f0eb4d@5e955596        context.close();    }}

 

 

 

 

转载地址:http://gyayz.baihongyu.com/

你可能感兴趣的文章
LiveGBS user/save 逻辑缺陷漏洞复现(CNVD-2023-72138)
查看>>
localhost:5000在MacOS V12(蒙特利)中不可用
查看>>
logstash mysql 准实时同步到 elasticsearch
查看>>
Luogu2973:[USACO10HOL]赶小猪
查看>>
mabatis 中出现< 以及> 代表什么意思?
查看>>
Mac book pro打开docker出现The data couldn’t be read because it is missing
查看>>
MAC M1大数据0-1成神篇-25 hadoop高可用搭建
查看>>
mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
查看>>
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>