从头认识Spring-2.7 自己主动检測Bean(1)-@Component @Repository @Service @Controller

时间:2022-08-04 21:58:21

这一章节我们来讨论一下自己主动检測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