Spring学习之使用标签来标记资源(@Component、@Repository、 @Service和@Controller)以及使用方式(包含如何在jsp中使用)
2014-10-13 10:27
597 查看
首先要在xml文件当中加入标下划线的部分,容器初始化时候需要扫描的包
注意:
a. 扫描的包部分(下划线部分)一定要加上,默认是不会扫描所有的包的。各个包之间用’,’隔开。如过具有相同的父包,那么我们可以用父包来代替。如下划线部分,我们可以用com.bjsxt来代替。
b. 在2.5版本中(
e.测试方式依然和之前的注入方式一样,注意这里的
需要现在想要获取的类前加标签
@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");
%>
假如类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基础之上,很少有人直接使用。
测试代码:
注意:
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正在使用)
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
- spring注解-@Component(不推荐使用)、@Repository、@Service、@Controller
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
- Spring注解使用以及@Component、@Repository、@Service、@Controller区别
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- spring @component,@service,@controller,@repository等标签
- Spring学习笔记 关于Bean定义的重用-使用abstract以及parent标签属性
- Spring注解@Component、@Repository、@Service、@Controller使用
- Spring注解@Component、@Repository、@Service、@Controller区别在哪?
- Spring中用@Component、@Repository、@Service和 @Controller等标注的默认Bean名称会是小写开头的非限定类名
- Spring注解@Component、@Repository、@Service、@Controller
- java /Spring /jsp 使用input标签 file 如何上传多个文件
- SpringAnnotation注解之@Component,@Repository,@Service,@Controller
- Spring学习笔记 关于Bean定义的重用-使用abstract以及parent标签属性
- Spring的注解类(@Component、@Repository、@Service、@Controller )
- Spring注解@Component、@Repository、@Service、@Controller
- Spring注解@Component、@Repository、@Service、@Controller区别 .