`

Spring基础入门三:基于@Component,@Service,@Controller,@Repositroy自动扫描方式把组件纳入Spring容器中管理

阅读更多

使用@Component,@Service,@Controller,@Repositroy可以完成不用XML配置也可以将Java组件交给Spring容器

JavaBean代码如下:

package autoScan.dao;

public interface StudentDao {
	public abstract void add();
}

 

@Repository
@Scope("singleton")
public class StudentDaoImple implements StudentDao {
	
	// @PostConstruct用于配置初始化方法与XML中的init-method作用一致
	@PostConstruct
	public void myInit() {
		System.out.println("初始化方法");
	}
	
	// @PreDestroy用于配置销毁方法与XML中的destroy-method作用一致
	@PreDestroy
	public void myDestroy() {
		System.out.println("销毁方法");
	}
	
	public void add() {
		System.out.println("autoScan.dao.imple.StudentDaoImple的add方法");
	}
}
 

 

package autoScan.service;

public interface StudentService {
	public abstract void save();
}
 
@Service("stuService")
public class StudentServiceImple implements StudentService {
	
	@Resource(name = "studentDaoImple")
	private StudentDao stuDao;
	
	public void setStuDao(StudentDao stuDao) {
		this.stuDao = stuDao;
	}

	public void save() {
		stuDao.add();
	}
}

 那么XML中只需配置<context:component-scan>即可如下:

<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">

	<!-- 通过在classpath自动扫描方式把组件纳入Spring容器中管理 -->
	<context:component-scan base-package="autoScan.dao"></context:component-scan>
	<context:component-scan base-package="autoScan.service"></context:component-scan>
	
</beans>

 说明:

/**
 * 通过在classpath自动扫描方式把组件纳入Spring容器中管理
 * 
 * @author 张明学 2010-6-3下午09:52:40
 */
public class AutoScanTest {
	public static void main(String[] args) {
		// 通过<context:component-scan base-package="autoScan.dao"/>将base-package内
		// 寻找标注了@Component,@Service,@Controller,@Repositroy的bean纳入Spring的容器中
		// @Repositroy:用于标注数据访问组件
		// @Service:用于标注业务层
		// @Controller:用于标注控制器
		// @Component:用于标注其它的组件 (暂时这四个注解功能上没有什么区别)
		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans5.xml");
		// Bean的id默认为类名第一个字母小写,也可以@Service("stuService")进行修改
		// 也可以通过@Scope("prototype")指定bean的作用域
		// 也可以通过@PostConstruct指定初始化方法
		// 也可以通过@PreDestroy指定销毁方法
		System.out.println(ctx.getBean("studentDaoImple"));
		System.out.println(ctx.getBean("stuService"));

		StudentService stuService = (StudentService) ctx.getBean("stuService");
		System.out.println(stuService);

		System.out.println(ctx.getBean("studentDaoImple") == ctx
				.getBean("studentDaoImple"));
		ctx.close();
	}
}
 
1
2
分享到:
评论
1 楼 菜菜菜 2010-06-05  
根据测试,需要在StudentDaoImple类前加注释@Service("studentDaoImple")

相关推荐

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

    Spring注解 @Component、@Repository、@Service、@Controller区别,有兴趣的可能看一下。

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

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

    Spring核心注解深入解析:提升开发效率

    @Component 和其派生注解(@Repository、@Service、@Controller)标记类为Spring组件,允许Spring通过类路径扫描自动检测和配置这些类。 @Autowired 注解用于自动注入依赖,它可以放置在字段、构造器、setter方法或...

    基于框架的Web开发-装配Bean自动装配.doc

    组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean。 自动装配(autowiring):Spring自动满足bean之间的依赖。 1 使用@Component定义bean 在类声明的前面使用@Component对类进行标注,这...

    Spring注释 注入方式源码示例,Annotation

    凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处需要注入对象,所以用@Resource就有了ref的功效。 要用注解注入方式...

    Spring注解 - 52注解 - 原稿笔记

    在火狐中显示可能会有问题,大家都是程序员,改个参数就好啦 注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before ,...

    Spring @讲义.txt

    Spring @讲义.txt Spring注解@Component、@Repository、@Service、@Controller区别

    基于注解的DI.docx

    常用注解: @Component、创建对象 ...@Autowired、spring框架中引用类型赋值的注解,支持byName、byType,默认byType @Resource、jdk中的注解,使用自动注入给引用数据类型赋值,支持byName、byType,默认byName

    华为技术专家整理Spring Boot 注解大全.docx

    其中 @ComponentScan 让 spring Boot 扫描到 Configuration 类并把它加入到程序上下文。 @Configuration U等同于 spring 的 XML 配置文件;使用 Java 代码可以检查类型安全。 @EnableAutoConfiguration 自动配置。...

    Spring组件自动扫描详解及实例代码

    Spring组件自动扫描详解及实例代码 问题描述 一个系统往往有成千上万的组件,如果需要手动将所有组件都纳入spring容器中管理,是一个浩大的工程。 解决方案 Spring 提供组件扫描(component scanning)功能。它能...

    Spring Boot最常用的30个注解.docx

    详细介绍了Spring Boot最常用的30个注解,包含概念、原理、示例 Spring Boot最常用的30个注解 一、 @SpringBootApplication 二、 Spring Bean 相关 1 @Controller 2 @Service 3 @Repository 4 @Component 5 @Bean 6 ...

    Spring.html

    Spring IOC 控制反转:把创建对象的权利交给Spring 创建对象 1.无参构造 2.静态工厂 3.实例工厂 管理对象 对象关系DI 构造器注入 set注入 生命周期 scope:prototype/singleton init-...

    Spring中文帮助文档

    2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载...

    Java面试可能问的问题.docx

    @Component 在类定义之前添加@Component注解,他会被spring容器识别,并转为bean。 @Repository 对Dao实现类进行注解 (特殊的@Component) @Service 用于对业务逻辑层进行注解, (特殊的@Compone

    Spring中 Configuration的使用.docx

    注: (1)、@Bean注解在返回实例的方法上,...(3)、既然@Bean的作用是注册bean对象,那么完全可以使用@Component、@Controller、@Service、@Ripository等注解注册bean,当然需要配置@ComponentScan注解进行自动扫描。

    基于Dubbo实现的SOA分布式(没有实现分布式事务)-SpringBoot整合各种组件的JavaWeb脚手架+源代码+文档

    6. 引用application.properties中的属性的方式:@ConfigurationProperties(prefix = "spring.mail") + @Component + setter + getter 7. 引用其他自定义配置文件中的属性的方式: - @Component - ## 项目备注 1...

    SpringBoot常用注解详解含使用示例(值得珍藏)

    本文将详细介绍Spring Boot中最常用的注解,包括@SpringBootApplication、@Component、@Service、@Repository、@Controller、@RequestMapping、@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@Autowired...

    springboot-autowire:学习springboot自动装配原理

    @Component注解,或者@Component注解的扩展,@ Controller,@ Service,存储库,@ Configruation等, ### 2. @ Configuration启动容器+ @ Bean注册Bean 从Spring3.0,@ Configuration用于定义的配置类,可替换XML...

    SpringMVC常见知识点.md

    - @Component @Controller @Service @Repository 区别? &lt;!-- /TOC --&gt; Spring MVC常见知识点及源码解析 MVC 是什么 / 有什么优点? MVC是一种设计模式,遵循 模型(Model),视图(View) 和 控制器(Controller)的...

    Spring API

    2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载...

Global site tag (gtag.js) - Google Analytics