Spring In Action-2.1-01-@Component注解

时间:2017-08-29 22:39:03   收藏:0   阅读:233

//@Component注解会告诉Spring创建这个类的实例bean(注意,启动Component注解功能需要在xml里面配置,下面会将)
@Component

package com.zte.springinaction.soundsystem.imp;

import org.springframework.stereotype.Component;

import com.zte.springinaction.soundsystem.CompactDisc;

//@Component注解会告诉Spring创建这个类的实例bean(注意,启动Component注解功能需要在xml里面配置)
@Component
public class SgtPeppers implements CompactDisc {

	private String title="Pepper‘s Lonely";
	private String artist="The beatles";
	
	public void play() {
		System.out.println("Sgt Playing:title="+title+" artist="+artist);
	}

}

  

扫描有两种方式:

一、java代码方式:

package com.zte.springinaction.soundsystem;

import org.springframework.context.annotation.ComponentScan;

//ComponentScan:本类所在包的所有子包都会被扫描,并自动为其创建bean
@ComponentScan
public class CDPlayerSpringConfig {

}

  

二、xml配置方式:

<context:component-scan base-package="soundsystem" />

  

原文:http://www.cnblogs.com/zjsy/p/7450537.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!