package com.szy.spring.service;
import org.springframework.stereotype.Service;
import com.szy.spring.dao.PersonDao;
@Service("service")
public class UserServiceImpl implements UserService
{
private PersonDao personDaoBean;
public void show()
{
personDaoBean.show();
}
public void setPersonDaoBean(PersonDao personDaoBean)
{
this.personDaoBean = personDaoBean;
}
}
在前面的例子中,都是使用XML的bean定义来使用组件,在大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会使配置文件显得很臃肿,查找和维护起来不方便。Spring2.5为我们引入了组件自动扫描机制,它可以在类路径下寻找标记了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入到spring容器中管理,它的作用和在xml中使用bean节点配置组件一样。要使用自动扫描机制,我们需要把配置文件如下配置:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="com.szy.spring"></context:component-scan>
</beans>
其中base-package为需要扫描的包(包括子包)
@Service用于标注业务层的组件,@Controller用于标注控制层组件(如struts中的action),@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。但是在目前的spring版本中,这几个注解的作用是一样的,但是在以后可能会进行区分。
下面把先前的例子修改一下:
首先是PersonDaoBean类,修改如下
package com.szy.spring.dao;
import org.springframework.stereotype.Repository;
@Repository
//告诉spring这个类要交给spring管理,
public class PersonDaoBean implements PersonDao
{
public void show()
{
System.out.println("执行PersonDaoBean中的add()方法");
}
}
然后是UserServiceImpl类
package com.szy.spring.service;
import org.springframework.stereotype.Service;
import com.szy.spring.dao.PersonDao;
@Service
//把这个类交给spring管理,作为服务了。
public class UserServiceImpl implements UserService
{
private PersonDao personDaoBean;
public void show()
{
personDaoBean.show();
}
public void setPersonDaoBean(PersonDao personDaoBean)
{
this.personDaoBean = personDaoBean;
}
public PersonDao getPersonDaoBean()
{
return personDaoBean;
}
}
下面我们进行测试,原来的测试代码是userServiceImpl
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service=(UserService)ctx.getBean("userService");
service.show();
其中userService是我们在配置文件中配置的bean的id。但是如今我们并没有id这个属性,在spring2.5中,默认的id是类的名称,但是开后是小写,也就是userServiceImpl,因此测试代码应修改如下:
AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service=(UserService)ctx.getBean("userServiceImpl");
System.out.println(service);
如果我们想自己命名的话,则只需在注解后加上括号,里面写入你希望的名字,如
@Service("userService")。
在spring中默认的是之生成一个bean实例,如果我们想每次调用都产生一个实例,则标注需如下配置
@Service @Scope("prototype")
在xml中我们还可以配置初始化方法和销毁方法,使用标注后只需如下标注
@PostConstruct
public void init()
{
System.out.println("初始化");
}
@PreDestroy
public void destory()
{
System.out.println("销毁");
}
使用注解后,我们的xml文件变得十分简单,因此建议大家在以后的开发中使用注解。
分享到:
相关推荐
JavaEE5学习笔记05-EJB之会话Bean总结...
(三) XML文件形式配置Spring事务管理 37 四、 HibernateTemplate 38 (一) HibernateTemplate 38 (二) HibernateDaoSupport 39 第十一课:Spring整合-SSH 40 一、 第一步:加入jar包(需要的jar包列表) 40 二、 第二步...
《Spring技术内幕》学习笔记2——IoC定位Bean定义资源 《Spring技术内幕》学习笔记3——IoC容器载入Bean定义资源文件 《Spring技术内幕》学习笔记4——IoC容器解析Bean定义资源并注册解析后的Bean 《Spring技术...
springboot学习笔记 spring基础 Spring概述 Spring的简史 xml配置 注解配置 java配置 Spring概述 Spring的模块 核心容器CoreContainer Spring-Core Spring-Beans ...
Java-Spring-SpringIoC容器-SpringIoC的学习 SpringIoC容器的学习笔记 主要介绍了IoC容器工作原理以及如何配置IoC容器 通过<bean>标签和注解配置容器
内容概要:学习Spring的一些学习笔记,主要学习Spring 框架两大核心机制(IoC、AOP) 笔记大纲:阅读笔记可以学习了解一下内容 如何使用 IoC ;配置文件;IoC 底层原理;通过运行时类获取 bean;通过有参构造创建 ...
ean在Spring和SpringMVC中无所不在,将这个概念内化很重要,所以下面这篇文章主要给大家介绍了关于Spring学习笔记之bean基础的相关资料,文中通过示例代码介绍的非常详解,需要的朋友可以参考下。
(三) XML文件形式配置Spring事务管理 37 四、 HibernateTemplate 38 (一) HibernateTemplate 38 (二) HibernateDaoSupport 39 第十一课:Spring整合-SSH 40 一、 第一步:加入jar包(需要的jar包列表) 40 二、 第二步...
## Spring入门学习 首先认识下Spring的结构 ![架构图](http://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/image001.gif) 然后我们皆可以写我们的demo了 ### 我们的Bean类 对于bean的理解,希望...
记录自己关于Spring的学习笔记还有Demo ,笔记均在Demo注释部分,后续会整理成MarkDown格式的文件发布在博客上。 如果觉得对您有帮助,您的Start就是对我最大的认同。 欢迎互相学习交流,如果项目中有问题的部分可以...
"spring课堂笔记.docx" 是一份关于 Spring 框架的课堂笔记,它提供了对 Spring 框架的深入理解和学习资源。以下是对这份课堂笔记的大致描述: "spring课堂笔记.docx" 是一份详尽的学习材料,旨在帮助开发者掌握 ...
Hibernate: 第一课:第一个hibernate项目 第二课:测试实体对象的生命周期 ...第三课:spring Bean的作用域 第四课:spring对AOP的只是(采用Annotation的方式) 第五课:spring对AOP的只是(采用配置文件的方式)
Spring Bean是Spring应用中最最重要的部分了。下面这篇文章主要给大家介绍了关于Spring学习笔记之bean生命周期的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
Spring是一个轻量级的DI/IoC和AOP容器框架。存在的目的是用于构建轻量级的J2EE应用。 轻量级:应用大小和应用开支,包括应用方式 依赖注入DI/IoC控制反转:提供松耦合的一种实现技术 AOP面向切面编程:(可以在不...
Spring学习笔记,主要是在tomcat环境下spring上下文的启动,初始化,bean配置的载入等
Spring Boot 学习笔记,包括底层实现原理及代码实战,非常齐全,助你快速打通 Spring Boot 的各个环节。 1. Spring boot helloworld 2. Spring boot 返回 json 数据 3. Spring boot 使用其他 json 转换框架 4. ...
依赖关系的管理:以后都交给spring来维护 在当前类需要用到其他类的对象时,由spring为我们提供,我们只需在配置文件中说明 依赖关系的维护:就称之为依赖注入 依赖注入: 注入方式:有三种 第一种:使用构造...
这是我自学spring的时候,在网上找的各种文档,其中包含的内容包括AOP,ApplicationContext,Bean,ContextLoadListener,MVC等内容,还有一些综合文档。个人感觉资源分为三分一点都不过分,肯定值。