`
yinxuchina
  • 浏览: 43013 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

通过自动扫描方式把组件纳入spring容器中

阅读更多

在一个大型项目中,通常会有很多组件,如果采用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"       
       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.yx" />
</beans>

 

   @Service用于标注业务组件   @Controller用于标注控制层名称(比如acction)
   @Repository 标注DAO组件  @Component泛指组件,当组件不好归类的时候,可以用这个注解标注

 

@Service
//spring默认把类名作为bean id,并且把第一个字母变为小写这里即为personServiceImpl
//如果要指定bean id,使用@Service("XXX")即可。
//如果要修改bean的作用域,则使用@Scope("prototype")即可。
public class PersonServiceImpl implements PersonService {
	@Resource
	private BaseDao baseDao;
	 
   @PostConstruct  //@PostConstruct 指定初始化方法
	public void init(){ 
		System.out.println("初始化");
	}
	@PreDestroy//指定销毁方法,只有在bean作用域在单例模式下,才起作用
	public void destory(){
		System.out.println("销毁.........");
	}
	public void save(){
		baseDao.save();
	}

}

 
 测试

public class SpringTest {
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test 
	public void instanceSpring(){
		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService)ctx.getBean("personServiceImpl");
		personService.save();
		ctx.close();
	}
}

 

分享到:
评论
1 楼 422759366 2009-07-31  
怎么实现自动扫描呢? 可以讲解一下!

相关推荐

    Spring通过在classpath自动扫描方式把组件纳入spring容器中管理

    在Spring框架中,自动扫描(Auto-Component Discovery)是一种便捷的方式,它允许开发者无需显式配置每个bean,就能将类路径下(classpath)的特定包及其子包中的组件(即带有特定注解的类)纳入Spring容器进行管理...

    Spring的自动扫描注入.docx

    在 Spring 2.5 中,引入了组件自动扫描机制,该机制可以在类路径下寻找标注了 @Component、@Service、@Controller、@Repository 注解的类,并将这些类纳入 Spring 容器中管理。 @Component、@Repository、@Service...

    详解spring自动扫描包

    Spring框架中自动扫描包的机制是从Spring 2.5版本引入的,这个机制可以在类路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进Spring容器中管理。这个机制的作用和在XML...

    mybatis-spring-1.31.jar

    它能够自动将MyBatis的操作纳入Spring的全局事务管理中,实现事务的统一控制。这样,开发者无需关心事务的开启、提交、回滚等细节,只需要关注业务逻辑,提高了开发效率。 再者,MyBatis-Spring提供了...

    Spring注解@Component、@Repository、@Service、@Controller区别.doc

    Spring 注解@Component、@...在 Spring 2.5 及更高版本中,我们可以使用自动扫描机制来扫描组件,而不需要在 XML 文件中配置 Bean。我们可以使用元素来启用自动扫描机制,并指定要扫描的包和子包。 例如: ``` ...

    spring+maven项目工程

    此外,@Component、@Service、@Repository和@Controller等注解用于标记那些需要被Spring容器管理的类,使得它们能被自动扫描并纳入到Spring的应用上下文中。 接下来,"通过xml实现IOC"则是指通过Spring的XML配置...

    传智播客 Spring 完全文档 笔记总结

    ##### 2.10 自动扫描方式把组件纳入 Spring - **自动扫描**:通过使用 @ComponentScan 注解可以自动扫描指定包下的类,并将它们注册为 Spring Bean。 - **组件扫描的优点**:这种方式可以减少配置文件的数量,使...

    mybatis-spring-1.2.1.zip

    3. **MapperFactoryBean**: 这个Bean用于将MyBatis的Mapper接口实例化,并自动注入到Spring容器中。MapperFactoryBean会根据Mapper接口生成对应的MapperProxy,从而实现方法调用到SQL执行的映射。 4. **...

    spring2 整合 Dwr(把DWR的配置写到Spring的配置文件)

    - 集成Spring可以将DWR的配置纳入Spring的IoC容器,便于统一管理,减少重复代码。 - 利用Spring的AOP,可以实现对DWR方法的拦截和事务控制。 - 可以通过Spring的自动扫描功能,自动注册DWR的远程接口,简化配置。...

    17. Spring Boot普通类调用bean【从零开始学Spring Boot】

    在非Spring管理的类中,如果你想使用Spring容器中的bean,有以下几种方式: - 实现ApplicationContextAware接口,Spring会在初始化时自动注入ApplicationContext。 - 使用`@Resource`注解,与`@Autowired`类似,...

    spring+ajax+mybatis+springmvc笔记

    - **组件扫描**:Spring 容器启动后会扫描指定包下的所有类,如果类前有 `@Component` 等注解,则会将其纳入容器管理。 - **其他相关注解**: - `@Autowired` 和 `@Qualifier`:支持 setter 或构造器注入,用于...

    spring学习笔记

    Spring支持使用注解进行组件扫描,例如,使用`@Component`、`@Service`、`@Repository`和`@Controller`来标记不同层的组件,并通过`&lt;context:component-scan&gt;`标签来扫描指定包下的类,将它们纳入Spring容器管理。...

    Spring相关编码规范

    2. **组件纳入Spring容器管理**: - 对于控制器,推荐使用`@Controller`注解,表示该类是处理HTTP请求的组件。 - 对于服务层,推荐使用`@Service`注解。如果有接口,建议在实现类上添加`@Service`并指定别名,如`@...

    spring定时器实现源码

    如果你使用的是如 Tomcat 的 Servlet 容器,那么你需要在主类或启动配置中添加 `@ServletComponentScan` 注解,以扫描并注册 Servlet 组件。然后,通过 `mvn spring-boot:run` 或者 IDE 的相应配置启动项目,定时...

    spring-boot-test_springboot学习笔记

    3. **组件扫描**:`@ComponentScan`注解用于指定哪些包下的类会被扫描并纳入Spring容器,这样在测试中可以注入相关的bean。 4. **数据源配置**:在测试中,经常需要模拟数据库操作。Spring Boot通过`@DataJpaTest`...

    mybatis3+spring配置

    这意味着Spring容器将负责事务的开启、提交和回滚。 - **编程式事务管理**:除了容器管理的事务之外,还可以使用编程式事务管理。这需要使用Spring提供的`PlatformTransactionManager`接口来手动控制事务。 3. **...

    ibatis 与spring3整合

    整合Ibatis 和 Spring 的主要目标是利用Spring的管理能力,将Ibatis的SqlSessionFactory和SqlSessionTemplate等组件纳入到Spring的上下文中,实现事务管理和对象的生命周期管理。以下是一些关键步骤: 1. **添加...

    MyBatis-Spring结合

    - **注入映射器**:通过`MapperFactoryBean`注入映射器接口到Spring容器中。 - **自动配置**:当多个映射器需要被配置时,可以使用自动扫描机制简化配置。 3. **SqlSessionTemplate**与`SqlSessionDaoSupport`:...

    spring boot相关配置代码.zip

    - **Components and Beans**: 通过 @Component、@Service、@Repository 和 @Controller 注解,你可以声明 Spring Bean,它们会被自动扫描并纳入 Spring 容器管理。你可以使用 @Autowired 注解来注入依赖。 - **...

Global site tag (gtag.js) - Google Analytics