Spring自帶的@Component注解及擴展@Repository、@Service、@Controller,如圖
在使用注解方式配置bean時,需要引進一個包:
使用方法:
1、為需要使用注解方式的類添加注解標記
@Component("標識符")
POJO類
在類上使用@Component注解,表示該類定義為Spring管理Bean,使用默認value(可選)屬性表示Bean標識符。如果不指定標識符,默認為首字母小寫類名。例如類UserController的標識符為userController
2、在xml中配置自動掃描策略
1 <context:component-scan 2 base-package="" 3 resource-pattern="**/*.class" 4 name-generator="org.springframework.context.annotation.AnnotationBeanNameGenerator" 5 use-default-filters="true" 6 annotation-config="true"> 7 <context:include-filter type="aspectj" expression=""/> 8 <context:exclude-filter type="regex" expression=""/> 9 </context:component-scan>
-
base-package:表示掃描注解類的開始位置,即將在指定的包中掃描,其他包中的注解類將不被掃描,默認將掃描所有類路徑;
-
resource-pattern:表示掃描注解類的后綴匹配模式,即“base-package+resource-pattern”將組成匹配模式用於匹配類路徑中的組件,默認后綴為“**/*.class”,即指定包下的所有以.class結尾的類文件;
-
name-generator:默認情況下的Bean標識符生成策略,默認是AnnotationBeanNameGenerator,其將生成以小寫開頭的類名(不包括包名);可以自定義自己的標識符生成策略;
-
use-default-filters:默認為true表示過濾@Component、@ManagedBean、@Named注解的類,如果改為false默認將不過濾這些默認的注解來定義Bean,即這些注解類不能被過濾到,即不能通過這些注解進行Bean定義;
-
annotation-config:表示是否自動支持注解實現Bean依賴注入,默認支持,如果設置為false,將關閉支持注解的依賴注入,需要通過<context:annotation-config/>開啟。
-
<context:include-filter>:表示過濾到的類將被注冊為Spring管理Bean。需要配合use-default-filters使用
-
<context:exclude-filter>:表示過濾到的類將不被注冊為Spring管理Bean,它比<context:include-filter>具有更高優先級;
-
type:表示過濾器類型,目前支持注解類型、類類型、正則表達式、aspectj表達式過濾器,當然也可以自定義自己的過濾器,實現org.springframework.core.type.filter.TypeFilter即可;
-
expression:表示過濾器表達式。
下面為案例分析:

1 package com.proc.bean; 2 3 import org.springframework.stereotype.Component; 4 5 @Component 6 public class TestObject { 7 8 }

1 package com.proc.bean.Controller; 2 3 import org.springframework.stereotype.Controller; 4 5 @Controller 6 public class UserController { 7 8 }

1 package com.proc.bean.repository; 2 3 public interface UserRepository { 4 5 void save(); 6 }

1 package com.proc.bean.repository; 2 3 import org.springframework.stereotype.Repository; 4 5 @Repository("userRepository") 6 public class UserRepositoryImps implements UserRepository{ 7 8 @Override 9 public void save() { 10 System.out.println("UserRepository save"); 11 } 12 }

1 package com.proc.bean.service; 2 3 import org.springframework.stereotype.Service; 4 5 @Service 6 public class UserService { 7 8 }
1、在xml中配置,通過base-package指定掃描指定包及其子包下所有類
1 <context:component-scan base-package="com.proc.bean"></context:component-scan>
測試輸出
1 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); 2 3 TestObject testObject=(TestObject) ctx.getBean("testObject"); 4 System.out.println(testObject); 5 6 UserController userController=(UserController) ctx.getBean("userController"); 7 System.out.println(userController); 8 9 UserService userService=(UserService) ctx.getBean("userService"); 10 System.out.println(userService); 11 12 UserRepository userRepository=(UserRepository)ctx.getBean("userRepository"); 13 System.out.println(userRepository);
輸出結果:
com.proc.bean.TestObject@50d156
com.proc.bean.Controller.UserController@61c7e3
com.proc.bean.service.UserService@11d0846
com.proc.bean.repository.UserRepositoryImps@1e4c80f
2、指定resource-pattern:資源匹配,只掃描controller包下面的所有類
<context:component-scan base-package="com.proc.bean" resource-pattern="controller/*.class"> </context:component-scan>
這里是能夠正確獲取到com.proc.bean.Controller.UserController@61c7e3
3、 排除使用指定注解標簽的類
1 <context:component-scan base-package="com.proc.bean"> 2 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 3 </context:component-scan>
type:選擇類型 annotation:注解標簽、assignable:類名方式
這里能夠正確獲取到
com.proc.bean.TestObject@191f517
com.proc.bean.controller.UserController@5965f2
com.proc.bean.service.UserService@9bd883
4、排除指定標識符的類
1 <context:component-scan base-package="com.proc.bean"> 2 <context:exclude-filter type="assignable" expression="com.proc.bean.controller.UserController"/> 3 </context:component-scan>
這里排除了com.proc.bean.controller.UserController類型的,所以只能夠正確得到
com.proc.bean.TestObject@134517
com.proc.bean.service.UserService@50d156
com.proc.bean.repository.UserRepositoryImps@61c7e3
5、包含指定注解標記的類
1 <context:component-scan base-package="com.proc.bean" use-default-filters="false"> 2 <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 3 </context:component-scan>
這里只能夠正確得到
com.proc.bean.repository.UserRepositoryImps@1200884
在使用include-filter是需要配合use-default-filters="false",讓自動掃描注解不使用默認的filter,而是使用我們指定的filter,否則將無效
6、包含指定類型的類
1 <context:component-scan base-package="com.proc.bean" use-default-filters="false"> 2 <context:include-filter type="assignable" expression="com.proc.bean.TestObject"/> 3 </context:component-scan>
這里只能夠正確得到
com.proc.bean.TestObject@17f23d9
最后提醒:
<context:include-filter/>和<context-exclude-filter/>標簽可以使用多個