天天看點

spring的IOC基于annotation

(不推薦)

 步驟一:先查spring的文檔找到annotation-based configuration

配置spring的配置檔案。先将context的命名空間配上,再<context:annotation-config/>,這句實際上是生成了幾個掃描spring自己注解的對象

步驟二:添加spring的注解 @Autowired(不推薦使用spring自己的注解)到相應的set方法上

注:

是按照byType注入的。

還可以在參數上@Qualifier("name")指定同一類型中确定的bean.

@Autowired(required=false)指明即使沒有相應的type的bean,而無法注入也可以。

(推薦,Java自己的注解)

步驟一:

<context:annotation-config/>

(可以看做開啟spring自己注解的注解掃描器)

<context:component-scan base-package="com.jsun"/>

(spring不用再XML的形式配bean,都自動掃描到)

需要配合@Component注解spring自己的class級别的注解

@Component

@Component(”componet_name“)

步驟二:

JSR-250的标準:@Resource  j2ee的注解.

@Resource      (預設先按标準的bean名字,然後按照type找)

@Resource(name="“)

繼續閱讀