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文件变得十分简单,因此建议大家在以后的开发中使用注解。
分享到:
相关推荐
这份"Spring学习笔记+学习源码.zip"资源包含了深入学习Spring及其相关技术的知识点,以及实践代码,对提升Spring技能将大有裨益。 首先,我们来详细讨论Spring框架的主要组件和功能: 1. **依赖注入(Dependency ...
以上是 Spring 2.5.6 学习笔记中的关键知识点,通过这些基础知识的学习,开发者可以开始构建基于 Spring 框架的应用程序。接下来,可以进一步深入学习 Spring 的高级特性,如事务管理、安全性、Web 开发等方面的知识...
在本篇 Spring 学习笔记中,我们将探讨 Spring 的入门、优点、组成以及重要的IOC理论。 1. **Spring 简介** Spring 是一个开源的、免费的 Java 框架,它的目标是减少企业级开发的复杂性。它集成了许多现有的技术,...
### Spring学习笔记(精华全记录) #### Spring框架概述 Spring框架源自Rod Johnson的个人项目,最初于2002年末发布。Spring并非一开始就作为一个完整的框架出现,而是从一个项目逐步发展而来。随着项目的成熟,...
### Spring学习笔记知识点详解 #### 一、Spring框架概述 **Spring** 是一个开源的、分层的企业级应用开发框架,旨在简化Java EE应用程序的开发。它的主要目标是提高开发效率,减少耦合度,并提供一种更为简洁的...