1. 程式人生 > 實用技巧 >讀取Excel並生成分析圖-pyecharts例項

讀取Excel並生成分析圖-pyecharts例項

簡介

Spring框架是由於軟體開發的複雜性而建立的。Spring使用的是基本的JavaBean來完成以前只可能由EJB完成的事情。然而,Spring的用途不僅僅限於伺服器端的開發。從簡單性、可測試性和鬆耦合性角度而言,絕大部分Java應用都可以從Spring中受益。

概括:Spring是一個輕量級的控制反轉(IoC)和麵向切面(AOP)的容器(框架)。

優點

1、Spring是一個開源免費的框架 , 容器 .

2、Spring是一個輕量級的框架 , 非侵入式的 .

3、控制反轉 IoC , 面向切面 Aop

4、對事物的支援 , 對框架的支援

組成

Spring 框架是一個分層架構,由 7 個定義良好的模組組成。Spring 模組構建在核心容器之上,核心容器定義了建立、配置和管理 bean 的方式 .

組成 Spring 框架的每個模組(或元件)都可以單獨存在,或者與其他一個或多個模組聯合實現。每個模組的功能如下:

  • 核心容器:核心容器提供 Spring 框架的基本功能。核心容器的主要元件是 BeanFactory,它是工廠模式的實現。BeanFactory 使用控制反轉(IOC) 模式將應用程式的配置和依賴性規範與實際的應用程式程式碼分開。
  • Spring 上下文:Spring 上下文是一個配置檔案,向 Spring 框架提供上下文資訊。Spring 上下文包括企業服務,例如 JNDI、EJB、電子郵件、國際化、校驗和排程功能。
  • Spring AOP:通過配置管理特性,Spring AOP 模組直接將面向切面的程式設計功能 , 整合到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理任何支援 AOP的物件。Spring AOP 模組為基於 Spring 的應用程式中的物件提供了事務管理服務。通過使用 Spring AOP,不用依賴元件,就可以將宣告性事務管理整合到應用程式中。
  • Spring DAO:JDBC DAO 抽象層提供了有意義的異常層次結構,可用該結構來管理異常處理和不同資料庫供應商丟擲的錯誤訊息。異常層次結構簡化了錯誤處理,並且極大地降低了需要編寫的異常程式碼數量(例如開啟和關閉連線)。Spring DAO 的面向 JDBC 的異常遵從通用的 DAO 異常層次結構。
  • Spring ORM:Spring 框架插入了若干個 ORM 框架,從而提供了 ORM 的物件關係工具,其中包括 JDO、Hibernate 和 iBatis SQL Map。所有這些都遵從 Spring 的通用事務和 DAO 異常層次結構。
  • Spring Web 模組:Web 上下文模組建立在應用程式上下文模組之上,為基於 Web 的應用程式提供了上下文。所以,Spring 框架支援與 Jakarta Struts 的整合。Web 模組還簡化了處理多部分請求以及將請求引數繫結到域物件的工作。
  • Spring MVC 框架:MVC 框架是一個全功能的構建 Web 應用程式的 MVC 實現。通過策略介面,MVC 框架變成為高度可配置的,MVC 容納了大量檢視技術,其中包括 JSP、Velocity、Tiles、iText 和 POI。

IoC基礎

新建一個空白的maven專案

分析實現

傳統方式程式碼 .

1、UserDao介面

public interface UserDao {
   public void getUser();
}

2、Dao的實現類

public class UserDaoImpl implements UserDao {
   @Override
   public void getUser() {
       System.out.println("獲取使用者資料");
  }
}

3、UserService的介面

public interface UserService {
   public void getUser();
}

4、Service的實現類

public class UserServiceImpl implements UserService {
   private UserDao userDao = new UserDaoImpl();

   @Override
   public void getUser() {
       userDao.getUser();
  }
}

5、測試

@Test
public void test(){
   UserService service = new UserServiceImpl();
   service.getUser();
}

把Userdao的實現類增加一個 .

public class UserDaoMySqlImpl implements UserDao {
   @Override
   public void getUser() {
       System.out.println("MySql獲取使用者資料");
  }
}

緊接著我們要去使用MySql的話 , 我們就需要去service實現類裡面修改對應的實現

public class UserServiceImpl implements UserService {
   private UserDao userDao = new UserDaoMySqlImpl();

   @Override
   public void getUser() {
       userDao.getUser();
  }
}

再增加一個Userdao的實現類 .

public class UserDaoOracleImpl implements UserDao {
   @Override
   public void getUser() {
       System.out.println("Oracle獲取使用者資料");
  }
}

每次變動 , 都需要修改大量程式碼 . 這種設計的耦合性太高 .

解決

利用set

public class UserServiceImpl implements UserService {
   private UserDao userDao;
// 利用set實現
   public void setUserDao(UserDao userDao) {
       this.userDao = userDao;
  }

   @Override
   public void getUser() {
       userDao.getUser();
  }
}

測試

@Test
public void test(){
   UserServiceImpl service = new UserServiceImpl();
   service.setUserDao( new UserDaoMySqlImpl() );
   service.getUser();
   //用Oracle
   service.setUserDao( new UserDaoOracleImpl() );
   service.getUser();
}

這種思想 , 從本質上解決了問題 , 我們程式設計師不再去管理物件的建立了 , 更多的去關注業務的實現 . 耦合性大大降低 .

IOC本質

控制反轉IoC(Inversion of Control),是一種設計思想,DI(依賴注入)是實現IoC的一種方法,也有人認為DI只是IoC的另一種說法。沒有IoC的程式中 , 我們使用面向物件程式設計 , 物件的建立與物件間的依賴關係完全硬編碼在程式中,物件的建立由程式自己控制,控制反轉後將物件的建立轉移給第三方,所謂控制反轉就是:獲得依賴物件的方式反轉了。

IoC是Spring框架的核心內容,使用多種方式完美的實現了IoC,可以使用XML配置,也可以使用註解。

Spring容器在初始化時先讀取配置檔案,根據配置檔案或元資料建立與組織物件存入容器中,程式使用時再從Ioc容器中取出需要的物件。

採用XML方式配置Bean的時候,Bean的定義資訊是和實現分離的,而採用註解的方式可以把兩者合為一體,Bean的定義資訊直接以註解的形式定義在實現類中,從而達到了零配置的目的。

控制反轉是一種通過描述(XML或註解)並通過第三方去生產或獲取特定物件的方式。在Spring中實現控制反轉的是IoC容器,其實現方法是依賴注入(Dependency Injection,DI)。

Spring應用

HelloSpring

匯入Jar包

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>5.1.10.RELEASE</version>
</dependency>

編寫程式碼

1、編寫一個Hello實體類

public class Hello {
   private String name;

   public String getName() {
       return name;
  }
   public void setName(String name) {
       this.name = name;
  }

   public void show(){
       System.out.println("Hello,"+ name );
  }
}

2、編寫beans.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">

   <!--bean就是java物件 , 由Spring建立和管理-->
   <bean id="hello" class="com.study.pojo.Hello">
       <property name="name" value="Spring"/>
   </bean>

</beans>

3、測試

@Test
public void test(){
   //解析beans.xml檔案 , 生成管理相應的Bean物件
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   //getBean : 引數即為spring配置檔案中bean的id .
   Hello hello = (Hello) context.getBean("hello");
   hello.show();
}

分析

  • hello 物件是由Spring建立的
  • hello 物件的屬性是由Spring容器設定的

控制反轉 :

  • 控制 : 誰來控制物件的建立 , 傳統應用程式的物件是由程式本身控制建立的 , 使用Spring後 , 物件是由Spring來建立的
  • 反轉 : 程式本身不建立物件 , 而變成被動的接收物件 .

依賴注入 : 就是利用set方法來進行注入的.

IOC是一種程式設計思想,由主動的程式設計變成被動的接收

IOC建立物件方式

通過無參構造方法來建立

1、User.java

public class User {

   private String name;

   public User() {
       System.out.println("user無參構造方法");
  }

   public void setName(String name) {
       this.name = name;
  }

   public void show(){
       System.out.println("name="+ name );
  }

}

2、beans.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">

   <bean id="user" class="com.study.pojo.User">
       <property name="name" value="studyshen"/>
   </bean>

</beans>

3、測試類

@Test
public void test(){
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   //在執行getBean的時候, user已經建立好了 , 通過無參構造
   User user = (User) context.getBean("user");
   //呼叫物件的方法 .
   user.show();
}

通過有參構造方法來建立

1、UserT . java

public class UserT {

   private String name;

   public UserT(String name) {
       this.name = name;
  }

   public void setName(String name) {
       this.name = name;
  }

   public void show(){
       System.out.println("name="+ name );
  }

}

2、beans.xml 有三種方式編寫

<!-- 第一種根據index引數下標設定 -->
<bean id="userT" class="com.study.pojo.UserT">
   <!-- index指構造方法 , 下標從0開始 -->
   <constructor-arg index="0" value="studyshen2"/>
</bean>
<!-- 第二種根據引數名字設定 -->
<bean id="userT" class="com.study.pojo.UserT">
   <!-- name指引數名 -->
   <constructor-arg name="name" value="studyshen2"/>
</bean>
<!-- 第三種根據引數型別設定 -->
<bean id="userT" class="com.study.pojo.UserT">
   <constructor-arg type="java.lang.String" value="studyshen2"/>
</bean>

3、測試

@Test
public void testT(){
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   UserT user = (UserT) context.getBean("userT");
   user.show();
}

結論:在配置檔案載入的時候。其中管理的物件都已經初始化了!

Spring配置

別名

alias 設定別名 , 為bean設定別名 , 可以設定多個別名

<!--設定別名:在獲取Bean的時候可以使用別名獲取-->
<alias name="userT" alias="userNew"/>

Bean的配置

<!--bean就是java物件,由Spring建立和管理-->

<!--
   id 是bean的識別符號,要唯一,如果沒有配置id,name就是預設識別符號
   如果配置id,又配置了name,那麼name是別名
   name可以設定多個別名,可以用逗號,分號,空格隔開
   如果不配置id和name,可以根據applicationContext.getBean(.class)獲取物件;

class是bean的全限定名=包名+類名
-->
<bean id="hello" name="hello2 h2,h3;h4" class="com.study.pojo.Hello">
   <property name="name" value="Spring"/>
</bean>

import

團隊的合作通過import來實現 .

<import resource="{path}/beans.xml"/>

依賴注入(DI)

概念

  • 依賴注入(Dependency Injection,DI)。
  • 依賴 : 指Bean物件的建立依賴於容器 . Bean物件的依賴資源 .
  • 注入 : 指Bean物件所依賴的資源 , 由容器來設定和裝配 .

Set 注入

要求被注入的屬性 , 必須有set方法 , set方法的方法名由set + 屬性首字母大寫

測試pojo類 :

Address.java

 public class Address {
 
     private String address;
 
     public String getAddress() {
         return address;
    }
 
     public void setAddress(String address) {
         this.address = address;
    }
 }

Student.java

 package com.study.pojo;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 
 public class Student {
 
     private String name;
     private Address address;
     private String[] books;
     private List<String> hobbys;
     private Map<String,String> card;
     private Set<String> games;
     private String wife;
     private Properties info;
 
     public void setName(String name) {
         this.name = name;
    }
 
     public void setAddress(Address address) {
         this.address = address;
    }
 
     public void setBooks(String[] books) {
         this.books = books;
    }
 
     public void setHobbys(List<String> hobbys) {
         this.hobbys = hobbys;
    }
 
     public void setCard(Map<String, String> card) {
         this.card = card;
    }
 
     public void setGames(Set<String> games) {
         this.games = games;
    }
 
     public void setWife(String wife) {
         this.wife = wife;
    }
 
     public void setInfo(Properties info) {
         this.info = info;
    }
 
     public void show(){
         System.out.println("name="+ name
                 + ",address="+ address.getAddress()
                 + ",books="
        );
         for (String book:books){
             System.out.print("<<"+book+">>\t");
        }
         System.out.println("\n愛好:"+hobbys);
 
         System.out.println("card:"+card);
 
         System.out.println("games:"+games);
 
         System.out.println("wife:"+wife);
 
         System.out.println("info:"+info);
 
    }
 }

1、常量注入

 <bean id="student" class="com.study.pojo.Student">
     <property name="name" value="小明"/>
 </bean>

測試:

 @Test
 public void test01(){
     ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");
 
     Student student = (Student) context.getBean("student");
 
     System.out.println(student.getName());
 
 }

2、Bean注入

注意點:這裡的值是一個引用,ref

 <bean id="addr" class="com.study.pojo.Address">
     <property name="address" value="重慶"/>
 </bean>
 
 <bean id="student" class="com.study.pojo.Student">
     <property name="name" value="小明"/>
     <property name="address" ref="addr"/>
 </bean>

3、陣列注入

 <bean id="student" class="com.study.pojo.Student">
     <property name="name" value="小明"/>
     <property name="address" ref="addr"/>
     <property name="books">
         <array>
             <value>西遊記</value>
             <value>紅樓夢</value>
             <value>水滸傳</value>
         </array>
     </property>
 </bean>

4、List注入

 <property name="hobbys">
     <list>
         <value>聽歌</value>
         <value>看電影</value>
         <value>爬山</value>
     </list>
 </property>

5、Map注入

 <property name="card">
     <map>
         <entry key="中國郵政" value="456456456465456"/>
         <entry key="建設" value="1456682255511"/>
     </map>
 </property>

6、set注入

 <property name="games">
     <set>
         <value>LOL</value>
         <value>BOB</value>
         <value>COC</value>
     </set>
 </property>

7、Null注入

 <property name="wife"><null/></property>

8、Properties注入

 <property name="info">
     <props>
         <prop key="學號">20190604</prop>
         <prop key="性別">男</prop>
         <prop key="姓名">小明</prop>
     </props>
 </property>

Bean的作用域

在Spring中,那些組成應用程式的主體及由Spring IoC容器所管理的物件,被稱之為bean。簡單地講,bean就是由IoC容器初始化、裝配及管理的物件 .

幾種作用域中,request、session作用域僅在基於web的應用中使用,只能用在基於web的Spring ApplicationContext環境。

Singleton

當一個bean的作用域為Singleton,那麼Spring IoC容器中只會存在一個共享的bean例項,並且所有對bean的請求,只要id與該bean定義相匹配,則只會返回bean的同一例項。Singleton是單例型別,就是在建立起容器時就同時自動建立了一個bean的物件,不管你是否使用,他都存在了,每次獲取到的物件都是同一個物件。注意,Singleton作用域是Spring中的預設作用域。要在XML中將bean定義成singleton,配置:

 <bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">

測試:

 @Test
 public void test03(){
     ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");
     User user = (User) context.getBean("user");
     User user2 = (User) context.getBean("user");
     System.out.println(user==user2);
 }

Prototype

當一個bean的作用域為Prototype,表示一個bean定義對應多個物件例項。Prototype作用域的bean會導致在每次對該bean請求(將其注入到另一個bean中,或者以程式的方式呼叫容器的getBean()方法)時都會建立一個新的bean例項。Prototype是原型型別,它在我們建立容器的時候並沒有例項化,而是當我們獲取bean的時候才會去建立一個物件,而且我們每次獲取到的物件都不是同一個物件。根據經驗,對有狀態的bean應該使用prototype作用域,而對無狀態的bean則應該使用singleton作用域。在XML中將bean定義成prototype,配置:

 <bean id="account" class="com.foo.DefaultAccount" scope="prototype"/>  
  或者
 <bean id="account" class="com.foo.DefaultAccount" singleton="false"/>

自動裝配

Bean的自動裝配

自動裝配說明

  • 自動裝配是使用spring滿足bean依賴的一種方法
  • spring會在應用上下文中為某個bean尋找其依賴的bean。

Spring中bean有三種裝配機制,分別是:

  1. 在xml中顯式配置;
  2. 在java中顯式配置;
  3. 隱式的bean發現機制和自動裝配。

這裡我們主要講第三種:自動化的裝配bean。

Spring的自動裝配需要從兩個角度來實現,或者說是兩個操作:

  1. 元件掃描(component scanning):spring會自動發現應用上下文中所建立的bean;
  2. 自動裝配(autowiring):spring自動滿足bean之間的依賴,也就是我們說的IoC/DI;

元件掃描和自動裝配組合發揮巨大威力,使得顯示的配置降低到最少。

推薦不使用自動裝配xml配置 , 而使用註解 .

測試環境搭建

1、新建一個專案

2、新建兩個實體類,Cat Dog 都有一個叫的方法

public class Cat {
   public void shout() {
       System.out.println("miao~");
  }
}
public class Dog {
   public void shout() {
       System.out.println("wang~");
  }
}

3、新建一個使用者類 User

public class User {
   private Cat cat;
   private Dog dog;
   private String str;
}

4、編寫Spring配置檔案

<?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">

   <bean id="dog" class="com.study.pojo.Dog"/>
   <bean id="cat" class="com.study.pojo.Cat"/>

   <bean id="user" class="com.study.pojo.User">
       <property name="cat" ref="cat"/>
       <property name="dog" ref="dog"/>
       <property name="str" value="lalala"/>
   </bean>
</beans>

5、測試

public class MyTest {
   @Test
   public void testMethodAutowire() {
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       User user = (User) context.getBean("user");
       user.getCat().shout();
       user.getDog().shout();
  }
}

autowire byName (按名稱自動裝配)

由於在手動配置xml過程中,常常發生字母缺漏和大小寫等錯誤,而無法對其進行檢查,使得開發效率降低。

採用自動裝配將避免這些錯誤,並且使配置簡單化。

autowire byType (按型別自動裝配)

使用autowire byType首先需要保證:同一型別的物件,在spring容器中唯一。如果不唯一,會報不唯一的異常。

使用註解

使用註解

準備工作:利用註解的方式注入屬性。

1、在spring配置檔案中引入context檔案頭

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

2、開啟屬性註解支援!

<context:annotation-config/>

@Autowired

  • @Autowired是按型別自動轉配的,不支援id匹配。
  • 需要匯入 spring-aop的包!

測試:

1、將User類中的set方法去掉,使用@Autowired註解

public class User {
   @Autowired
   private Cat cat;
   @Autowired
   private Dog dog;
   private String str;

   public Cat getCat() {
       return cat;
  }
   public Dog getDog() {
       return dog;
  }
   public String getStr() {
       return str;
  }
}

2、此時配置檔案內容

<context:annotation-config/>

<bean id="dog" class="com.study.pojo.Dog"/>
<bean id="cat" class="com.study.pojo.Cat"/>
<bean id="user" class="com.study.pojo.User"/>

3、測試,成功輸出結果!

@Autowired(required=false) 說明:false,物件可以為null;true,物件必須存物件,不能為null。

//如果允許物件為null,設定required = false,預設為true
@Autowired(required = false)
private Cat cat;

@Qualifier

  • @Autowired是根據型別自動裝配的,加上@Qualifier則可以根據byName的方式自動裝配
  • @Qualifier不能單獨使用。

測試實驗步驟:

1、配置檔案修改內容,保證型別存在物件。且名字不為類的預設名字!

<bean id="dog1" class="com.study.pojo.Dog"/>
<bean id="dog2" class="com.study.pojo.Dog"/>
<bean id="cat1" class="com.study.pojo.Cat"/>
<bean id="cat2" class="com.study.pojo.Cat"/>

2、沒有加Qualifier測試,直接報錯

3、在屬性上新增Qualifier註解

@Autowired
@Qualifier(value = "cat2")
private Cat cat;
@Autowired
@Qualifier(value = "dog2")
private Dog dog;

@Resource

  • @Resource如有指定的name屬性,先按該屬性進行byName方式查詢裝配;
  • 其次再進行預設的byName方式進行裝配;
  • 如果以上都不成功,則按byType的方式自動裝配。
  • 都不成功,則報異常。

實體類:

public class User {
   //如果允許物件為null,設定required = false,預設為true
   @Resource(name = "cat2")
   private Cat cat;
   @Resource
   private Dog dog;
   private String str;
}

beans.xml

<bean id="dog" class="com.study.pojo.Dog"/>
<bean id="cat1" class="com.study.pojo.Cat"/>
<bean id="cat2" class="com.study.pojo.Cat"/>

<bean id="user" class="com.study.pojo.User"/>

測試:結果OK

配置檔案2:beans.xml , 刪掉cat2

<bean id="dog" class="com.study.pojo.Dog"/>
<bean id="cat1" class="com.study.pojo.Cat"/>

實體類上只保留註解

@Resource
private Cat cat;
@Resource
private Dog dog;

結果:OK

結論:先進行byName查詢,失敗;再進行byType查詢,成功。

@Autowired與@Resource異同:

1、@Autowired與@Resource都可以用來裝配bean。都可以寫在欄位上,或寫在setter方法上。

2、@Autowired預設按型別裝配(屬於spring規範),預設情況下必須要求依賴物件必須存在,如果要允許null 值,可以設定它的required屬性為false,如:@Autowired(required=false) ,如果我們想使用名稱裝配可以結合@Qualifier註解進行使用

3、@Resource(屬於J2EE復返),預設按照名稱進行裝配,名稱可以通過name屬性進行指定。如果沒有指定name屬性,當註解寫在欄位上時,預設取欄位名進行按照名稱查詢,如果註解寫在setter方法上預設取屬性名進行裝配。當找不到與名稱匹配的bean時才按照型別進行裝配。但是需要注意的是,如果name屬性一旦指定,就只會按照名稱進行裝配。

它們的作用相同都是用註解方式注入物件,但執行順序不同。@Autowired先byType,@Resource先byName。

使用註解開發

1、配置掃描哪些包下的註解

<!--指定註解掃描包-->
<context:component-scan base-package="com.study.pojo"/>

2、在指定包下編寫類,增加註解

@Component("user")
// 相當於配置檔案中 <bean id="user" class="當前註解的類"/>
public class User {
   public String name = "lala";
}

3、測試

@Test
public void test(){
   ApplicationContext applicationContext =
       new ClassPathXmlApplicationContext("beans.xml");
   User user = (User) applicationContext.getBean("user");
   System.out.println(user.name);
}

屬性注入

使用註解注入屬性

1、可以不用提供set方法,直接在直接名上新增@value("值")

@Component("user")
// 相當於配置檔案中 <bean id="user" class="當前註解的類"/>
public class User {
   @Value("小明")
   // 相當於配置檔案中 <property name="name" value="小明"/>
   public String name;
}

2、如果提供了set方法,在set方法上新增@value("值");

@Component("user")
public class User {

   public String name;

   @Value("小明")
   public void setName(String name) {
       this.name = name;
  }
}

XML與註解比較

  • XML可以適用任何場景 ,結構清晰,維護方便
  • 註解不是自己提供的類使用不了,開發簡單方便

基於Java類進行配置

JavaConfig

測試:

1、編寫一個實體類,Dog

@Component  //將這個類標註為Spring的一個元件,放到容器中!
public class Dog {
   public String name = "dog";
}

2、新建一個config配置包,編寫一個MyConfig配置類

@Configuration  //代表這是一個配置類
public class MyConfig {

   @Bean //通過方法註冊一個bean,這裡的返回值就Bean的型別,方法名就是bean的id!
   public Dog dog(){
       return new Dog();
  }

}

3、測試

@Test
public void test2(){
   ApplicationContext applicationContext =
           new AnnotationConfigApplicationContext(MyConfig.class);
   Dog dog = (Dog) applicationContext.getBean("dog");
   System.out.println(dog.name);
}