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"> ...
随机推荐
- 禁止CloudStack删除Xenserver原有虚拟机
CloudStack在文档中指明需要加入一台干净的Xenserver作为hyperviser. 但是实际使用中,总会存在不同的需求,很多场景是试用CloudStack接管当前已有的hyperviser ...
- Java从零开始学十九(异常)
一.什么是异常 从字面上讲,就是不正常的现实就是异常. 程序中的异常也是要在程序运行中才会偶尔发生.如果程序还没有运行,编译就报错,这种不叫异常,这种叫编译错误,通常是语法上的错误 二.java中异常 ...
- TP自动生成模块目录
TP自动生成模块目录 例如我想在项目中增加一个AdminI模块 只需要在入口文件index.php中添加: define('BIND_MODULE','Admin'); 再访问127.0.0.1项目就 ...
- Canvas简述
HTML Canvas API有两方面优势可以弥补:首先,不需要将所绘制图像中的每个图元当做对象存储,因此执行性能非常好:其次,在其他编程语言现有的优秀二维绘图API的基础上实现Canvas API相 ...
- java面试第十八天
软件开发流程: 1.可行性分析 2.需求分析->开发测试 3.概要设计->分隔模块,定义框架等 4.详细设计->类设计.接口设计 5.编码 6.测试 7.部署 8.维护 单元测试: ...
- javascript 1.5s跳转
<script type="text/javascript"> var t = 1.5; window.onload=countDown; function count ...
- ubuntu移植jsoncpp到Android平台(转)
NDK开发模块的时候,如果涉及到网络请求,类似json数据传递的时候,有现成的第三方json库可以移植,后台C++开发中使用的比较多的是jsoncpp,今天记录一下jsoncpp移植到Android平 ...
- 〖Linux〗联想K860/i Android 4.2及以上的Bootimg解压与打包工具
因为自己有需要,所以花了一点时间来写了一下. 1. 解压工具 #!/bin/bash - #====================================================== ...
- 谷歌浏览器禁止window.close的问题
当一个窗口不是通过window.open或者window.showModalDialog打开的时候,调用JS的window.close() 谷歌浏览器会提示如下警告,并拒绝执行 Scripts may ...
- 搭建Weblogic服务器
安妮,我的小熊熊在ne....... 01.安全设置 service iptables stop chkconfig iptables off #关闭防火墙,只是建议,为了简便操作 setenf ...