詳解Spring bean的註解註入之@Autowired的原理及使用

一、@Autowired

概念:

@Autowired 註釋,它可以對類成員變量、方法及構造函數進行標註,完成自動裝配的工作。 通過 @Autowired的使用來消除 set ,get方法。

在使用@Autowired之前,我們對一個bean配置起屬性時,用的是

<property name="屬性名" value=" 屬性值"/>    

使用@Autowired之後,我們隻需要在需要使用的地方使用一個@Autowired 就可以瞭。

代碼使用:

public interface StudentService {
    public boolean login(String username,String password);
}
@Service
public class StudentServiceImpl implements StudentService {

    @Override
    public boolean login(String username,String password) {
       if("crush".equals(username)&&"123456".equals(password)){
            System.out.println("登錄成功");
            return true;
        }
        return false;
    }
}
@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;
        public void login(){
       boolean crush = studentService.login("crush", "123456");
       if(crush){
           System.out.println("crush"+"登錄成功!!!!!");
       }else{
           System.out.println("登錄失敗");
       }
    }
}

測試:

@Test
    public void login(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        StudentController student = applicationContext.getBean("studentController", StudentController.class);
        student.login();
    }

我們在使用@Autowired 之後不用再去xml文件中繼續配置瞭。

註意細節:

1、使用@Autowired的當前類也必須由spring容器托管(打@Coponent、@Controller、@Service 、@repository)

2、不管是public 和 private 修飾的字段都可以自動註入

3、默認情況下,使用@Autowired註解的屬性一定要被裝配,如果在容器中找不到該類型的bean註入,就會報錯。如果允許不被裝配就可以將@Autowired的required屬性為false

4、@Autowired 是基於類型的註入,如果當前類型屬性在容器中隻有一個Bean, 那麼屬性名不限制,但一般建議遵循類名首字母小寫的規則‘

5、如果當前屬性類型在容器中有個多個Bean,那麼必須要通過屬性名 或者 @Qualifier 指定Bean name

6、@Autowired 可以打在XXX[] 、List上 ,此時會將容器中所有XXX類型的bean 都註入進去、且屬性名沒有約束,但是註意可以通過@Qualifier指定註入指定beanName的bean,屬性名是沒有約束作用的

7、@Autowired可以打在Map<String,XXX>上,此時所有XXX類型的bean都會被註入 ,beanName 為key ,對象為value,但是註意可以通過@Qualifier指定註入指定beanName的bean,屬性名是沒有約束作用的

二、@Service、@Repository、@Controller、@Component

這幾個註解的含義都是一樣的,都是寫在類上面或者接口上面,將自動註冊到Spring容器。

1、@Service用於標註業務層組件
2、@Controller用於標註控制層組件(如struts中的action)
3、@Repository用於標註數據訪問組件,即DAO組件.
4、@Component泛指組件,當組件不好歸類的時候,我們可以使用這個註解進行標註。 註冊到Spring 容器中。

使用

@Service
public class StudentServiceImpl implements StudentService {
}
@Controller
public class StudentController {
}

其作用就相當於在application.xml文件中 寫以下代碼

<bean id="studentServiceImpl" class="com.crush.service.impl.StudentServiceImpl"/>
<bean id="studentController" class="com.crush.controller.StudentController"/>

當然如果要使註解生效,必不可少的要加上這樣一行掃描包的代碼

<!--讓com.crush包下類中使用 spring的註解生效-->
<context:component-scan base-package="com.crush"/>

三、@Bean

@Bean明確地指示瞭一種方法,什麼方法呢——產生一個bean的方法,並且交給Spring容器管理;從這我們就明白瞭為啥@Bean是放在方法的註釋上瞭,因為它很明確地告訴被註釋的方法,你給我產生一個Bean,然後交給Spring容器,剩下的你就別管瞭

四、@Configuration

@Configuration用於定義配置類 這裡隻簡單說明。

Spring 目前是有兩種配置方式的,一種是xml文件配置加Java 代碼,這種是從Spring出生的時候就有瞭,另一種是完全使用Java代碼來進行配置及編寫,這是在Spring 後面版本才出的。

從Spring3.0,@Configuration用於定義配置類,可替換xml配置文件被註解的類內部包含有一個或多個被@Bean註解的方法,這些方法將會被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進行掃描,並用於構建bean定義,初始化Spring容器。

這種方式更加受java程序員的喜歡。

@Configuration
public class MyConfig {
}

並且這種方式在後續的學習中,在Spring源碼中使用的非常多。

五、@Resource

@Resource的作用相當於@Autowired,隻不過@Autowired按byType自動註入,而@Resource默認按 byName自動註入罷瞭。@Resource有兩個屬性是比較重要的,分是name和type,Spring將@Resource註解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。所以如果使用name屬性,則使用byName的自動註入策略,而使用type屬性時則使用byType自動註入策略。如果既不指定name也不指定type屬性,這時將通過反射機制使用byName自動註入策略。

@Autowired 與@Resource的區別

@Autowired原理

到此這篇關於詳解Spring bean的註解註入之@Autowired的原理及使用的文章就介紹到這瞭,更多相關Autowired原理內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: