首先,在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{
@Override
public 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 { @Test
public 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.  

假如类A由spring容器管理,在类B中引用A,假设想在B中的A是由spring容器创建的,有两种方法:

a).类B也由spring容器管理(即类B前有@compoment(“b”)标签),并注入A,用BeanFactory.getBean("b")
得到B实例,就可以(推荐).

b).类B不由spring容器管理,在类B中用代码 (A)BeanFactory.getBean("a")得到A实例,就可以.(不推荐,会产生两个beanFactory由于在类B中须要用BeanFactory,所以我们必须在B中new一个)

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 { @Test
public 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);
} }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Spring学习使用标签来标记资源(@Component、@Repository、 @Service和@Controller)和用法(包括如何jsp正在使用)的更多相关文章

  1. @Component, @Repository, @Service,@Controller的区别

    @Component, @Service, @Controller, @Repository是spring注解,注解后可以被spring框架所扫描并注入到spring容器来进行管理 @Componen ...

  2. [转]what’s the difference between @Component ,@Repository & @Service annotations in Spring

    原文地址:https://www.cnblogs.com/softidea/p/6070314.html @Component is equivalent to <bean> @Servi ...

  3. What's the difference between @Component, @Repository & @Service annotations in Spring?

    @Component is equivalent to <bean> @Service, @Controller , @Repository = {@Component + some mo ...

  4. 【转载】@Component, @Repository, @Service的区别

    @Component, @Repository, @Service的区别 官网引用 引用spring的官方文档中的一段描述: 在Spring2.0之前的版本中,@Repository注解可以标记在任何 ...

  5. Spring注解的使用和区别:@Component、@Service、@Repository、@Controller

    Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring ...

  6. @Component @Repository @Service @Controller

    Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring ...

  7. SpringAnnotation注解之@Component,@Repository,@Service,@Controller

    @Component:组件,表示此写上了此注解的bean,作为一个组件存在于容器中.这样的话别的地方就可以使用@Resource这个注解来把这个组件作为一个资源来使用了.初始化bean的名字为类名首字 ...

  8. Spring security (一)架构框架-Component、Service、Filter分析

    “致"高级"工程师(BUG工程师) 一颗折腾的心

  9. @Component, @Repository, @Service的区别

    注解 含义 @Component 最普通的组件,可以被注入到spring容器进行管理 @Repository 作用于持久层 @Service 作用于业务逻辑层 @Controller 作用于表现层(s ...

随机推荐

  1. 举例说, Decorator模式(Decorator Pattern)

    前言    在食品工业中的装饰图案具有比较广泛的应用,大多数的两个图案和在网上的例子饮食相关的,一旦被称为电影的手表,点咖啡要加糖要加奶昔要加这加那的时候.感觉好有派~好高大上啊~.为啥我在小卖部都是 ...

  2. hdu 1298 T9(特里+DFS)

    pid=1298" target="_blank" style="">题目连接:hdu 1298 T9 题目大意:模拟手机打字的猜想功能.依据概 ...

  3. 不一样的味道--Html和Xml解析、格式、遍历

    很多其它内容查看官网:http://www.tinygroup.org TinyXmlParser一切以简单.有用.高速为主. 演示样例1:Xml字符串解析 比方,我们要解析一段Xml字符串,简单例如 ...

  4. 2014年10本月微软MVP应用程序启动!

     2014年10本月微软MVP启动应用程序!    CSDN与微软合作,长期为用户提供申请"微软最有价值专家"的平台,希望有兴趣.资历的朋友以及正在朝这个方向努力的朋友可以积极參与 ...

  5. hdu 4691 最长的共同前缀 后缀数组 +lcp+rmq

    http://acm.hdu.edu.cn/showproblem.php? pid=4691 去年夏天,更多的学校的种族称号.当时,没有后缀数组 今天将是,事实上,自己的后缀阵列组合rmq或到,但是 ...

  6. 多快好省的做个app开发

    从技术经理的角度算一算,如何可以多快好省的做个app [导读]前端时间,一篇“从产品经理的角度算一算,做个app需要多少钱”的文章在网上疯传,可见大家对互联网创业的热情!这次,从一名技术经理的角度再给 ...

  7. Unity3d C# Socket 下载文件 (同步到)

    续篇 Unity3d C# HttpWebRequest 异步下载文件 ,由于project编译为IL2CPP的情况下仍然无效.提示HttpWebrequest 在当前版本号不支持.所以还是寻求其他的 ...

  8. Code Review中的几个提示

    原文:http://coolshell.cn/?p=1302  酷壳 Code Review中的几个提示 陈皓 Code Review应该是软件project最最有价值的一个活动,之前,本站发表过&l ...

  9. python有些错误换行问题解决

    有时候数据会遇到一些错误包.例如,正确的数据应: 20141010,aaa,bbb,ccc,ddd,eee 但实际的数据是来: 20141010,aaa,bbb, ccc,ddd, eee 这样出现错 ...

  10. HDU 4916 Count on the path

    意甲冠军: 考虑到一棵树,m询价  不要求回答每一次询价u和v通过在两个节点形成的最低等级点路径 思路: 一開始以为是LCA-  只是T了好几次-  后来发现不用LCA也可做 考虑每一个询问u和v   ...