纵有疾风起
人生不言弃

springBoot快速入门

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

<profile>    <id>jdk‐1.8</id>    <activation>        <activeByDefault>true</activeByDefault>        <jdk>1.8</jdk>    </activation>    <properties>        <maven.compiler.source>1.8</maven.compiler.source>        <maven.compiler.target>1.8</maven.compiler.target>        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>    </properties></profile>

 

★ idea中是自动集成springBoot插件

★ eclipse 需要安装一个spring ide

新建一个空白项目

 springBoot快速入门插图

新建一个maven quick-start模块

springBoot快速入门插图1

pom文件内容:

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

<parent>  <groupId>org.springframework.boot</groupId>  <artifactId>spring‐boot‐starter‐parent</artifactId>  <version>2.2.2.RELEASE</version></parent><dependencies>  <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring‐boot‐starter‐web</artifactId>  </dependency></dependencies><!‐‐ 这个插件,可以将应用打包成一个可执行的jar包;‐‐>  <build>      <plugins>          <plugin>              <groupId>org.springframework.boot</groupId>              <artifactId>spring‐boot‐maven‐plugin</artifactId>          </plugin>      </plugins>  </build>

 

启动代码

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);    }}

springBoot快速入门插图2

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

Pom

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.boot.spring</groupId>  <artifactId>spring4-demo</artifactId>  <version>1.0-SNAPSHOT</version>  <name>spring4-demo</name>  <!-- FIXME change it to the project's website -->  <url>http://www.example.com</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <maven.compiler.source>1.8</maven.compiler.source>    <maven.compiler.target>1.8</maven.compiler.target>  </properties>  <dependencies>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context</artifactId>      <version>4.3.14.RELEASE</version>    </dependency>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.11</version>      <scope>test</scope>    </dependency>  </dependencies>  <build>    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->      <plugins>        <plugin>          <artifactId>maven-clean-plugin</artifactId>          <version>3.0.0</version>        </plugin>        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->        <plugin>          <artifactId>maven-resources-plugin</artifactId>          <version>3.0.2</version>        </plugin>        <plugin>          <artifactId>maven-compiler-plugin</artifactId>          <version>3.7.0</version>        </plugin>        <plugin>          <artifactId>maven-surefire-plugin</artifactId>          <version>2.20.1</version>        </plugin>        <plugin>          <artifactId>maven-jar-plugin</artifactId>          <version>3.0.2</version>        </plugin>        <plugin>          <artifactId>maven-install-plugin</artifactId>          <version>2.5.2</version>        </plugin>        <plugin>          <artifactId>maven-deploy-plugin</artifactId>          <version>2.8.2</version>        </plugin>      </plugins>    </pluginManagement>  </build></project>

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();    }}

 

 

 

 

文章转载于:https://www.cnblogs.com/lm970585581/p/9800356.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » springBoot快速入门
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录