//@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" />
时间: 2024-10-15 22:07:25