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

从头认识Spring-2.7 自己主动检測Bean(1)-@Component @Repository @Service @Controller的更多相关文章

  1. 从头认识Spring-2.7 自己主动检測Bean(2)-过滤器&lt;context:include-filter/&gt;

    这一章节我们来讨论一下过滤器<context:include-filter/>的使用. 1.domain Person接口: package com.raylee.my_new_sprin ...

  2. 自己主动检測&后台复制光盘内容

    原理:利用python的win32模块,注冊服务,让代码在后台执行,检測光盘并复制文件 启动的方法就是直接在cmd下,main.py install ,然后去windows 的服务下就能够看到The ...

  3. Android Studio代码自己主动检測错误提示

    Android Studio的代码自己主动检測的错误提示方式感觉有点奇葩.和Eclipse区别非常大,Eclipse检測到某个资源文件找不到或者错误,都会在Project中相应的文件前面打叉.可是An ...

  4. 使用UIDataDetectorTypes自己主动检測电话、网址和邮箱

    支付宝公布最新版本号9.0.再一次引发一场撕逼大战.微信说支付宝抄袭了它.支付宝说微信一直都在抄袭自己.在我看来.微信和支付宝都抄袭了对方.对于大佬们的抄袭.我们也是司空见惯了. 支付宝这一次更新,真 ...

  5. [转]what’s the difference between @Component ,@Repository & @Service annotations in Spring

    原文地址:https://www.cnblogs.com/softidea/p/6070314.html @Component is equivalent to <bean> @Servi ...

  6. What's the difference between @Component, @Repository & @Service annotations in Spring?

    @Component is equivalent to <bean> @Service, @Controller , @Repository = {@Component + some mo ...

  7. Android自己主动检測版本号及自己主动升级

    步骤: 1.检測当前版本号的信息AndroidManifest.xml-->manifest-->android:versionName. 2.从server获取版本号号(版本号号存在于x ...

  8. Spring系列11:@ComponentScan批量注册bean

    回顾 在前面的章节,我们介绍了@Comfiguration和@Bean结合AnnotationConfigApplicationContext零xml配置文件使用Spring容器的方式,也介绍了通过& ...

  9. 使用Spring的@Autowired 实现DAO, Service, Controller三层的注入(转)

    简述: 结合Spring和Hibernate进行开发 使用@Autowired实现依赖注入, 实现一个学生注册的功能,做一个技术原型 从DAO(Repository) -> Service -& ...

随机推荐

  1. 关于linux下的.a文件与 .so 文件

    连续几天终于将一个又一个问题解决了,这里说其中一个问题 描述问题:使用多线程pthread的时候,(我用的IDE,CODEBOLCKS)编译后发现直接弹出窗口,程序还没有被Build..巴拉巴拉,然后 ...

  2. 涨知识---V

    1.内存对齐 在解释内存对齐的作用前,先来看下内存对齐的规则: 1).  对于结构的各个成员,第一个成员位于偏移为0的位置,以后每个数据成员的偏移量必须是min(#pragma pack()指定的数, ...

  3. 【转】linux之type命令

    转自: http://codingstandards.iteye.com/blog/831504 用途说明 type命令用来显示指定命令的类型.一个命令的类型可以是如下之一 alias 别名 keyw ...

  4. Oracle 递归的写法(start with) 以及where条件作用域

    先转一个讲Oracle递归讲得非常透彻的文章: http://blog.csdn.net/weiwenhp/article/details/8218091 前言:嗯,这也是一个前人挖坑,后人来填的故事 ...

  5. Scala-基础-数据类型

    import junit.framework.TestCase import org.junit.Test import scala.runtime.RichByte //数据类型 class Dem ...

  6. JS高级——扩展内置对象的方法

    基本概念 内置对象有很多,几个比较重要的:Math.String.Date.Array 基本使用 1.内置对象创建出来的对象使用的方法使用的其实都是内置对象的原型对象中的方法 (1)a并没有charA ...

  7. The type new View.OnClickListener(){} must implement the inherited abstract method View.Onclicklis

    public class MainActivity extends Activity { protected Button startBrew = null; @Override protected ...

  8. Windows Server 2008无法远程连接

    Server 2008 R2依次配置好之后,重启发现总是远程桌面时而连接不上.具体现象如下: 偶尔可以通过桌面远程连接连接到Server.以为是防火墙的问题,各种设置——甚至关闭,依然无法连接.反复重 ...

  9. C# 时间对比

    public bool IfTime(string StartTime, string EndTime) { DateTime dt1 = Convert.ToDateTime(StartTime); ...

  10. vs Could Not Connect

    解决,   在win7上卸载IIS 10.0 Express ,安装 IIS7.5 Express