非常重要的編碼規範
1.包名:全小寫,多個單詞用“_”隔開,不準駝峯或數字開頭
2.類名:首字母大寫,駝峯
3.方法名:首字母小寫,駝峯
4.代碼塊“{}”中的內容要有縮進,同級代碼要左對齊 , 註釋和代碼左對齊
spring 到SpringBoot
一.Spring回顧
1.基本概念
1.1.SpringBoot介紹
Spring Boot是其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。
1.2..JavaConfig基本概念
Spring註解式編程也叫JavaConfig,即通過一個Java類來配置Spring,或者說把以前xml中配置的內容搬到了一個Java類中,這個類就是Spring的配置類,和以前的XML有相同的作用。
2.Spring回顧
2.1.IOC基本概念
控制反轉,我們不需要手工去管理Bean,而是將Bean的創建,初始化,銷燬等等工作全部交給Spring管理 。解放我們的工作量。
2.2.DI基本概念
依賴注入,既然Bean的管理交給了Spring,那麼Bean之間的依賴關係也需要Spring去維護,DI指定就是Bean與Bean之間的依賴注入。
2.3.AOP基本概念
面向切面編程實現 ,可以看做是對面向對象的補充,面向切面指的是把多個Bean看成一個面,使用橫切技術可以讓功能(業務)作用於多個Bean , 使用AOP可以實現程序代碼的解耦,公共代碼的統一抽取和封裝等等。Aop的原理是動態代理。
2.4.XML配置IOC
1.創建項目
2.導入依賴
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
3.創建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- //5.把這類在 applicationContext.xml 配置成Bean-->
<bean id="myBean" class="springboot001._01_xml_ioc.Mybean01">
</bean>
</beans>
4.創建一個簡單的類 MyBean
public class MyBean {
}
5.把這類在 applicationContext.xml 配置成Bean
6.編寫測試類:讓Spring加載applicationContext.xml 配置文件,獲取容器 - ClassPathXmlApplicationContext
7.獲取MyBean
package springboot001._01_xml_ioc;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlTest {
@Test
public void testMyBean(){
//6.編寫測試類:讓Spring加載applicationContext.xml 配置文件,獲取容器
//針對於classpath下的xml配置的 Spring容器上下文對象
ClassPathXmlApplicationContext classPathXmlApplicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
//7.獲取MyBean
Mybean01 bean = classPathXmlApplicationContext.getBean(Mybean01.class);
System.out.println(bean);
}
}
2.5總結
ClassPathXmlApplicationContext//加載配置文件,拿到Spring容器
二.註解方式配置IOC
操作步驟
1.創建新的包
2.創建一個類作爲Spring的配置類
/**
* Spring的配置類 相當於是:applicationContext.xml
* @Configuration :Spring的配置標籤,標記改類是Spring的配置類
*/
@Configuration
public class MyApplicationConfig {
/**
* @Bean : Spring的bean的定義標籤 ,標記方法返回的對象交給Spring容器管理
*/
@Bean
public MyBean02 myBean(){
return new MyBean02();
}
}
- 創建一個普通的類作爲bean(MyBean)
同1
- 在配置類中,配置Bean
- 編寫測試:加載配置類 ,獲取容器 ,獲取MyBean
public class MyJavaConfigTest {
@Test
public void test(){
//加載配置文件,拿到Spring容器
ApplicationContext applicationContext =
new AnnotationConfigApplicationContext(MyApplicationConfig.class);
//通過容器獲取Mybaean
MyBean02 bean = applicationContext.getBean(MyBean02.class);
System.out.println(bean);
}
}
總結
- @Configuration加了這個註解的類就相當於傳統的一個applicationContext-xxx.xml
- @Bean Spring的bean的定義標籤 ,標記方法返回的對象交給Spring容器管理
加上這個標籤相當於<bean id=” ” class=” ”>
- AnnotationConfigApplicationContext對象拿配置類的字節碼文件
三.ComponentScan自動掃描
@ComponentScan是ioc組件自動掃描,相當於是 <context:component-scan base-package=* 默認掃描當前包及其子包 中打了 @Service , @Controller , @Component , @Repository其中一個註解的 類自動交給Spring管理;也可以通過 basePackages 屬性指定掃描的包路徑。
@Configuration
@ComponentScan("包名")
public class ApplicationConfig {
}
注意:如果使用了自動掃描 ,那麼配置類中就不要去配置@Bean
@Configuration
public class ApplicationConfig {
/**
* bean的id :就是方法的名字“myBean”
* bean的name: 通過 @Bean(name = "myBean") 指定
* init-method初始方法: @Bean(initMethod = "") 指定
* destroy-method銷燬方法:@Bean(destroyMethod = "")指定
* lazy-init="false"懶初始化: 通過標籤 @Lazy(value=false) 指定
* scope="singleton"單例: 通過 @Scope(value="singleton")指定
*/
@Bean(name = "myBean",initMethod = "init",destroyMethod = "destroy")
//@Lazy(value=false)
//@Scope(value="singleton")
public MyBean myBean(){
MyBean myBean = new MyBean();
return myBean;
}
}
四.Bean的詳解
1.Bean的屬性介紹
我們以前使用Spring使用xml進行配置,如下:
<bean id="myBean"
name="myBean004"
init-method=""
destroy-method=""
lazy-init="false"
scope="prototype"
class="cn.itsource._01_xml_ioc.MyBean">
...
</bean>
那麼在JavaCong中如何配置Bean的各種屬性?
1.1.Bean的id
那麼在JavaCong中,方法名就是Bean的id
1.2.Bean的name
可以通過 @Bean標籤的name屬性指定
1.3.生命週期方法
init-method初始方法: @Bean(initMethod = "") 指定
destroy-method銷燬方法:@Bean(destroyMethod = "")指定
1.4.Bean的Scope
單利多利,通過 @Scope("prototype")指定
1.5.懶初始化:
lazy-init="false"懶初始化: 通過標籤 @Lazy(value=false) 指定
@Configuration
@ComponentScan(basePackages = "springboot001._04_bean_detail")
public class ApplicationConfig04 {
/**
* bean的id :就是方法的名字“myBean”
* bean的name: 通過 @Bean(name = "myBean") 指定
* init-method初始方法: @Bean(initMethod = "") 指定
* destroy-method銷燬方法:@Bean(destroyMethod = "")指定
* lazy-init="false"懶初始化: 通過標籤 @Lazy(value=false) 指定
* scope="singleton"單例: 通過 @Scope(value="singleton")指定
*/
@Bean(name = "myBean004",initMethod = "init",destroyMethod = "destroy")
@Lazy(value=false) //false不使用懶加載
@Scope(value="singleton")
public MyBean04 myBean(){
MyBean04 myBean = new MyBean04();
return myBean;
}
}
2.定義Mybean的init和destroy方法
public class MyBean04 {
String name = "zs";
public MyBean04() {
System.out.println("無參構造被調用了");
this.name="無參構造";
}
public MyBean04(String name) {
this.name = name;
}
public void init() {
this.name = "初始化實例";
System.out.println("初始化實例....");
}
public void destroy() {
System.out.println("銷燬實例....");
}
}
3.Spring的Test
上面的案例中我們的是使用的Junit的測試 ,在很多場景下Junit測試不能滿足我們的需求,比如Junit測試會導致Bean的銷燬方法沒辦辦法正常管理,所以我們需要用到Spring的測試。
//使用Spring的Test ,不要直接用Junit的Test
//@ContextConfiguration(classes = ApplicationConfig.class) :加載Spring的配置類
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationConfig.class)
public class JavaConfigTest {
@Autowired
private MyBean myBean;
@Test
public void testMyBean() throws Exception{
// MyBean myBean2= new MyBean("xzc");
// MyBean myBean3= new MyBean();
System.out.println("myBean.name "+myBean.name);
// System.out.println("myBean2.name "+myBean2.name);
// System.out.println("myBean3.name "+myBean3.name);
}
}
總結
1.使用Spring的測試之後,就不要再使用new AnnotationConfigApplicationContext這種方式
2.@ContextConfiguration標籤後面跟的是配置類的字節碼
3.注入是先調用無參構造然後調用init初始化方法的
五.依賴注入
我們以前使用Spring使用xml進行配置,我們除了定義Bean而外還會給Bean進行依賴對象的注入,如下:
<bean id="myBean"
...
class="">
<property name="username" value="zs" />
<property name="OtherBean" ref="OtherBean" />
</bean>
1.操作步驟
1.創建BaseBean類,BaseBean中有OtherBean字段
@Data
public class BaseBean {
String name = "BaseBean";
OtherBean otherbean = null;
}
2.創建OtherBean類
@Data
public class OtherBean {
String name = "我是OtherBean";
}
3.定義MyBean,OtherBean並把OtherBean設置給MyBean
@Configuration
public class ApplicationConfig05 {
@Bean
public BaseBean BaseBean(OtherBean otherbean){
BaseBean baseBean=new BaseBean();
baseBean.setName("我是MyBean");
baseBean.setOtherbean(otherbean);
return baseBean;
}
@Bean
public OtherBean OtherBean(){
return new OtherBean();
}
}
- 測試
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationConfig05.class)
public class JavaConfigTest {
@Autowired
private BaseBean baseBean;
@Autowired
private OtherBean otherBean;
@Test
public void testMyBean() throws Exception{
System.out.println("baseBean.name "+baseBean.name);
System.out.println("otherBean.name "+otherBean.name);
System.out.println(baseBean);
}
}
- 總結
//定義MyBean的條件類,MyBean是否能定義,通過該類的 matches 方法返回的boolean決定的
public class MyBeanCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//環境對象
Environment environment = context.getEnvironment();
//操作系統名字
String os = environment.getProperty("os.name");
System.out.println(os);
if(os.equals("Windows 10")){
return true;
}
return false;
}
}
六.條件Conditional
Conditional用來對某些組件是否註冊做條件限制,比如:Conditional註解帖在bean的定義方法上來判斷,如果不滿足條件就不會定義bean
創建一個Bean,如果操作系統的環境是Windows ,那就創建,否則不創建
1.流程
1.建包
2.建類MyBean
3.建配置類
@Configuration
public class ApplicationConfig {
@Bean
@Conditional(value = MyBeanCondition.class)
public MyBean myBean(){
return new MyBean();
}
}
4.配置條件類,配置MyBean
//定義MyBean的條件類,MyBean是否能定義,通過該類的 matches 方法返回的boolean決定的
public class MyBeanCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//環境對象
Environment environment = context.getEnvironment();
//操作系統名字
String os = environment.getProperty("os.name");
if(os.equals("Windows 10")){
return true;
}
return false;
}
}
- 測試
2.總結
1. @Conditional(value = MyBeanCondition.class)用來映射條件類的字節碼文件
2. 條件類的本質是實現 Conditio接口的matches方法,該方法返回一個布爾值。
七.Import導入
@Import標籤,可以有用來實現配置類之間的導入,如同 “<import resource="shriot.xml" /> 導入配置 , 當然還可以通過導入ImportSelector,以及導入ImportBeanDefinitionRegistrar的方式註冊Bean。
1.導入配置Configuration
演示案例:創建兩個配置類,一個配置類 ApplicationConfig 配置MyBean,另一個配置類 OtherConfig 配置OtherBean,使用@Improt把兩個配置類導入在一起,並且MyBean中的OtherBean要有值
//主配置類
//@Import(OtherConfig.class): 導入另外一個配置類
@Configuration
@Import(OtherApplicationConfig.class)
public class BaseApplicationConfig {
@Bean
public MyBean myBean(OtherBean otherBean){
MyBean myBean = new MyBean();
myBean.setOtherBean(otherBean);
return myBean;
}
}
//第二個配置類
@Configuration
public class OtherConfig {
@Bean
public OtherBean otherBean(){
return new OtherBean();
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationConfig.class)
public class JavaConfigTest {
@Autowired
private MyBean myBean;
@Autowired
private OtherBean otherBean;
@Test
public void testMyBean() throws Exception{
System.out.println(myBean);
System.out.println(myBean.getOtherBean());
System.out.println(otherBean);
}
}
2.總結
顯然就是在 五.依賴注入的基礎上吧把一個配置類的東西拆開放在兩個配置類裏面,擴展的類通過@Import(OtherApplicationConfig.class)鏈接基礎配置類,同時測試文件導入基礎配置類。
- 導入ImportSelector
- SpringBoot-HelloWorld
1.HelloWorld
使用Spring創建有一個簡單Web程序,通過瀏覽器訪問 http://localhost:8080 能夠返回“hello world”信息
-
- HelloWorld-web應用
打開官網https://docs.spring.io/spring-boot/docs/2.1.13.RELEASE/reference/html/getting-started-installing-spring-boot.html#getting-started-installation-instructions-for-java
1.配置pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.創建配置類
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!Spring Boot";
}
public static void main(String[] args) {
SpringApplication.run(Example.class, args);
}
}
3.成功
2.SpringBoot的特點
1.使用註解配置,無需xml(簡單粗暴)
2.快速搭建,開發
3.簡化的maven
4.方便的和三方框架集成
5.內嵌tomcat,部署簡單
6.內置健康檢查,監控等
7.自動配置,讓配置更加簡單