日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring学习使用标签来标记资源(@Component、@Repository、 @Service和@Controller)和用法(包括如何jsp正在使用)...

發布時間:2023/12/13 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的Spring学习使用标签来标记资源(@Component、@Repository、 @Service和@Controller)和用法(包括如何jsp正在使用)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。