从头认识Spring-2.7 自动检测Bean

Posted 李灵晖

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从头认识Spring-2.7 自动检测Bean相关的知识,希望对你有一定的参考价值。

这一章节我们来讨论一下自动检测Bean。

1.domain

厨师类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Chief {

	@Value("jack")
	private String name = "";

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

除了上面的默认配置,我们还可以自己加上bean的id

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("jack")
public class Chief {

	@Value("jack")
	private String name = "";

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}


厨师类这里我们使用@Component来标注这个Bean,默认id是chief

其实还有其他标签可以作为bean的标注

@Component-作为构件来配置bean

@Repository-标识数据仓库

@Service-标识服务

@Controller-标识MVC的控制器


但是上面的几个标签的作用大同小异,都是标注有这么一个bean


2.测试类:

(1)下面是通过.class来提取bean

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
		"/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_19/ApplicationContext-test.xml" })
public class ChiefTest {

	@Autowired
	private ApplicationContext applicationContext;

	@Test
	public void testChief() {
		Chief jack = (Chief) applicationContext.getBean(Chief.class);
		System.out.println(jack.getName());
	}
}

(2)由于上面的bean没有标注id,因此,它的默认id是chief,我们也可以通过id来提取

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
		"/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_19/ApplicationContext-test.xml" })
public class ChiefTest {

	@Autowired
	private ApplicationContext applicationContext;

	@Test
	public void testChief() {
		Chief jack = (Chief) applicationContext.getBean("chief");
		System.out.println(jack.getName());
	}
}

3.配置文件(最简单)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

	<context:component-scan
		base-package="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19" />
</beans>

测试输出:

jack


总结:这一章节我们主要介绍自动检测Bean。


目录:http://blog.csdn.net/raylee2007/article/details/50611627 

 

我的github:https://github.com/raylee2015/my_new_spring