Spring—注解開發

Spring原始注解

Spring是輕代碼而重配置的框架,配置比較繁重,影響開發效率,所以注解開發是一種趨勢,注解代替xml配置文 件可以簡化配置,提高開發效率。

  • @Component 使用在類上用于實例化Bean
  • @Controller 使用在web層類上用于實例化Bean
  • @Service 使用在service層類上用于實例化Bean
  • @Repository 使用在dao層類上用于實例化Bean
  • @Autowired 使用在字段上用于根據類型依賴注入
  • @Quali?er 結合@Autowired一起使用用于根據名稱進行依賴注入
  • @Resource 相當于@Autowired+@Quali?er,按照名稱進行注入
  • @Value 注入普通屬性
  • @Scope 標注Bean的作用范圍
  • @PostConstruct 使用在方法上標注該方法是Bean的初始化方法
  • @PreDestroy 使用在方法上標注該方法是Bean的銷毀方法

注意:
使用注解進行開發時,需要在applicationContext.xml中配置組件掃描,作用是指定哪個包及其子包下的Bean需要 進行掃描以便識別使用注解配置的類、字段和方法。

<?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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
"><context:component-scan base-package="com"/>
<context:property-placeholder location="jdbc.properties"/>
</beans>

使用@Compont或@Repository標識UserDaoImpl需要Spring進行實例化。

@Service("service")
@Scope("singleton")
public class service {@Autowired@Qualifier("impl")Dao dao;@Value("12")int no;@Value("${jdbc.username}")String name;public service(Dao dao) {this.dao = dao;}public service() {}public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");service i = (service) applicationContext.getBean("service");i.save();}public void save() {System.out.println(name);dao.save();}public void setDao(Dao dao) {this.dao = dao;}
}
@Repository("impl")
@Scope("singleton")
public class ImplDao implements Dao {public void save() {System.out.println("saving");}public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");ImplDao i = (ImplDao) applicationContext.getBean("impl");i.save();}
}

Spring新注解

使用上面的注解還不能全部替代xml配置文件,還需要使用注解替代的配置如下:

  • 非自定義的Bean的配置

  • 加載properties文件的配置:context:property-placeholder

  • 組件掃描的配置:context:component-scan

  • 引入其他文件:

  • @Con?guration 用于指定當前類是一個 Spring 配置類,當創建容器時會從該類上加載注解

  • @ComponentScan 用于指定 Spring 在初始化容器時要掃描的包。 作用和在 Spring 的 xml 配置文件中 的 <context:component-scan base-package=“com.itheima”/>一樣

  • @Bean 使用在方法上,標注將該方法的返回值存儲到 Spring 容器中

  • @PropertySource 用于加載.properties 文件中的配置

  • @Import 用于導入其他配置類

@Configuration
@ComponentScan("com")
@Import(DataSourceConfiguration.class)
public class SpringConfiguration {@Testpublic void test(){ApplicationContext applicationContext=new AnnotationConfigApplicationContext(SpringConfiguration.class);service service=(service) applicationContext.getBean("service");service.save();javax.sql.DataSource dataSource=(javax.sql.DataSource)applicationContext.getBean("datasource");try {System.out.println(dataSource.getConnection());} catch (SQLException throwables) {throwables.printStackTrace();}}
}
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {@Value("${jdbc.driver}")String clazz;@Value("${jdbc.url}")String url;@Value("${jdbc.username}")String user;@Value("${jdbc.password}")String password;@Bean("datasource")public ComboPooledDataSource testC3P0() throws Exception {ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();comboPooledDataSource.setDriverClass(clazz);comboPooledDataSource.setJdbcUrl(url);comboPooledDataSource.setUser(user);comboPooledDataSource.setPassword(password);return comboPooledDataSource;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391947.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391947.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391947.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

政府公開數據可視化_公開演講如何幫助您設計更好的數據可視化

政府公開數據可視化What do good speeches and good data visualisation have in common? More than you may think.好的演講和好的數據可視化有什么共同點&#xff1f; 超出您的想象。 Aristotle — the founding father of all things public speaking — believed that th…

C++字符串完全指引之一 —— Win32 字符編碼 (轉載)

C字符串完全指引之一 —— Win32 字符編碼原著&#xff1a;Michael Dunn翻譯&#xff1a;Chengjie Sun 原文出處&#xff1a;CodeProject&#xff1a;The Complete Guide to C Strings, Part I 引言  毫無疑問&#xff0c;我們都看到過像 TCHAR, std::string, BSTR 等各種各樣…

網絡計算機無法訪問 請檢查,局域網電腦無法訪問,請檢查來賓訪問帳號是否開通...

局域網電腦無法訪問&#xff0c;有時候并不是由于網絡故障引起的&#xff0c;而是因為自身電腦的一些設置問題&#xff0c;例如之前談過的網絡參數設置不對造成局域網電腦無法訪問。今天分析另一個電腦設置的因素&#xff0c;它也會導致局域網電腦無法訪問&#xff0c;那就是賓…

unity中創建游戲場景_在Unity中創建Beat Em Up游戲

unity中創建游戲場景Learn how to use Unity to create a 3D Beat Em Up game in this full tutorial from Awesome Tuts. 在Awesome Tuts的完整教程中&#xff0c;了解如何使用Unity來創建3D Beat Em Up游戲。 This tutorial covers everything you need to know to make a …

雷軍的金山云D輪獲3億美元!投后估值達19億美金

12月12日&#xff0c;雷軍旗下金山云宣布D輪完成3億美元融資&#xff0c;金額為云行業單輪融資最高。至此金山云投后估值達到19億美元&#xff0c;成為國內估值最高的獨立云服務商。金山集團相關公告顯示&#xff0c;金山云在本輪融資中總計發行3.535億股D系列優先股。驪悅投資…

轉:利用深度學習方法進行情感分析以及在海航輿情云平臺的實踐

http://geek.csdn.net/news/detail/139152 本文主要為大家介紹深度學習算法在自然語言處理任務中的應用——包括算法的原理是什么&#xff0c;相比于其他算法它具有什么優勢&#xff0c;以及如何使用深度學習算法進行情感分析。 原理解析 在講算法之前&#xff0c;我們需要先剖…

消費者行為分析_消費者行為分析-是否點擊廣告?

消費者行為分析什么是消費者行為&#xff1f; (What is Consumer Behavior?) consumer behavior is the study of individuals, groups, or organizations and all the activities associated with the purchase, use, and disposal of goods and services, and how the consu…

Spring—集成Junit

Spring集成Junit步驟 ①導入spring集成Junit的坐標 ②使用Runwith注解替換原來的運行期 ③使用ContextCon?guration指定配置文件或配置類 ④使用Autowired注入需要測試的對象 ⑤創建測試方法進行測試 ①導入spring集成Junit的坐標 <dependency> <groupId>org.s…

計算機的微程序存放在dram,計算機組成與結構

計算機組成與結構A/B卷填空1. 原碼一位乘法中&#xff0c;符號位與數值位(分開計算)&#xff0c;運算結果的符號位等于(相乘兩數符號位的異或值)。2. 微程序&#xff0c;微指令只存放在只讀存儲器中。3. 輔助磁道被分為若干個扇區4. 總線數據傳輸方式&#xff1a;_串行_,_并行_…

python算法面試_求職面試的Python算法

python算法面試During software job interviews, candidates often have to solve algorithm challenges. In this video from CupOfCode01, you will learn about common algorithm concepts in Python and how to solve algorithm challenges you may encounter in an interv…

vue實用難點講解

此篇文章是我基于研究vue文檔三遍的基礎上&#xff0c;覺得還有點難理解或者難記的知識點總結 列表渲染 1.渲染組件必須加key&#xff0c;并且屬性是手動傳遞給組件的<my-componentv-for"(item, index) in items"v-bind:item"item"v-bind:index"in…

leetcode 1208. 盡可能使字符串相等(滑動窗口)

給你兩個長度相同的字符串&#xff0c;s 和 t。 將 s 中的第 i 個字符變到 t 中的第 i 個字符需要 |s[i] - t[i]| 的開銷&#xff08;開銷可能為 0&#xff09;&#xff0c;也就是兩個字符的 ASCII 碼值的差的絕對值。 用于變更字符串的最大預算是 maxCost。在轉化字符串時&a…

魅族mx5游戲模式小熊貓_您不知道的5大熊貓技巧

魅族mx5游戲模式小熊貓重點 (Top highlight)I’ve been using pandas for years and each time I feel I am typing too much, I google it and I usually find a new pandas trick! I learned about these functions recently and I deem them essential because of ease of u…

可行性分析報告

1 引言1.1 編寫目的&#xff1a;闡明編寫可行性研究報告的目的&#xff0c;提出讀者對象。1.2 項目背景&#xff1a;應包括● 所建議開發軟件的名稱● 項目的任務提出者、開發者、用戶及實現軟件的單位● 項目與其他軟件或其他系統的關系。1.3 定義&#xff1a;列出文檔中用到的…

(Python的)__ name__中包含什么?

_名稱_變量及其在Python中的用法簡介 (An introduction to the _ _name_ _ variable and its usage in Python) You’ve most likely seen the __name__ variable when you’ve gone through Python code. Below you see an example code snippet of how it may look:通過Pytho…

畢業論文計算機附錄模板,畢業論文格式是什么,附錄又是什么?

畢業論文格式是什么&#xff0c;附錄又是什么?附錄對論文內用起到一個補充說明的作用&#xff0c;附錄應屬于論文的正文&#xff0c;有的論文需要寫明&#xff0c;有的論文可能不需要寫&#xff0c;大多數情況是不需要寫的&#xff0c;附錄的位置一般放在論文的結尾處&#xf…

文件上傳速度查詢方法

由于業務遷移&#xff0c;需要將大量文件拷貝到目標機器上的/mnt目錄&#xff0c;在拷貝過程中&#xff0c;想要查看上傳的速度&#xff0c;做法如下&#xff1a;[rootmail01 ~]# du -sh /mnt5.6G /mnt[rootmail01 ~]# watch -n1 du -sm /mnt/ #會出現下面的一屏現象 …

spring—AOP 的動態代理技術

AOP 的動態代理技術 常用的動態代理技術 JDK 代理 : 基于接口的動態代理技術 cglib 代理&#xff1a;基于父類的動態代理技術 JDK 代理 public class proxy {Testpublic void test() {final ImplDao dao new ImplDao();Dao pro (Dao) Proxy.newProxyInstance(ImplDao.cl…

非常詳細的Django使用Token(轉)

基于Token的身份驗證 在實現登錄功能的時候,正常的B/S應用都會使用cookiesession的方式來做身份驗證,后臺直接向cookie中寫數據,但是由于移動端的存在,移動端是沒有cookie機制的,所以使用token可以實現移動端和客戶端的token通信。 驗證流程 整個基于Token的驗證流程如下: 客戶…

Java中獲取完整的url

HttpServletRequest httpRequest(HttpServletRequest)request; String strBackUrl "http://" request.getServerName() //服務器地址 ":" request.getServerPort() //端口號 httpRequest.getContextPath() //項目名稱 httpRequ…