SpringBoot第一章:eclipse创建以及javaConfig的配置使用

springBoot的创建

一.我们可以从网站上直接download下来一个压缩包,解压后导入到项目中。

二.可以使用eclipse直接创建工程项目进行创建SpringBoot.

点击新建Starter项目:

新建项目名称和包

进入到项目中,@SpringBootApplication 就是项目启动的核心类:

 

先建一个测试类:

HelloTest.java

package com.SpringBoot.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController()
public class HelloTest {
	@GetMapping("hello")
	public String helloworld() {
		return "HelloWord!!! ";
	}
}

通过Spring boot运行,如果第一次运行出错,提示没有启动成功。则很大可能是因为新建项目时,maven的依赖的springboot的版本和项目boot版本不一致所导致的问题

比如:我先建的项目 boot版本是:2.3.10

那么我在pom.xml中配置的依赖包也应该是 :2.3.10

如果不一致,我们则需要进行修改:

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.10.RELEASE</version>
    <relativePath></relativePath>
  </parent>

 

测试。我们在成功启动springboot后,在浏览器中可以进行输入url进行测试:http://localhost:8080/hello

你能明显的发现,输入的地址和springmvc中的不一样,少了项目名称。

 

JavaConfig的配置使用

JavaConfig的一个好处是可以通过注解的方式来减少Spring配置文件的代码量,能够实现相同的功能

按照正常的有以下几点:

1. 通过ApplicationContext类

通过ClassPathXmlApplicationContext 去解析applicationContext.xml 中的<bean id="hello" class="com.java.config.demo.HelloWorld"/> 这个类。

第一步:把bean配置到applicationContext.xml中,通过ApplicationContext解析

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = context.getBean("hello", HelloWorld.class);
helloWorld.sayHello();

第二步:applicationContext.xml中会自动装配:HelloWorld.java这个类,因为在配置文件中已经配置好了

<bean id="hello" class="com.java.config.demo.HelloWorld"/>

 全文如下:

<?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-4.3.xsd
	 http://www.springframework.org/schema/context
	 http://www.springframework.org/schema/context/spring-context.xsd">
	 
	<bean id="hello" class="com.java.config.demo.HelloWorld"/>
	


</beans>

 

二. 通过javaConfig 配置方式 : @Configuration 注解  

如果你不使用spring的xml配置文件去装配bean,那么我们可以使用@Configuration 配合 @Bean

HelloConfig.java:

package com.java.config.demo;

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

@Configuration
public class HelloConfig {
	@Bean
	public HelloWorld helloWorld() {
		return new HelloWorld();
	}
}

@Configuration  的作用就是通过注解的方式把HelloConfig.class这个类配置到应用上下文中(ApplicationContext) 相当于容器 ,再使用这个类时,使用AnnotationConfigApplicationContext(注解配置 - 应用上下文 ) 进行读取里面的内容

也可以做到xml的一样功能。

略微不同的是在使用调用这个bean的时候不一样:不在去解析 xml配置文件

register  解析:把创建的类注册进来

refresh()  解析: 刷新文本内容中的类对象

用main方法去调用这个bean:

        //@Configuration
		AnnotationConfigApplicationContext context = new         
        AnnotationConfigApplicationContext();
		context.register(HelloConfig.class);
		context.refresh();
		HelloWorld helloWorld = context.getBean(HelloWorld.class);
		helloWorld.sayHello();

 

三. @Component + xml解析方式

ScanHelloWorld.java

package com.java.config.demo.scan;

import org.springframework.stereotype.Component;

@Component("scanHelloWorld")
public class ScanHelloWorld {
	public void sayHello() {
		System.out.println("--------scan");
	}
}

我们使用component-scan 去扫描  com.java.config.demo.scan 包下的类

<?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-4.3.xsd
	 http://www.springframework.org/schema/context
	 http://www.springframework.org/schema/context/spring-context.xsd">
	 
	<bean id="hello" class="com.java.config.demo.HelloWorld"/>
	
	<context:component-scan base-package="com.java.config.demo.scan"/>

</beans>

然后通过: 解析加载xml文件去读取 bean内容:

会根据ScanHelloWorld名字去xml中找相应的ScanHelloWorld名字,xml会自动去扫描包下的类

        //component-scan for xml file
		ApplicationContext context = new     
        ClassPathXmlApplicationContext("applicationContext.xml");
		ScanHelloWorld scan = context.getBean("scanHelloWorld", ScanHelloWorld.class);
		scan.sayHello();

四 . 通过javaConfig 配置方式 : @ComponentScan 

ScanConfig.java  充当了 application.xml的扫描功能

package com.java.config.demo;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@ComponentScan
public class ScanConfig {

}

这样就不用通过读取解析xml配置文件了,可以直接通过javaConfig 去装配bean

app.java

        AnnotationConfigApplicationContext context = new 
        AnnotationConfigApplicationContext();
		context.register(ScanConfig.class);
		context.refresh();
		ScanHelloWorld scan = context.getBean(ScanHelloWorld.class);
		scan.sayHello();

 

五.@PropertySource 扫描多个配置文件

如果jdk版本高于:1.8

@PropertySource("classpath:file1.properties")
@PropertySource("classpath:file2.properties")

如果jdk版本是低于1.8:

@PropertySources({@PropertySource("classpath:file1.properties"), @PropertySource("classpath:file2.properties")})

 

六.@Import 

配置加载导入另一个类

比如:在HelloConfig.java中引入ScanConfig.class,那么两个类中的方法是都可以使用的

package com.java.config.demo;

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

@Configuration
@Import(ScanConfig.class)
public class HelloConfig {
	@Bean
	public HelloWorld helloWorld() {
		return new HelloWorld();
	}
}

app.java

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.register(HelloConfig.class);
		context.refresh();
		HelloWorld helloWorld = context.getBean(HelloWorld.class);
		helloWorld.sayHello();
		ScanHelloWorld scan = context.getBean(ScanHelloWorld.class);
		scan.sayHello();

 

 

七.@ImportResource("classpath:applicationContext.xml")

老版本中,引入xml文件

 

 

八.在springboot项目中: SpringBootApplication 

这个注解是集成了大部分的注解:

@SpringBootApplication 其中有使用 @EnableAutoConfiguration 注解(自动配置):

而在@EnableAutoConfiguration 注解中我们能看到它 @Import(AutoConfigurationImportSelector.class) 导入了spring容器的 自动配置类:会把所有符合条件的bean进行自动装配,也就是说它把所有的类是自动装配到了spring容器中

@SpringBootApplication相当于集成了:@Configuration + @ComponentScan  +@EnableAutoConfiguration 这三大功能

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逼哥很疯狂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值