注解注入annotation
2016-06-20 17:12
369 查看
注解注入annotation
注意重点内容
要想用annotation必须在applicationContext.xml中加入 命名空间
applicationContext.xml中文件
注意重点内容
要想用annotation必须在applicationContext.xml中加入 命名空间
<!-- 要想用注解注入 ,必须加入这三条 xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> -->
applicationContext.xml中文件
<?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" 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"> <bean id="person" class="com.example.vic.test_spring.di_annotation.Person"> </bean> <bean id="student" class="com.example.vic.test_spring.di_annotation.Student"> </bean> <!-- 会先遍历 bean中的类 寻找@resource 然后匹配其中的name 然后注入 --> <context:annotation-config></context:annotation-config> </beans>
public class Person { public Person() { System.out.println("new person"); } @Resource(name="student") private Student student; @PostConstruct //在构造器之后 //然后注解注入就在这个init()之后 public void init(){ System.out.println("init"); } @PreDestroy //在spring容器销毁之前 public void destroy(){ System.out.println("destroy"); } public void sys(){ this.student.say(); } }
public class Student { public Student() { System.out.println("new Student"); } public void say() { System.out.println("student"); } }
相关文章推荐
- Ubuntu Server 14.04 x64安装Grafana
- samba 配置文件详解 及 权限设置
- 4-3 求链式表的表长
- Windows Server环境下MQTT服务器的搭建 07/17
- Qr-Code encode and decode with python+qrcode+zbar
- javassist的理解
- List逆转
- 使用Sublime Text3+Ctags+Cscope替代Source Insight
- MySQL 5.7 SYS系统SCHEMA
- BZOJ2793 [Poi2012]Vouchers
- 程序设计的基本概念
- 加锁查询 FOR UPDATE 解决表格查询极慢的问题
- Ubuntu Server 14.04 x64安装StatsD
- ionic-myAppDemo项目分析
- 做菜感悟
- oracle中date类型的比较
- android-R.array
- QSignalMapper的简单使用
- python 正则表达式学习小结
- Android蓝牙低功耗开发