javascript
Spring配置形式之基于注解的方式
在classpath中掃描組件
組件掃描(component scanning): Spring 能夠從 classpath 下自動(dòng)掃描, 偵測和實(shí)例化具有特定注解的組件.
特定組件包括:
@Component: 基本注解, 標(biāo)識了一個(gè)受 Spring 管理的組件
@Respository: 標(biāo)識持久層組件
@Service: 標(biāo)識服務(wù)層(業(yè)務(wù)層)組件
@Controller: 標(biāo)識表現(xiàn)層組件
對于掃描到的組件, Spring 有默認(rèn)的命名策略: 使用非限定類名, 第一個(gè)字母小寫. 也可以在注解中通過 value 屬性值標(biāo)識組件的名稱
base-package 屬性指定一個(gè)需要掃描的基類包,Spring 容器將會(huì)掃描這個(gè)基類包里及其子包中的所有類.
當(dāng)需要掃描多個(gè)包時(shí), 可以使用逗號分隔.
如果僅希望掃描特定的類而非基包下的所有類,可使用 resource-pattern 屬性過濾特定的類,示例:
<context:include-filter> 子節(jié)點(diǎn)表示要包含的目標(biāo)類
<context:exclude-filter> 子節(jié)點(diǎn)表示要排除在外的目標(biāo)類
<context:component-scan> 下可以擁有若干個(gè) <context:include-filter> 和 <context:exclude-filter> 子節(jié)點(diǎn)
<context:include-filter> 和 <context:exclude-filter> 子節(jié)點(diǎn)支持多種類型的過濾表達(dá)式:
<context:component-scan> 元素還會(huì)自動(dòng)注冊 AutowiredAnnotationBeanPostProcessor 實(shí)例, 該實(shí)例可以自動(dòng)裝配具有 @Autowired 和 @Resource 、@Inject注解的屬性.
使用@Autowired
@Autowired 注解自動(dòng)裝配具有兼容類型的單個(gè) Bean屬性
-構(gòu)造器, 普通字段(即使是非 public), 一切具有參數(shù)的方法都可以應(yīng)用@Authwired 注解
-默認(rèn)情況下, 所有使用 @Authwired 注解的屬性都需要被設(shè)置. 當(dāng) Spring 找不到匹配的 Bean 裝配屬性時(shí), 會(huì)拋出異常, 若某一屬性允許不被設(shè)置, 可以設(shè)置 ? ? ? ? ? @Authwired 注解的 required 屬性為 false
-默認(rèn)情況下, 當(dāng) IOC 容器里存在多個(gè)類型兼容的 Bean 時(shí), 通過類型的自動(dòng)裝配將無法工作. 此時(shí)可以在 @Qualifier 注解里提供 Bean 的名稱. Spring 允許對方法的 ? ? ? ? 入?yún)?biāo)注 @Qualifiter 已指定注入 Bean 的名稱
-@Authwired 注解也可以應(yīng)用在數(shù)組類型的屬性上, 此時(shí) Spring 將會(huì)把所有匹配的 Bean 進(jìn)行自動(dòng)裝配.
-@Authwired 注解也可以應(yīng)用在集合屬性上, 此時(shí) Spring 讀取該集合的類型信息, 然后自動(dòng)裝配所有與之兼容的 Bean.
-@Authwired 注解用在 java.util.Map 上時(shí), 若該 Map 的鍵值為 String, 那么 Spring 將自動(dòng)裝配與之 Map 值類型兼容的 Bean, 此時(shí) Bean 的名稱作為鍵值
使用@Autowired或@Inject自動(dòng)裝配Bean
Spring 還支持 @Resource 和 @Inject 注解,這兩個(gè)注解和 @Autowired 注解的功用類似
@Resource 注解要求提供一個(gè) Bean 名稱的屬性,若該屬性為空,則自動(dòng)采用標(biāo)注處的變量或方法名作為 Bean 的名稱
@Inject 和 @Autowired 注解一樣也是按類型匹配注入的 Bean, 但沒有 reqired 屬性
建議使用 @Autowired 注解
相關(guān)代碼:
結(jié)構(gòu):
UserController.java
@Controller public class UserController {@Autowiredprivate UserService userService;public void execute(){System.out.println("UserController execute...");userService.add();} }UserRepository.java
public interface UserRepository {void save(); }UserJdbcRepository.java
@Repository public class UserJdbcRepository implements UserRepository{@Overridepublic void save() {System.out.println("UserJdbcRepository save..."); } }UserRepositoryImpl.java
@Repository public class UserRepositoryImpl implements UserRepository{@Autowired(required=false)private TestObject testObject;@Overridepublic void save() {System.out.println("UserRepository Save...");System.out.println("testObject"); } }UserService.java
@Service public class UserService {private UserRepository userRepository; @Autowiredpublic void setUserRepository(@Qualifier("userRepositoryImpl") UserRepository userRepository) {this.userRepository = userRepository;}public void add(){System.out.println("UserService add...");userRepository.save();} }TestObject.java
public class TestObject {}Main.java
public class Main {public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("bean-annotation.xml"); // TestObject testObject=(TestObject) context.getBean("testObject"); // System.out.println(testObject); UserController userController=(UserController) context.getBean("userController");System.out.println(userController);userController.execute();// UserService userService=(UserService) context.getBean("userService"); // System.out.println(userService); // // UserRepository userRepository=(UserRepository) context.getBean("userRepository"); // System.out.println(userRepository); } }bean-annotation.xml
<!-- 指定spring ioc容器掃描的包 --><!-- 可以通過resource-pattern="repository/*.class"指定掃描的資源 --><!--<context:component-scan base-package="com.yslf.annotation" resource-pattern="repository/*.class"></context:component-scan>--><!-- context:exclude-filter子節(jié)點(diǎn)指定排除哪些指定表達(dá)式的組件 --><!--context:include-filter子節(jié)點(diǎn)指定包含哪些表達(dá)式的組件,該子節(jié)點(diǎn)需要use-default-filters="false"配合 --><!-- --><!-- --><context:component-scan base-package="com.yslf.annotation"><!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>--><!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>--><!-- <context:exclude-filter type="assignable" expression="com.yslf.annotation.repository.UserRepository"/>--><!-- <context:include-filter type="assignable" expression="com.yslf.annotation.repository.UserRepository"/>--> </context:component-scan>?
轉(zhuǎn)載于:https://www.cnblogs.com/zhlzy/p/7271768.html
總結(jié)
以上是生活随笔為你收集整理的Spring配置形式之基于注解的方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 4912 Paths on th
- 下一篇: Spring Data JPA 条件查