spring学习笔记04-IOC常用注解(一)
文章目錄
- 2.3常用注解
- 2.3.1 用于創建對象的
- 2.3.1.1 @Component
- 2.3.1.2 @Controller @Service @Repository
- 2.3.2 用于注入數據的
- 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 和生命周期相關的:(了解)
- bean.xml(用xml方式配置)
- bean.xml(用注解來實現對象的配置,注
2.3常用注解
2.3.1 用于創建對象的
相當于:
<bean id="" class="">2.3.1.1 @Component
作用:
??把資源讓 spring 來管理。相當于在 xml 中配置一個 bean。
屬性:
??value:指定 bean 的 id。如果不指定 value 屬性,默認 bean 的 id 是當前類的類名。首字母小寫。
2.3.1.2 @Controller @Service @Repository
??他們三個注解都是針對一個的衍生注解,他們的作用及屬性都是一模一樣的。
??他們只不過是提供了更加明確的語義化。
@Controller:一般用于表現層的注解。
@Service:一般用于業務層的注解。
@Repository:一般用于持久層的注解。
細節:如果注解中有且只有一個屬性要賦值時,且名稱是 value,value 在賦值是可以不寫。
2.3.2 用于注入數據的
相當于:
<property name="" ref=""> <property name="" value="">2.3.2.1 @Autowired
作用:
??自動按照類型注入。當使用注解注入屬性時,set 方法可以省略。它只能注入其他 bean 類型。當有多個
??類型匹配時,使用要注入的對象變量名稱作為 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到
就報錯。
2.3.2.2 @Qualifier
作用:
??在自動按照類型注入的基礎之上,再按照 Bean 的 id 注入。它在給字段注入時不能獨立使用,必須和@Autowire 一起使用;但是給方法參數注入時,可以獨立使用。
屬性:
??value:指定 bean 的 id。
2.3.2.3 @Resource
作用:
??直接按照 Bean 的 id 注入。它也只能注入其他 bean 類型。
屬性:
??name:指定 bean 的 id。
2.3.2.4 @Value
作用:
??注入基本數據類型和 String 類型數據的
屬性:
??value:用于指定值
2.3.3 用于改變作用范圍的:
??相當于:
2.3.3.1 @Scope
作用:
??指定 bean 的作用范圍。
屬性:
??value:指定范圍的值。
取值:singleton prototype request session globalsession
2.3.4 和生命周期相關的:(了解)
相當于:
<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;/*** 賬戶的業務層實現類** 曾經XML的配置:* <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"* scope="" init-method="" destroy-method="">* <property name="" value="" | ref=""></property>* </bean>** 用于創建對象的* 他們的作用就和在XML配置文件中編寫一個<bean>標簽實現的功能是一樣的* Component:* 作用:用于把當前類對象存入spring容器中* 屬性:* value:用于指定bean的id。當我們不寫時,它的默認值是當前類名,且首字母改小寫。* Controller:一般用在表現層* Service:一般用在業務層* Repository:一般用在持久層* 以上三個注解他們的作用和屬性與Component是一模一樣。* 他們三個是spring框架為我們提供明確的三層使用的注解,使我們的三層對象更加清晰*** 用于注入數據的* 他們的作用就和在xml配置文件中的bean標簽中寫一個<property>標簽的作用是一樣的* Autowired:* 作用:自動按照類型注入。只要容器中有唯一的一個bean對象類型和要注入的變量類型匹配,就可以注入成功* 如果ioc容器中沒有任何bean的類型和要注入的變量類型匹配,則報錯。* 如* 出現位置:* 可以是變量上,也可以是方法上* 細節:* 在使用注解注入時,set方法就不是必須的了。* Qualifier:* 作用:在按照類中注入的基礎之上再按照名稱注入。它在給類成員注入時不能單獨使用。但是在給方法參數注入時可以(稍后我們講)* 屬性:* value:用于指定注入bea果Ioc容器中有多個類型匹配時:n的id。* Resource* 作用:直接按照bean的id注入。它可以獨立使用* 屬性:* name:用于指定bean的id。* 以上三個注入都只能注入其他bean類型的數據,而基本類型和String類型無法使用上述注解實現。* 另外,集合類型的注入只能通過XML來實現。** Value* 作用:用于注入基本類型和String類型的數據* 屬性:* value:用于指定數據的值。它可以使用spring中SpEL(也就是spring的el表達式)* SpEL的寫法:${表達式}** 用于改變作用范圍的* 他們的作用就和在bean標簽中使用scope屬性實現的功能是一樣的* Scope* 作用:用于指定bean的作用范圍* 屬性:* value:指定范圍的取值。常用取值:singleton prototype** 和生命周期相關 了解* 他們的作用就和在bean標簽中使用init-method和destroy-methode的作用是一樣的* PreDestroy* 作用:用于指定銷毀方法* 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("初始化方法執行了");}@PreDestroypublic void destroy(){System.out.println("銷毀方法執行了");}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對象--><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"><!--注入數據源--><constructor-arg name="ds" ref="dataSource"></constructor-arg></bean><!-- 配置數據源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!--連接數據庫的必備信息--><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(用注解來實現對象的配置,注<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在創建容器時要掃描的包,配置所需要的標簽不是在beans的約束中,而是一個名稱為context名稱空間和約束中--><context:component-scan base-package="com.itheima"></context:component-scan> </beans>總結
以上是生活随笔為你收集整理的spring学习笔记04-IOC常用注解(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring学习笔记06-spring整
- 下一篇: JS省市二级联动