之前的文章大多都是一带而过,一方面比较简单,一方面不是用的注解形式

在企业开发中,主要还是使用的注解来进行开发的

1   <!--
component:把一个类放入到spring容器中,该类就是一个component
在base-package指定的包及子包下扫描所有的类
-->
<context:component-scan base-package="com.lee.spring011.scan"></context:component-scan>

主要还是用 @Resource,另外2个不常用

 package com.lee.spring010.DI.annotation;

 import javax.annotation.Resource;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; public class Person { @Resource
// @Resource(name="studentA")
// @Autowired 纯粹按照类型进行匹配
// @Qualifier("studentA")
private Student studentA; // public Student getStudentA() {
// return studentA;
// } public void tell() {
studentA.sayHello();
} }
 package com.lee.spring010.DI.annotation;

 public class Student {

     public void sayHello() {
System.out.println("Hello! I am a student...nathan!");
} }

测试:

 package com.lee.spring010.DI.annotation;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AnnotationTest { /**
* 原理
* 1、当启动spring容器的时候,创建两个对象
* 2、当spring容器解析到
* <context:annotation-config></context:annotation-config>
* spring容器会在spring容器管理的bean的范围内查找这些类的属性上面是否加了@Resource注解
* 3、spring解析@Resource注解的name属性
* 如果name属性为""
* 说明该注解根本没有写name属性
* spring容器会得到该注解所在的属性的名称和spring容器中的id做匹配,如果匹配成功,则赋值
* 如果匹配不成功,则按照类型进行匹配
* 如果name属性的值不为""
* 则按照name属性的值和spring的id做匹配,如果匹配成功,则赋值,不成功,则报错
* 说明:
* 注解只能用于引用类型
* 注解写法比较简单,但是效率比较低
* xml写法比较复杂,但是效率比较高
*/
@Test
public void testPerson() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person personA = (Person)context.getBean("personA");
// personA.getStudentA().sayHello();
personA.tell();
} }

github地址:https://github.com/leechenxiang/maven-spring001-helloworld

Spring 一二事(7) - annotation的更多相关文章

  1. Spring 一二事(8) - annotation 形式的 MVC

    <!-- component:把一个类放入到spring容器中,该类就是一个component 在base-package指定的包及子包下扫描所有的类 --> <context:co ...

  2. Spring 一二事(10) - annotation AOP

    先贴出POM的内容,这个毕竟是用的maven来简单构建的 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...

  3. Spring 一二事(4) - 单例

    spring bean配置后再默认情况下是单例的,如果需要配置可以选择 prototype, request, session和global session 在配置spring mvc的action时 ...

  4. Spring 一二事(1)

    简单介绍一下spring,一方面带新手入入门,一方面自己也重温一下第一个小工厂先暂时不用maven,下一个会用maven来来配置 jar包只需要一个,spring版本为2.5(暂时为2.5,后续更新, ...

  5. Spring 一二事(9) - xml 形式的 AOP

    AOP在spring中是非常重要的一个 在切面类中,有5种通知类型: aop:before  前置通知 aop:after-returning  后置通知 aop:after  最终通知 aop:af ...

  6. Spring 一二事(5) - 依赖注入

    <!-- 依赖注入的装配过程 --> <bean id="person" class="com.lee.spring007.di.xml.setter. ...

  7. Spring 一二事(3) - 别名

    别名就是可以通过另外一个名字来访问如下,已有bean:helloWorld3,那么定义别名(alias )后,就能使用“abc”来访问 <bean id="helloWorld3&qu ...

  8. Spring 一二事(2)

    静态工厂方法及实例工厂的使用: applicationContext.xml: <!-- factory-method 是指调用静态工厂方法 --> <bean id="h ...

  9. Spring 一二事(6) - IOC MVC 简易搭建

    <bean id="personAction" class="com.lee.spring008.IOC.DI.MVC.PersonAction"> ...

随机推荐

  1. ArcGIS Pro体验01——申请、下载、安装

    ArcGIS Pro采用了Ribbon界面风格,看起来好漂亮,听起来很强大,就是不知道用起来怎么样,在网上看到一个ArcGIS Pro Beta2版本,下载下来,安装启动,好眼熟,像Office201 ...

  2. HDU1588-Gauss Fibonacci(矩阵高速幂+等比数列二分求和)

    题目链接 题意:g(x) = k * x + b.f(x) 为Fibonacci数列.求f(g(x)),从x = 1到n的数字之和sum.并对m取模. 思路:  设A = |(1, 1),(1, 0) ...

  3. D. Flowers Codeforces Round #271(div2)

    D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input ...

  4. php如何实现页面跳转

    •PHP页面跳转一.header()函数 header()函数是PHP中进行页面跳转的一种十分简单的方法.header()函数的主要功能是将HTTP协议标头(header)输出到浏览器. header ...

  5. line-height测量及使用

    1.line-height定义 line-height表示行高,即两行文字基线间的距离. 以下是图示说明: 行高是2条红线之间的距离,即:1+2+3+4 在实际测量中,基线不好找,可测量顶线到顶线的距 ...

  6. jQuery停止动画finish和stop函数区别

    stop()函数直接停止动画,finish()也会停止动画同时所有排队的动画的CSS属性跳转到他们的最终值. 示例代码: <html> <head> <meta char ...

  7. &quot;高可用方案工具包&quot; high availability toolkit 1.2 公布了。version 1.2 新增了 负载均衡 load balance 的技术实现

    "高可用方案工具包"  high availability toolkit 1.2 公布了. version 1.2 新增了 负载均衡 load balance 的技术实现. 项目 ...

  8. tomcat 禁用access.log

    修改 server.xml 注释掉,如: <!-- Access log processes all example. Documentation at: /docs/config/valve. ...

  9. Content-Length实体的大小

    •15.2 Content-Length实体的大小 Content-Length首部指出了报文中实体主体的字节大小,这个大小包含了所有内容的编码,如对文本进行gzip压缩的话,那么Content-Le ...

  10. 文本挖掘之文本聚类(MapReduce)

    刘 勇  Email:lyssym@sina.com 简介 针对大数量的文本数据,采用单线程处理时,一方面消耗较长处理时间,另一方面对大量数据的I/O操作也会消耗较长处理时间,同时对内存空间的消耗也是 ...