spring学习笔记04-IOC常用注解(一)
文章目錄
- 2.3常用注解
- 2.3.1 用于創(chuàng)建對(duì)象的
- 2.3.1.1 @Component
- 2.3.1.2 @Controller @Service @Repository
- 2.3.2 用于注入數(shù)據(jù)的
- 2.3.2.1 @Autowired
- 2.3.2.2 @Qualifier
- 2.3.2.3 @Resource
- 2.3.2.4 @Value
- 2.3.3 用于改變作用范圍的:
- 2.3.3.1 @Scope
- 2.3.4 和生命周期相關(guān)的:(了解)
- bean.xml(用xml方式配置)
- bean.xml(用注解來(lái)實(shí)現(xiàn)對(duì)象的配置,注
2.3常用注解
2.3.1 用于創(chuàng)建對(duì)象的
相當(dāng)于:
<bean id="" class="">2.3.1.1 @Component
作用:
??把資源讓 spring 來(lái)管理。相當(dāng)于在 xml 中配置一個(gè) bean。
屬性:
??value:指定 bean 的 id。如果不指定 value 屬性,默認(rèn) bean 的 id 是當(dāng)前類(lèi)的類(lèi)名。首字母小寫(xiě)。
2.3.1.2 @Controller @Service @Repository
??他們?nèi)齻€(gè)注解都是針對(duì)一個(gè)的衍生注解,他們的作用及屬性都是一模一樣的。
??他們只不過(guò)是提供了更加明確的語(yǔ)義化。
@Controller:一般用于表現(xiàn)層的注解。
@Service:一般用于業(yè)務(wù)層的注解。
@Repository:一般用于持久層的注解。
細(xì)節(jié):如果注解中有且只有一個(gè)屬性要賦值時(shí),且名稱(chēng)是 value,value 在賦值是可以不寫(xiě)。
2.3.2 用于注入數(shù)據(jù)的
相當(dāng)于:
<property name="" ref=""> <property name="" value="">2.3.2.1 @Autowired
作用:
??自動(dòng)按照類(lèi)型注入。當(dāng)使用注解注入屬性時(shí),set 方法可以省略。它只能注入其他 bean 類(lèi)型。當(dāng)有多個(gè)
??類(lèi)型匹配時(shí),使用要注入的對(duì)象變量名稱(chēng)作為 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到
就報(bào)錯(cuò)。
2.3.2.2 @Qualifier
作用:
??在自動(dòng)按照類(lèi)型注入的基礎(chǔ)之上,再按照 Bean 的 id 注入。它在給字段注入時(shí)不能獨(dú)立使用,必須和@Autowire 一起使用;但是給方法參數(shù)注入時(shí),可以獨(dú)立使用。
屬性:
??value:指定 bean 的 id。
2.3.2.3 @Resource
作用:
??直接按照 Bean 的 id 注入。它也只能注入其他 bean 類(lèi)型。
屬性:
??name:指定 bean 的 id。
2.3.2.4 @Value
作用:
??注入基本數(shù)據(jù)類(lèi)型和 String 類(lèi)型數(shù)據(jù)的
屬性:
??value:用于指定值
2.3.3 用于改變作用范圍的:
??相當(dāng)于:
2.3.3.1 @Scope
作用:
??指定 bean 的作用范圍。
屬性:
??value:指定范圍的值。
取值:singleton prototype request session globalsession
2.3.4 和生命周期相關(guān)的:(了解)
相當(dāng)于:
<bean id="" class="" init-method="" destroy-method="" />AccountServiceImpl.java
package com.itheima.service.impl;import com.itheima.dao.IAccountDao; import com.itheima.service.IAccountService; import org.springframework.stereotype.Service;import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource;/*** 賬戶(hù)的業(yè)務(wù)層實(shí)現(xiàn)類(lèi)** 曾經(jīng)XML的配置:* <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"* scope="" init-method="" destroy-method="">* <property name="" value="" | ref=""></property>* </bean>** 用于創(chuàng)建對(duì)象的* 他們的作用就和在XML配置文件中編寫(xiě)一個(gè)<bean>標(biāo)簽實(shí)現(xiàn)的功能是一樣的* Component:* 作用:用于把當(dāng)前類(lèi)對(duì)象存入spring容器中* 屬性:* value:用于指定bean的id。當(dāng)我們不寫(xiě)時(shí),它的默認(rèn)值是當(dāng)前類(lèi)名,且首字母改小寫(xiě)。* Controller:一般用在表現(xiàn)層* Service:一般用在業(yè)務(wù)層* Repository:一般用在持久層* 以上三個(gè)注解他們的作用和屬性與Component是一模一樣。* 他們?nèi)齻€(gè)是spring框架為我們提供明確的三層使用的注解,使我們的三層對(duì)象更加清晰*** 用于注入數(shù)據(jù)的* 他們的作用就和在xml配置文件中的bean標(biāo)簽中寫(xiě)一個(gè)<property>標(biāo)簽的作用是一樣的* Autowired:* 作用:自動(dòng)按照類(lèi)型注入。只要容器中有唯一的一個(gè)bean對(duì)象類(lèi)型和要注入的變量類(lèi)型匹配,就可以注入成功* 如果ioc容器中沒(méi)有任何bean的類(lèi)型和要注入的變量類(lèi)型匹配,則報(bào)錯(cuò)。* 如* 出現(xiàn)位置:* 可以是變量上,也可以是方法上* 細(xì)節(jié):* 在使用注解注入時(shí),set方法就不是必須的了。* Qualifier:* 作用:在按照類(lèi)中注入的基礎(chǔ)之上再按照名稱(chēng)注入。它在給類(lèi)成員注入時(shí)不能單獨(dú)使用。但是在給方法參數(shù)注入時(shí)可以(稍后我們講)* 屬性:* value:用于指定注入bea果Ioc容器中有多個(gè)類(lèi)型匹配時(shí):n的id。* Resource* 作用:直接按照bean的id注入。它可以獨(dú)立使用* 屬性:* name:用于指定bean的id。* 以上三個(gè)注入都只能注入其他bean類(lèi)型的數(shù)據(jù),而基本類(lèi)型和String類(lèi)型無(wú)法使用上述注解實(shí)現(xiàn)。* 另外,集合類(lèi)型的注入只能通過(guò)XML來(lái)實(shí)現(xiàn)。** Value* 作用:用于注入基本類(lèi)型和String類(lèi)型的數(shù)據(jù)* 屬性:* value:用于指定數(shù)據(jù)的值。它可以使用spring中SpEL(也就是spring的el表達(dá)式)* SpEL的寫(xiě)法:${表達(dá)式}** 用于改變作用范圍的* 他們的作用就和在bean標(biāo)簽中使用scope屬性實(shí)現(xiàn)的功能是一樣的* Scope* 作用:用于指定bean的作用范圍* 屬性:* value:指定范圍的取值。常用取值:singleton prototype** 和生命周期相關(guān) 了解* 他們的作用就和在bean標(biāo)簽中使用init-method和destroy-methode的作用是一樣的* PreDestroy* 作用:用于指定銷(xiāo)毀方法* PostConstruct* 作用:用于指定初始化方法*/ @Service("accountService") //@Scope("prototype") public class AccountServiceImpl implements IAccountService {// @Autowired // @Qualifier("accountDao1")@Resource(name = "accountDao2")private IAccountDao accountDao = null;@PostConstructpublic void init(){System.out.println("初始化方法執(zhí)行了");}@PreDestroypublic void destroy(){System.out.println("銷(xiāo)毀方法執(zhí)行了");}public void saveAccount(){accountDao.saveAccount();} }bean.xml(用xml方式配置)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 配置Service --><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"><!-- 注入dao --><property name="accountDao" ref="accountDao"></property></bean><!--配置Dao對(duì)象--><bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"><!-- 注入QueryRunner --><property name="runner" ref="runner"></property></bean><!--配置QueryRunner--><bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"><!--注入數(shù)據(jù)源--><constructor-arg name="ds" ref="dataSource"></constructor-arg></bean><!-- 配置數(shù)據(jù)源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!--連接數(shù)據(jù)庫(kù)的必備信息--><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property><property name="user" value="root"></property><property name="password" value="1234"></property></bean> </beans>bean.xml(用注解來(lái)實(shí)現(xiàn)對(duì)象的配置,注<context:component-scan 包含了要掃描的那些帶有注解的包)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--告知spring在創(chuàng)建容器時(shí)要掃描的包,配置所需要的標(biāo)簽不是在beans的約束中,而是一個(gè)名稱(chēng)為context名稱(chēng)空間和約束中--><context:component-scan base-package="com.itheima"></context:component-scan> </beans>總結(jié)
以上是生活随笔為你收集整理的spring学习笔记04-IOC常用注解(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: spring学习笔记06-spring整
- 下一篇: 写给女友的情诗