Spring - 注解的使用 和 继承的配置
来源:互联网 发布:营收数据分析 ppt模板 编辑:程序博客网 时间:2024/06/13 18:06
1.注解
(1)注解就是为了说明java中的某一个部分的作用(Type);
(2)注解都可以用于那个部分是@Target注解起的作用;
(3)注解可以标注在ElementType枚举类所指定的位置上;
(4)用来解析注解的类成为注解解析器;
2.依赖注入的注解
(1) bean.xml 导入注解解析器 - context命名空间,只有引用类型可以加上注解,基本类型不可以;
(2)@Resource使用步骤:
1) 在spring的配置文件中导入命名空间
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
2) 引入注解解析器
<context:annotation-config/>
3) 在spring的配置文件中把bean引入进来
<bean id="person_di" class="cn.labelnet.di.Person"></bean><bean id="student_di" class="cn.labelnet.di.Student"></bean>
4) 在一个类属性上把bean引入进来
@Resource(name="student_di")private Student student;
5) 在一个类的属性上加上@Resource(name="")
该注解可以用于属性上或者方法上,但一般用于属性上,属性name默认值为"";
(3)示例
1)Person类
public class Person {private String name;private Integer age;@Resource(name="student_di")private Student student;public void showStudent(){this.student.say();}}
public class Student {public void say(){System.out.println("我是学生");}}
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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/><bean id="person_di" class="cn.labelnet.di.Person"></bean><bean id="student_di" class="cn.labelnet.di.Student"></bean></beans>
4)测试
@Testpublic void testDI() { Person person=(Person) context.getBean("person_di"); person.showStudent();}
(4)总结:
当启动spring容器的时候,spring容器加载了配置文件,只要遇到bean的配置,就会
为该bean创建对象,在容器范围内查找所有的bean,如果有@Resource注解,判断注解name属
性值是否与speing容器中ID的值做匹配,匹配成功则赋值;
3.类扫描注解
(1)类扫描的注解解析器
component : 指的就是一个类;base-package 在该包
(2)基本步骤:
1. 导入命名空间
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
2. 引用注解解析器
包含两个功能,类扫描和依赖注入,在base-package包下的查找所有类;
<context:component-scan base-package="包名"></context:component-scan>
3. 配置@Component("")
@Component("a")public class Person {private String name;private Integer age;@Resource(name = "b")private Student student;public void showStudent() {this.student.say();}}
4. 按照@Resource步骤来使用;
(3)示例
1)Person类
@Component("a")public class Person {private String name;private Integer age;@Resource(name = "b")private Student student;public void showStudent() {this.student.say();}}
@Component("b")public class Student {public void say(){System.out.println("我是学生 SCAN");}}
3)bean配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="cn.lablenet.scan"></context:component-scan></beans>
4)测试
@Testpublic void testScan() { Person person=(Person) context.getBean("a"); person.showStudent();}
(4)总结xml与注解
1. xml书写麻烦,但是效率高;
2. 注解书写简单,但效率低;(可以忽略)
4.示例 - 简单的mvc程序
使用的注解有 : @Repository , @Service , @Controller ,分别对应 dao层,service层,action层;
(1)dao 接口
public interface DocumentDao {void saveDocument();}
(2)dao实现
@Repository("DocDao")public class DocumentDaoImpl implements DocumentDao {@Overridepublic void saveDocument() { System.out.println("保存成功");}}
public interface DocumentService {void saveDocument();}
@Service("DocService")public class DocumentServiceImpl implements DocumentService {@Resource(name="DocDao")private DocumentDao docdao;@Overridepublic void saveDocument() {docdao.saveDocument();}}
(5)action实现
@Controller("DocController")public class DocumentView {@Resource(name="DocService")private DocumentService doucService;public void saveDoc(){doucService.saveDocument();}}
(6)bean配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="cn.labelnet.mvc"></context:component-scan> </beans>
(7)测试
@Testpublic void testDocument() {DocumentView view= (DocumentView)context.getBean("DocController");view.saveDoc();}
5.继承配置
继承 :
在配置文件中 给 bean 添加 abstruct="true" 属性,告诉spring容器,该bean不能创建对象;
子类想用父类的属性,需要在bean 添加 parent="xxx abstruct" 属性,添加父类bean的id;
示例:
父类 :
public class Person {private String name;public void say(){System.out.println("我是父类");}}
public class Student extends Person {}
bean配置 :
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="person_ex" class="cn.labelnet.extend.Person" abstract="true"></bean> <bean id="student_ex" class="cn.labelnet.extend.Student" parent="person_ex"></bean> </beans>
6.Demo免积分下载
http://download.csdn.net/detail/lablenet/9379973
- Spring - 注解的使用 和 继承的配置
- Spring service继承的事务配置(注解)
- 【Spring】基于注解的Spring AOP的配置和使用
- spring(DI)注解的使用、类扫描的注解、Xml的继承、注解的继承
- 基于注解的Spring AOP的配置和使用
- 基于注解的Spring AOP的配置和使用--转载
- 基于注解的Spring AOP的配置和使用
- 基于注解的Spring AOP的配置和使用
- 基于注解的Spring AOP的配置和使用 (顶)
- 基于注解的Spring AOP的配置和使用
- 基于注解的Spring AOP的配置和使用--转载
- 基于注解的Spring AOP的配置和使用--转载
- 基于注解的Spring AOP的配置和使用--转载
- 基于注解的Spring AOP的配置和使用--转载
- 基于注解的Spring AOP的配置和使用
- 基于注解的Spring AOP的配置和使用
- 基于注解的Spring AOP的配置和使用--转载
- 基于注解的Spring AOP的配置和使用--转载
- Android根据经纬度获取地址,眼前一亮
- aws EC2 挂载 其它卷(aws EC2 网络配置出错,不能 ssh 连接时,需要启动一个新实例,去挂载 这个出错的卷,来修改卷里的错误配置)
- python实现的控制台进度条
- 《Android源码设计模式解析与实战》读书笔记(十二)
- iOS -- 获取当前时间,以及出现的错误
- Spring - 注解的使用 和 继承的配置
- sip协议---------阿冬专栏!!!
- STM32后缀名说明
- DPDK在虚拟机上的安装和使用
- iOS崩溃调试的使用和技巧总结
- 值得注意的OpenCV数据类型问题
- Android版添加phonegap--融云即时通讯插件教程
- Warning: The string 'returncode' with parameter delimiters is not a parameter
- 明年工资新规定,8种人将淘汰,你在其中么