詳解Spring bean的注解注入之@Autowired的原理及使用
一、@Autowired
概念:
@Autowired 注釋,它可以對類成員變量、方法及構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動裝配的工作。 通過 @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文件中繼續(xù)配置了。
注意細(xì)節(jié):
1、使用@Autowired的當(dāng)前類也必須由spring容器托管(打@Coponent、@Controller、@Service 、@repository)
2、不管是public 和 private 修飾的字段都可以自動注入
3、默認(rèn)情況下,使用@Autowired注解的屬性一定要被裝配,如果在容器中找不到該類型的bean注入,就會報錯。如果允許不被裝配就可以將@Autowired的required屬性為false
4、@Autowired 是基于類型的注入,如果當(dāng)前類型屬性在容器中只有一個Bean, 那么屬性名不限制,但一般建議遵循類名首字母小寫的規(guī)則‘
5、如果當(dāng)前屬性類型在容器中有個多個Bean,那么必須要通過屬性名 或者 @Qualifier 指定Bean name
6、@Autowired 可以打在XXX[] 、List上 ,此時會將容器中所有XXX類型的bean 都注入進(jìn)去、且屬性名沒有約束,但是注意可以通過@Qualifier指定注入指定beanName的bean,屬性名是沒有約束作用的
7、@Autowired可以打在Map<String,XXX>上,此時所有XXX類型的bean都會被注入 ,beanName 為key ,對象為value,但是注意可以通過@Qualifier指定注入指定beanName的bean,屬性名是沒有約束作用的
二、@Service、@Repository、@Controller、@Component
這幾個注解的含義都是一樣的,都是寫在類上面或者接口上面,將自動注冊到Spring容器。
1、@Service用于標(biāo)注業(yè)務(wù)層組件
2、@Controller用于標(biāo)注控制層組件(如struts中的action)
3、@Repository用于標(biāo)注數(shù)據(jù)訪問組件,即DAO組件.
4、@Component泛指組件,當(dāng)組件不好歸類的時候,我們可以使用這個注解進(jìn)行標(biāo)注。 注冊到Spring 容器中。
使用
@Service public class StudentServiceImpl implements StudentService { }
@Controller public class StudentController { }
其作用就相當(dāng)于在application.xml文件中 寫以下代碼
<bean id="studentServiceImpl" class="com.crush.service.impl.StudentServiceImpl"/> <bean id="studentController" class="com.crush.controller.StudentController"/>
當(dāng)然如果要使注解生效,必不可少的要加上這樣一行掃描包的代碼
<!--讓com.crush包下類中使用 spring的注解生效--> <context:component-scan base-package="com.crush"/>
三、@Bean
@Bean明確地指示了一種方法,什么方法呢——產(chǎn)生一個bean的方法,并且交給Spring容器管理;從這我們就明白了為啥@Bean是放在方法的注釋上了,因為它很明確地告訴被注釋的方法,你給我產(chǎn)生一個Bean,然后交給Spring容器,剩下的你就別管了
四、@Configuration
@Configuration用于定義配置類 這里只簡單說明。
Spring 目前是有兩種配置方式的,一種是xml文件配置加Java 代碼,這種是從Spring出生的時候就有了,另一種是完全使用Java代碼來進(jìn)行配置及編寫,這是在Spring 后面版本才出的。
從Spring3.0,@Configuration用于定義配置類,可替換xml配置文件被注解的類內(nèi)部包含有一個或多個被@Bean注解的方法,這些方法將會被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進(jìn)行掃描,并用于構(gòu)建bean定義,初始化Spring容器。
這種方式更加受java程序員的喜歡。
@Configuration public class MyConfig { }
并且這種方式在后續(xù)的學(xué)習(xí)中,在Spring源碼中使用的非常多。
五、@Resource
@Resource的作用相當(dāng)于@Autowired,只不過@Autowired按byType自動注入,而@Resource默認(rèn)按 byName自動注入罷了。@Resource有兩個屬性是比較重要的,分是name和type,Spring將@Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。所以如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不指定name也不指定type屬性,這時將通過反射機(jī)制使用byName自動注入策略。
到此這篇關(guān)于詳解Spring bean的注解注入之@Autowired的原理及使用的文章就介紹到這了,更多相關(guān)Autowired原理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
restTemplate未設(shè)置連接數(shù)導(dǎo)致服務(wù)雪崩問題以及解決
面對線上問題,仔細(xì)分析原因,及時調(diào)整配置,能有效解決問題,本文詳細(xì)描述了線上遇到流量突增引發(fā)的問題,通過查看代碼和連接池信息,分析出問題的原因是連接池滿了,連接池大小配置不足以應(yīng)對大并發(fā)流量,通過調(diào)整連接池大小配置2024-10-10Java NIO實例UDP發(fā)送接收數(shù)據(jù)代碼分享
這篇文章主要介紹了Java NIO實例UDP發(fā)送接收數(shù)據(jù)代碼分享,分享了客戶端和服務(wù)端完整代碼,小編覺得還是挺不錯的,共需要的朋友參考。2017-11-11解決sharding JDBC 不支持批量導(dǎo)入問題
這篇文章主要介紹了解決sharding JDBC 不支持批量導(dǎo)入問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10