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.mzph.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…