Spring學習使用標簽來標記資源(@Component、@Repository、 @Service和@Controller)和用法(包括如何jsp正在使用)...

首先,在xml其中新增部分標有下劃線的文件,容器初始化的時候需要掃描包

?注意:

a.?????包款掃描(下劃線部分)一定要加,默認是不掃描整個包。與每一包之間’,’開。如過具有同樣的父包,那么我們能夠用父包來取代。例如以下劃線部分,我們能夠用com.bjsxt來取代。

<?

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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /><!-- 自己主動裝配一定要 加上此片段--> <context:component-scan base-package="com.bjsxt.dao.impl,com.bjsxt.service,com.bjsxt.model"></context:component-scan> </beans>



b.?????在2.5版本號中(@Component@Repository@Service@Controller)四個標簽代表同樣的含義,以后的版本號中可能會有差別。

c.????在標注組件等資源時候,盡量加上名稱比如@Component("stuDaoImpl")

默認的名稱是 類名首字母小寫。

<pre name="code" class="java">package com.bjsxt.dao.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.*;
import com.bjsxt.model.Student;
@Component("stuDaoImpl")
public class StudentDaoImpl implements StudentDao{@Overridepublic void StudentSave(Student s) {System.out.println("學生被保存!");}
}

 

d.????? 用法 在set方法或者屬性名前增加 @resource(name="stuDaoImpl"),推薦增加名稱,默認是依照類型進行查找。比如:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;import com.bjsxt.dao.*;
import com.bjsxt.dao.impl.*;
import com.bjsxt.model.*;
@Component("studentService")//聲明資源
public class StudentService {
private StudentDao studentDao;
public StudentDao getStudentDao() {return studentDao;
}
@Resource(name="stuDaoImpl")//注入
public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;
}
public void add(Student s)
{this.studentDao.StudentSave(s);
}

e.測試方式依舊和之前的注入方式一樣,注意這里的

Studentstudent=(Student)ctx.getBean("student");是我們用標簽在student類中聲明的@Component("student")。

Spring容器會能通過ctx(相當于beanFactory)找到。

package com.bjsxt.service;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.bjsxt.model.Student;public class StudentServiceTest {@Testpublic void test() {System.out.println("程序執行之前....");ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");System.out.println("程序執行開始....");StudentService sService=(StudentService)ctx.getBean("studentService");Student student=(Student)ctx.getBean("student");Student student2=(Student)ctx.getBean("student");System.out.println(student2==student);sService.add(student);}
}

f.?? 在jsp頁面須要處理業務,有java代碼須要spring注入service。

須要如今想要獲取的類前加標簽

@Component("docrelationService")首先jsp頁面導入

<%@page import="org.springframework.context.ApplicationContext"%>
<%@pageimport="org.springframework.web.context.support.WebApplicationContextUtils"%>

獲取spring注入

<%
????????ApplicationContext context =WebApplicationContextUtils
?????????????.getWebApplicationContext(application);

???????????DocrelationServicedocrelationService = (DocrelationService) context
?????????????.getBean("docrelationService");

%>

g.??

?假如類Aspring容器管理,在類B中引用A,假設想在B中的A是由spring容器創建的,有兩種方法:

?????a).B也由spring容器管理(即類B前有@compoment(b)標簽),并注入A,BeanFactory.getBean("b") 得到B實例,就可以(推薦).

?????b).B不由spring容器管理,在類B中用代碼?(ABeanFactory.getBean("a")得到A實例,就可以.(不推薦,會產生兩個beanFactory由于在類B中須要用BeanFactory,所以我們必須在Bnew一個)

c)B創建實例時候假設採用a)方法,決不能採用B?b=new?B();的方式,否則會報nullpoint異常。

d)ClassPathXmlApplicationContext建立在beanFactory基礎之上,非常少有人直接使用。

測試代碼:

package com.bjsxt.service;import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.bjsxt.model.Student;public class StudentServiceTest {@Testpublic void test() {System.out.println("程序執行之前....");//BeanFactory ctx=new ClassPathXmlApplicationContext("beans.xml");//ClassPathXmlApplicationContext建立在beanFactory基礎之上,非常少有人直接使用。

ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");//直接獲取spring容器 System.out.println("程序執行開始...."); StudentService sService=(StudentService)ctx.getBean("studentService");//StudentService相當于類B //StudentService sService=new StudentService(); Student student=(Student)ctx.getBean("student"); Student student2=(Student)ctx.getBean("student"); System.out.println(student2==student); sService.add(student); } }





版權聲明:本文博客原創文章,博客,未經同意,不得轉載。

轉載于:https://www.cnblogs.com/zfyouxi/p/4640478.html

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

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

相關文章

python 判斷字符串時是否是json格式方法

在實際工作中&#xff0c;有時候需要對判斷字符串是否為合法的json格式 解決方法使用json.loads,這樣更加符合‘Pythonic’寫法 代碼示例&#xff1a; Python import json def is_json(myjson):try:json_object json.loads(myjson)except ValueError, e:return Falsereturn Tr…

學習筆記(17):Python網絡編程并發編程-Process對象的其他屬性或方法

立即學習:https://edu.csdn.net/course/play/24458/296427?utm_sourceblogtoedu 1.pid與ppid&#xff1a;pid進程編碼&#xff0c;ppid進程的父進程編碼&#xff1b;os.getpid()查看正在運行的進程編碼&#xff0c;os.getppid()查看正在運行進程的父進程編碼 2.僵尸進程&…

用弦截法求一元三次方程的根x^3-5x^2+16x-80=0 ;帶注釋!

//用弦截法求一元三次方程的根x^3-5x^216x-800 #include<stdio.h>#include<math.h> float f(float x) //定義子函數f(x) x^3-5x^216x-80&#xff0c;當f(x) →0時&#xff0c;則x即為所求的實數根&#xff1b; { float y; y((x-5.0)*x16.0)*x-80.0; …

兩個很有用的進程間通信函數popen,pclose

兩個很有用的進程間通信函數popen,pclose 今天起的比較晚&#xff0c;然后來了也不想復習&#xff0c;還是看書學習--寫代碼--寫博客有意思&#xff0c;不敢說有多精通&#xff0c;至少每天都在學習新知識&#xff0c;不求立刻完全消化&#xff0c;但求每天有進步。 現在就看看…

c++中指針箭頭的用法

1、c中指針用箭頭來引用類或者結構體的成員&#xff0c;箭頭操作符“->”用來引用指針對象。這是是用于類&#xff0c;或者是結構體的指針變量用的。 如struct Point {int x,y;};Point *ptnew Point;pt->x1; 舉例子說明一下&#xff1a;比如&#xff0c;我有一個對象dark…

化零為整WCF(14) - 事務(Transaction)

[索引頁][源碼下載] 化零為整WCF(14) - 事務(Transaction)作者&#xff1a;webabcd介紹WCF(Windows Communication Foundation) - 事務(Transaction)&#xff1a; 對契約方法使用TransactionFlowAttribute聲明&#xff08;設置TransactionFlowOption參數&#xff09;&#x…

有限元分析筆記01-平面應力和平面應變

https://www.zhihu.com/question/30439292 http://blog.sina.cn/dpool/blog/s/blog_c4c804690102vqqs.html plate stress plate strain

MQTT-SN協議亂翻之實現要點

前言 本篇是MQTT-SN 1.2協議最后一篇翻譯了&#xff0c;主要涉及實現要點&#xff0c;很簡短。 需要支持QoS 值為 -1 QoS雖默認設置有0,1,2三個值&#xff0c;但還有一種情況其值為-1。來自客戶端的PUBLISH消息中若QoS為-1的情況下&#xff0c;此刻客戶端不會關心和網關有沒有建…

oracle-REDO日志文件分析(insert)

1:記錄當前scnselect dbms_flashback.get_system_change_number from dual;GET_SYSTEM_CHANGE_NUMBER------------------------11595722:創建表CREATE TABLE team (team_code VARCHAR2(3),team_name VARCHAR2(30),country_code VARCHAR2(3) );INSERT INTO team VALUES (M…

刪除修改bond

參考地址&#xff1a;http://www.111cn.net/sys/linux/79301.htm 四、刪除bonding設備 如由于最初配置的bonding設備取名為bond0&#xff0c;而后改名為了bond1&#xff0c;造成了兩個bonding設備的存在&#xff0c;現在需刪除bond0 。先查看下網絡設備&#xff1a; # ls /sys/…

學習筆記(18):Python網絡編程并發編程-守護進程

立即學習:https://edu.csdn.net/course/play/24458/296429?utm_sourceblogtoedu 守護進程&#xff08;了解&#xff09; 1.概念&#xff1a;守護進程是主進程在創建子進程的時候&#xff0c;將子進程設置成守護自己的進程&#xff0c;等主進程結束后&#xff0c;不管子進程的…

靜態頁面之間的轉發與json與ajax做到動態數據

我們見過很多使用jsp ,php,asp的動態網頁技術的網站了,我們知道如果一個網站內容更新頻率極低,而內容量不是十分龐大時,這樣的網站(一次開發完成后不會需要較多的維護成本)的完全可以使用全部使用靜態頁面來做,此時其實反而可以得到更好的效果(更快的響應時間(省掉了服務器各種…

數組的最后一位的下一位為什么是0?

以下是我做的兩個實驗&#xff0c;加證實了數組的最后一位的后一位是0&#xff0c;只應該是系統自動添加的標志位 1、比如 int a[5] 則a[5]0,這個是什么原因我還沒有搞懂 #include<iostream> using namespace std;int main() {int a[5];int *pa;for(int i0;i<5;i){a[i…

iOS開發網絡篇—NSURLConnection基本使用

iOS開發網絡篇—NSURLConnection基本使用 一、NSURLConnection的常用類 &#xff08;1&#xff09;NSURL&#xff1a;請求地址 &#xff08;2&#xff09;NSURLRequest&#xff1a;封裝一個請求&#xff0c;保存發給服務器的全部數據&#xff0c;包括一個NSURL對象&#xff0c;…

如何查看mysql連接相關參數

1.查看當前所有連接的詳細資料: mysqladmin -u root -ppassword processlist 這里password為數據庫用戶root的密碼 2.只查看當前連接數(Threads就是連接數.): mysqladmin -u root -ppassword status 這里password為數據庫用戶root的密碼 3.如何知道當前MySQL設置的并發連接數是…

學習筆記(19):Python網絡編程并發編程-互斥鎖

立即學習:https://edu.csdn.net/course/play/24458/296430?utm_sourceblogtoedu 1.互斥鎖&#xff1a; 多進程間的內存是相互隔離的&#xff0c;因此其數據也是相互隔離的&#xff0c;但是所有的進程都共享一個文件操作系統或者說共享文件處理器和打印端。而共享帶來的是競爭…

使用HTML5+CSS3制作圓角內發光按鈕----示例

<!doctype html> <html> <head> <meta charset"utf-8" /> <title>制作漂亮的圓角按鈕<title> <style type"text/css"> .loginBtnDiv { float:right; padding-right:50px; padding-top:10px; } .loginBtn, .Resg…

C++中的sort()函數的原形

1、sor(a,an,compare) {//前兩個是參數是待排序的數組首地址和尾地址 //最后一個參數是compare表示的比較類型 //可調用functional函數的less&#xff08;&#xff09;和greater&#xff08;&#xff09;函數比較大小}

鼠標放上超鏈接顯示背景效果

鼠標放上超鏈接顯示背景效果&#xff1a; <html> <head> <style type"text/css"> a.one:link {color: #ff0000} a.one:visited {color: #0000ff} a.one:hover {color: #ffcc00}a.two:link {color: #ff0000} a.two:visited {color: #0000ff} a.two:…

學習筆記(20):Python網絡編程并發編程-互斥鎖與join的區別

立即學習:https://edu.csdn.net/course/play/24458/296432?utm_sourceblogtoedu 互斥鎖與join的異同&#xff1a; 1.同&#xff1a;都是將多進程并發模式變成多進程串行&#xff0c;保證了數據的有序性 2.異&#xff1a; 互斥鎖只是對于進程的局部代碼實施串行執行變化&#x…