spring筆記2-註解,spring2-註解

來源:互聯網
上載者:User

spring筆記2-註解,spring2-註解

一.屬性與成員變數的區別:
屬性:對外暴露的,getxxx/setxxx稱為屬性;
成員變數:private String name稱為成員變數或欄位

二.applicationContext.xml的書寫
<!--約束-->
<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-4.2.xsd
                     http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
</beans>                   

<!--在spring的設定檔中開啟spring對註解ioc的支援,指定spring初始化時要掃描的包—>
<context:component-scan base-package="com.itheima"></context:component-scan>

<!--讀取資料庫設定檔—>
<context:property-placeholder location="classpath:db.properties"/>
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     <property name="driverClass" value="${jdbc.driverClass}"></property>
     <property name="jdbcUrl" value="${jdbc.url}"></property>
     <property name="user" value="${jdbc.user}"></property>
     <property name="password" value="${jdbc.password}"></property>
</bean>


三.註解釋義
@component:把資源讓spring來管理。相當於在xml中配置一個bean。如果不指定value屬性,預設bean的id是當前類的類名。首字母小寫。      
@Controller:一般用於表現層的註解。
@Service:一般用於業務層的註解
@Repository :一般用於持久層的註解
細節:如果註解中有且只有一個屬性要賦值時,且名稱是value,value在賦值是可以不寫。

@Autowired:
作用:自動按照類型注入。
當使用註解注入屬性時,set方法可以省略。它只能注入其他bean類型。當有多個類型匹配時,使用要注入的物件變數名稱作為bean的id,在spring容器尋找,找到了也可以注入成功。找不到就報錯。

@Qualifier:在自動按照類型注入的基礎之上,再按照Bean的id注入。它在給欄位注入時不能獨立使用,必須和@Autowire一起使用;但是給方法參數注入時,可以獨立使用。

@Resource:直接按照Bean的id注入。它也只能注入其他bean類型。

@Value:注入基礎資料型別 (Elementary Data Type)和String類型資料的

@Scope:指定bean的作用範圍。value:指定範圍的值。取值:singleton  prototype request session globalsession

@PostConstruct:用於指定初始化方法。

@PreDestroy:用於指定銷毀方法

@Configuration:用於指定當前類是一個配置類,會從該類上載入註解。讀取該類上@ ComponentScan註解初始化spring容器。

@ComponentScan:用於指定spring在初始化容器時要掃描的包,(xml中需要basePackages屬性,用於指定要掃描的包)。和該註解中的value屬性作用一樣。

@PropertySource:用於載入.properties檔案中的配置,value[]:用於指定properties檔案位置。如果是在類路徑下,需要寫上classpath:

@Import:用於匯入其他配置類,value[]:用於指定其他配置類的位元組碼。

@Bean:該註解只能寫在方法上,表明使用此方法建立一個對象,並且交給spring管理。name:給當前@Bean註解方法建立的對象指定一個名稱(即bean的id)。

@RunWith註解替換原有運行器;@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration指定spring設定檔的位置;@ContextConfiguration(locations={"classpath:bean.xml"})

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.