Spring 一二事(7) - annotation
之前的文章大多都是一带而过,一方面比较简单,一方面不是用的注解形式
在企业开发中,主要还是使用的注解来进行开发的
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的更多相关文章
- Spring 一二事(8) - annotation 形式的 MVC
<!-- component:把一个类放入到spring容器中,该类就是一个component 在base-package指定的包及子包下扫描所有的类 --> <context:co ...
- Spring 一二事(10) - annotation AOP
先贴出POM的内容,这个毕竟是用的maven来简单构建的 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...
- Spring 一二事(4) - 单例
spring bean配置后再默认情况下是单例的,如果需要配置可以选择 prototype, request, session和global session 在配置spring mvc的action时 ...
- Spring 一二事(1)
简单介绍一下spring,一方面带新手入入门,一方面自己也重温一下第一个小工厂先暂时不用maven,下一个会用maven来来配置 jar包只需要一个,spring版本为2.5(暂时为2.5,后续更新, ...
- Spring 一二事(9) - xml 形式的 AOP
AOP在spring中是非常重要的一个 在切面类中,有5种通知类型: aop:before 前置通知 aop:after-returning 后置通知 aop:after 最终通知 aop:af ...
- Spring 一二事(5) - 依赖注入
<!-- 依赖注入的装配过程 --> <bean id="person" class="com.lee.spring007.di.xml.setter. ...
- Spring 一二事(3) - 别名
别名就是可以通过另外一个名字来访问如下,已有bean:helloWorld3,那么定义别名(alias )后,就能使用“abc”来访问 <bean id="helloWorld3&qu ...
- Spring 一二事(2)
静态工厂方法及实例工厂的使用: applicationContext.xml: <!-- factory-method 是指调用静态工厂方法 --> <bean id="h ...
- Spring 一二事(6) - IOC MVC 简易搭建
<bean id="personAction" class="com.lee.spring008.IOC.DI.MVC.PersonAction"> ...
随机推荐
- Roo中的@Version
首页 关于 Roo中的@Version 发表回复 问题提出 当我们为entity添加@RooJpaActiveRecord注解时,Roo为我们自动生成了一个名为Entity_Roo_Jpa_Entit ...
- android开发步步为营之67:使用android开源项目android-async-http异步下载文件
android-async-http项目地址 https://github.com/loopj/android-async-http.android-async-http顾名思义是异步的http请求, ...
- ZH奶酪:PHP如何判断提交表单中多个复选框是否选中?
1.name命名为数组,例如“select[]” 2.例如这样选: 3.在后台使用$_POST['select']得到数组 4.然后就可以看到得到的数组了 原文链接:http://www.zhihu. ...
- 借助AWR报告分析解决oracleCPU过高的问题(转)
原文地址:http://www.cnblogs.com/crystal-guoguo/p/4213458.html 简介:在oracle数据库中,有两个非常实用的自带监控工具EM(Enterprise ...
- Linux对文件归档和压缩(学习笔记八)
一.归档和压缩 压缩命令工具:gzip,bzip2 归档命令工具:tar 二.压缩 2.1.gzip gzip是一种标准的.广泛应用的文件压缩和解压缩实用工具.gzip允许文件并置.用gzip压缩文件 ...
- PyQt5教程——布局管理(4)
PyQt5中的布局管理 布局管理是GUI编程中的一个重要方面.布局管理是一种如何在应用窗口上防止组件的一种方法.我们可以通过两种基础方式来管理布局.我们可以使用绝对定位和布局类. 绝对定位 程序指定了 ...
- 事务的四大特性ACID介绍
事务是恢复和并发控制的基本单位.ACID 事务应该具有4个属性:原子性.一致性.隔离性.持续性.这四个属性通常称为ACID特性. 原子性(atomicity).一个事务是一个不可分割的工作单位,事务中 ...
- 【微信小程序】用户首次进入小程序拒绝授权,如何再次调用授权页面,获取用户信息userInfo
前言:微信小程序的app.js里面,最少有2个接口,一个wx.login:一个是wx.getUserInfo: 前者得到腾讯给我们的微信用户唯一的code,通过code获取openid,这个不需要用户 ...
- PHP基于Sphinx+Swcs中文分词的全文的检索
简介 Sphinx是开源的搜索引擎,它支持英文的全文检索.所以如果单独搭建Sphinx,你就已经可以使用全文索引了 但是有些时候我们还要进行中文分词所有scws就出现了,我们也可以使用Coreseek ...
- hdu1695 容斥原理 莫比乌斯反演
给定两个数b,d,问[1,b]和[1,d]区间上有多少对互质的数.(x,y)和(y,x)算一个. 对于[1,b]部分,用欧拉函数直接求.对于大于b的部分,求n在[1,b]上有多少个互质的数,用容斥原理 ...