【笔记】Spring4框架系列 [ 5 ]
来源:互联网 发布:软件开发周末兼职 编辑:程序博客网 时间:2024/05/03 06:47
基于注释注入@Component @Value @Resource @Autowired @PostConstruct @PreDestroy等
【Student 】
package com.athl.spring;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;/* 与@Component具有相同功能,不同意义的注解还有三个: @Repository:注释在Dao实现类上 @Service:注解在Service实现类上 @Controller:注解在处理器上(SpringMVC) */@Component("myStudent")public class Student { @Value("张三") private String name; @Value("22") private int age; /*域属性的byType自动注入,只需@Autowired*/ //@Autowired /*byName自动注入,需要@Qualifier与@Autowired配合使用*/ //@Qualifier("mySchool") /*@Resource是jdk的注解,版本6及以上版本.*/ //@Resource/*byType自动注入*/ @Resource(name="mySchool")/*byName自动注入*/ private School school; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setSchool(School school) { this.school = school; } /*Bean生命周期始末注解*/ @PostConstruct public void afterInit(){ System.out.println("初始化完毕后"); } @PreDestroy /*需要显示关闭容器*/ public void preDestroy(){ System.out.println("销毁之前"); } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", school=" + school + "]"; }}
【School 】
package com.athl.spring;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Component("mySchool")public class School { @Value("黄埔军校") private String name; public School() { } public School(String name) { this.name = name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "School [name=" + name + "]"; }}
【applicationContext.xml】
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描com.athl这个包及其子包 --> <!-- <context:component-scan base-package="com.athl"/> --> <!-- 扫描com.athl的所有子包 --> <!-- <context:component-scan base-package="com.athl.*"/> --> <context:component-scan base-package="com.athl.spring"/> <!-- 注解与xml同时使用 --> <!-- 注解好处:配置方便,直观; 缺点:修改需要重新编译代码。 xml好处:修改无须重新编译。 同时使用,xml的优先级要高于注解.若要修改修改配置文件即可。 那么Bean类要有setter或构造器. --></beans>
【Test】
package com.athl.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest { @Test public void run(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println(ac.getBean("myStudent")); /*显示关闭容器*/ ((ClassPathXmlApplicationContext)ac).close(); }}
源码下载:http://download.csdn.net/detail/jul_11th/9741743
谢谢支持!
0 0
- 【笔记】Spring4框架系列 [ 5 ]
- 【笔记】Spring4框架系列 [ 1 ]
- 【笔记】Spring4框架系列 [ 2 ]
- 【笔记】Spring4框架系列 [ 3 ]
- 【笔记】Spring4框架系列 [ 4 ]
- 【笔记】Spring4框架系列 [ 6 ]
- 【笔记】Spring4框架系列 [ 7 ]
- 【笔记】Spring4框架系列 [ 8 ]
- 【笔记】Spring4框架系列 [ 9 ]
- 【笔记】Spring4框架系列 [ 10 ]
- spring4.x系列学习笔记(五)
- spring4.x系列学习笔记(四)
- J2EE系列之Spring4学习笔记(十三)--Spring4整合Struts2和Hibernate4
- J2EE系列之Spring4学习笔记(十四)--Spring4整合Struts2和Hibernate4
- J2EE系列之Spring4学习笔记(一)--Spring介绍
- Spring4第五讲学习笔记,整合Spring,Struts,Mybatis框架
- Java学习笔记--SSH框架整合(struts2、spring4、hibernate5)
- 搭建SSH框架问题汇总笔记(Struts2+Spring4+Hibernate5)
- 接口和抽象类理解
- 利用canvas获取图片像素值
- Viewpager实现真正的无限滑动,拒绝Integer.MAX_VALUE
- 398. Random Pick Index (M)
- Java中常用的类与基本方法
- 【笔记】Spring4框架系列 [ 5 ]
- 前端面试题集锦
- Leetcode19 - Remove Nth Node From End of List(链表)
- 值得你关注的Android6.0上的重要变化(一)
- 学习笔记
- 树莓派修改时区,
- Leetcode234 - Palindrome Linked List(链表)
- Windows下利用WinDBG、OllyDBG进行源码级调试
- 工欲善其事必先利其器之apache ant