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

本文介绍Spring框架中@Component注解的使用方法,包括如何通过该注解将类注册为Bean,以及两种不同的配置方式:Java配置和XML配置。同时展示了如何通过@ComponentScan进行组件扫描。
代码下载:http://download.youkuaiyun.com/download/poiuy1991719/9959474

 

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

//接口
package soundsystem; import org.springframework.stereotype.Component; public interface CompactDisc { void play(); }

  

//接口实现类
package soundsystem; import org.springframework.stereotype.Component; //@Component注解会告诉Spring创建这个类的实例bean(注意,启动Component注解功能需要在xml里面配置) @Component public class SgtPeppers implements CompactDisc { private String title="Pepper's Lonely"; private String artist="The beatles"; SgtPeppers(){ System.out.println("SgtPeppers类实例化"); } public void play() { System.out.println("Sgt Playing:title="+title+" artist="+artist); } }

  

 

扫描有两种方式:

一、java代码方式:

//配置类
package soundsystem;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

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

}

  

//测试类
package soundsystem; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)//Spring的Junit测试,会在测试开始时,创建Spring的应用上下文 @ContextConfiguration(classes=CDPlayerSpringConfig.class)//表明配置类 public class SpringTest1 { //自动装配 @Autowired private SgtPeppers sp; @Test public void instanceSpring(){ sp.play(); } }

  

 

二、xml配置方式:

package soundsystem;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest2 {

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

	@Test 
	public void instanceSpring(){
		//将配置传过去,实例化容器
		ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
		SgtPeppers sp = (SgtPeppers)ctx.getBean("sgtPeppers");
		sp.play();
	}
}

  

<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
           
    <!-- 自动扫描配置 -->
    <context:component-scan base-package="soundsystem" />

</beans>

 

 

转载于:https://www.cnblogs.com/zjsy/p/7450537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值