spring之通過註解方式配置Bean(一)
(1)元件掃描:spring能夠從classpath下自動掃描、偵測和例項化具有特定註解的元件。
(2)特定元件包括:
- @Component:基本註解,標識一個受spring管理的元件;
- @Respority:標識持久層元件;
- @Service:標識服務層(業務層)元件;
- @Controller:標識表現層元件;
(3)對於掃描到的元件,spring有預設的命名規則:使用非限定類名。第一個字母小寫,也可以在註解中通過value屬性值標識元件的名稱。
(4)當在元件類上使用了特定的註解之後,還需要在spring的配置檔案中宣告<context:component-scan>:
- base-package屬性指定一個需要掃描的基類包,spring容器將會掃描這個基類包裡及其子包的所有類;
- 當需要掃描多個包時,可以使用逗號分隔;
- 如果僅希望掃描特定的類而非基包下的所有類,可使用resource-pattern屬性過濾特定的類,示例:
<contest:component-scan base-package=":com.gong.spring.beans" resource-pattern="autowire/*.class"> - <context:include-filter>子節點表示要包含的目標類
- <context:exclude-filter>子節點表示要排除在外的目標類
- <context:component-scan>下可以擁有若干個<context:include-filter>和<context:exclude-filter>子節點;
一、所需環境
引入以下jar包,加入到build path中:
兩個連線資料庫的,五個spring核心包,aop是額外的使用註解方式需要引入的包,一定要記得引入這個包。
基本目錄如下:
二、基本配置
TestObjec.java
package com.gong.spring.beans.annotation; import org.springframework.stereotype.Component; @Component public class TestObject { }
UserController.java
package com.gong.spring.beans.annotation.controller; import org.springframework.stereotype.Controller; @Controller public class UserController { public void execute() { System.out.println("UserController的execute方法"); } }
UserRepository.java
package com.gong.spring.beans.annotation.repository; public interface UserRepository { public void save(); }
UserRepositoryImpl.java
package com.gong.spring.beans.annotation.repository; import org.springframework.stereotype.Repository; //可以指定使用時的名字 @Repository(value="userRepository") public class UserRepositoryImpl implements UserRepository{ @Override public void save() { // TODO Auto-generated method stub System.out.println("UserReposityImpl的save方法"); } }
UserService.java
package com.gong.spring.beans.annotation.service; import org.springframework.stereotype.Service; @Service public class UserService { public void add() { System.out.println("UserService中的add方法"); } }
Main.java
package com.gong.spring.beans.annotation; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.gong.spring.beans.annotation.controller.UserController; import com.gong.spring.beans.annotation.repository.UserRepository; import com.gong.spring.beans.annotation.service.UserService; public class Main { public static void main(String[] args) { //1.建立spring的IOC容器物件 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml"); //2.從容器中獲取Bean例項 TestObject to = (TestObject) ctx.getBean("testObject"); System.out.println(to); UserController userController = (UserController) ctx.getBean("userController"); System.out.println(userController); UserService userService = (UserService) ctx.getBean("userService"); System.out.println(userService); UserRepository userRepository = (UserRepository) ctx.getBean("userRepository"); System.out.println(userRepository); } }
beans-annotation.xml(需要引入context名稱空間)
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 配置springIOC容器掃描的包 --> <context:component-scan base-package="com.gong.spring.beans.annotation"></context:component-scan> </beans>
輸出:
說明這些帶有註解的類已經被spring所識別並被IOC容器所管理。需要注意的是,預設情況下獲取bean的例項時,名字是類名,但首字母是小寫。例如TestObject通過getBean("testObject")來獲取,當然,也可以在註解時使用value屬性來宣告bean的名字。
三、子節點配置
再來看:
<!-- 配置springIOC容器掃描的包 --> <context:component-scan base-package="com.gong.spring.beans.annotation" resource-pattern="repository/*.class"></context:component-scan>
指定掃描的模式之後,我們就只能訪問到repository下面的元件了,即輸出:
=
在<context:include-filter>和<context:exclude-filter>子節點支援多種型別的表示式:
- annotation:所有標註了XxxAnnotation的類;
- assignable:所有繼承或擴充套件XxxService的類;
- aspectj
- regex
- custom
(1)使用annotation的方式
<!-- 配置springIOC容器掃描的包 --> <context:component-scan base-package="com.gong.spring.beans.annotation"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan>
不包含org.springframework.stereotype.Repository,即輸出:
<!-- 配置springIOC容器掃描的包 --> <context:component-scan base-package="com.gong.spring.beans.annotation" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan>
只包含org.springframework.stereotype.Repository,注意需要配置use-default-filters為false,即輸出:
(2)使用assignable方式
<!-- 配置springIOC容器掃描的包 --> <context:component-scan base-package="com.gong.spring.beans.annotation"> <context:exclude-filter type="assignable" expression="com.gong.spring.beans.annotation.repository.UserRepository"/> </context:component-scan>
不包含UserRepository介面及其實現類的。
<!-- 配置springIOC容器掃描的包 --> <context:component-scan base-package="com.gong.spring.beans.annotation" use-default-filters="false"> <context:include-filter type="assignable" expression="com.gong.spring.beans.annotation.repository.UserRepository"/> </context:component-scan>
設定use-default-filters="false"。只包含UserRepository介面及其實現類