@Resource注解是通过名字来自动装配的。在spring中自动装配的模式如果是通过名字来自动装配那么必须保证bean的名字和pojo 的属性名一直。

下面是详细代码:说明了@Resource注解是通过名字来完成自动装配的,可以说@Resource注解在某些情况下可以代替@Autowired(通过类型)注解.

Address类的代码如下:

package com.timo.domain;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable; import javax.annotation.PostConstruct;
import javax.annotation.Resource; public class Address implements InitializingBean ,BeanPostProcessor{
private String city6;
private String state3; public Address() {
System.out.println("Instantiate");
} public String getCity4() {
return city6;
} public void setCity(String city) {
this.city6 = city;
System.out.println("populate properties");
} public String getState2() {
return state3;
} public void setState(String state) {
this.state3 = state;
}
public void destory(){
System.out.println("destory");
} public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet");
}
public void init(){
System.out.println("init");
}
@PostConstruct
public void postConstructor(){
System.out.println("post constructor");
} @Nullable
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization");
return null;
} @Nullable
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization");
return null;
}
}

Student类的代码如下:

package com.timo.domain;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.BeanPostProcessor; import javax.annotation.Resource; public class Student implements BeanNameAware,BeanPostProcessor{
private Integer age;
private String name;
@Resource
//用@Resource注解完成自动装配。
private Address address; public Student(Address address) {
this.address = address;
} public Student(String name, Address address) {
this.name = name;
this.address = address;
} public Student(Integer age, String name, Address address) {
this.age = age;
this.name = name;
this.address = address;
} public Student() {
System.out.println("student Instantiate");
} public Integer getAge2() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getName2() {
return name;
} public void setName(String name) {
this.name = name;
} public Address getAddress() {
return address;
} public void setAddress(Address address) {
this.address = address;
} @Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
", address=" + address +
'}';
}
public void show(){
System.out.println(address.getCity4());
System.out.println(address.getState2());
}
public void setBeanName(String name) {
System.out.println("the bean name is:"+name);
}
}

配置文件的代码如下:applicationContext-resource.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--<context:annotation-config/> only looks for annotations on beans in the same application context in which
it is defined RequiredAnnotationBeanPostProcessor AutowiredAnnotaionBeanPostProcessor
CommonAnnotationBeanPostProcessor PersistenceAnnotaionBeanPostProcessor-->
<context:annotation-config/>
<bean id="address" class="com.timo.domain.Address">
<property name="city" value="安徽省"/>
<property name="state" value="合肥市"/>
</bean>
<bean id="student" class="com.timo.domain.Student"></bean>
</beans>

测试类的代码如下:

package com.timo.test;

import com.timo.domain.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test23 {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-resource.xml");
Student student = applicationContext.getBean(Student.class);
student.show(); }
}

上述中如果你把Student类中的@Resource去掉,则会有空指针异常。

@Resource注解完成自动装配的更多相关文章

  1. (转)用@Resource注解完成属性装配

    http://blog.csdn.net/yerenyuan_pku/article/details/52858878 前面我们讲过spring的依赖注入有两种方式: 使用构造器注入. 使用属性set ...

  2. Spring学习记录(十一)---使用注解和自动装配

    Spring支持用注解配置Bean,更简便. 上面的组件,是根据实际情况配的.比如写的一个类,是做业务处理的,那就用注解@Service表示服务层组件,以此类推.将整体分成不同部分. 要在xml加入c ...

  3. Spring中@Autowired注解与自动装配

    1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...

  4. 使用Spring的JavaConfig 和 @Autowired注解与自动装配

    1 JavaConfig  配置方法 之前我们都是在xml文件中定义bean的,比如: 1 2 3 4 5 6 7 8 <beans xmlns="http://www.springf ...

  5. (转)@Autowire注解与自动装配

    http://blog.csdn.net/yerenyuan_pku/article/details/52860713 前面我们已经学会使用@Resource注解注入属性,并且我们还编码剖析了@Res ...

  6. springboot自动装配(1)---@SpringBootApplication注解怎么自动装配各种组件

    1.对于springboot个人认为它就是整合了各种组件,然后提供对应的自动装配和启动器(starter) 2.@SpringBootApplication注解其实就是组合注解,通过它找到自动装配的注 ...

  7. Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  8. 【转】Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  9. Spring 学习——Spring注解——Autowiring(自动装配)

    装配方式 方式一:默认 方式二:byName:根据属性名称自动装配.会查找Bean容器内部所有初始化的与属性名成相同的Bean,自动装配.(需要通过set方法注入,注入Bean的id名称需要和实体类的 ...

随机推荐

  1. Teen Readers【青少年读者】

    Teen Readers Teens and younger children are reading a lot less for fun, according to a Common Sense ...

  2. 牛客网暑期ACM多校训练营(第四场) F

    参考:http://www.cnblogs.com/Jadon97/p/9383027.html #include <iostream> #include <cstdio> # ...

  3. STL 一些常用的STL函数(持续更新

    先说一下  一边要用到算法的东西一般要加#include<algorithm>头文件 一.栈和队列 1 栈 :一种线性表 特点  后进先出 头文件  #include<stack&g ...

  4. 【转】Git远程操作详解

    Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Gi ...

  5. Java RMI 入门指南

    开通博客也有好些天了,一直没有时间静下心来写博文,今天我就把两年前整理的一篇关于JAVA RMI入门级文章贴出来,供有这方面需要的同学们参考学习. RMI 相关知识 RMI全称是Remote Meth ...

  6. jackson 处理空值

    @JsonInclude(value=Include.NON_NULL) public class ResultBean 这样在返回数据的时候, { "code": "s ...

  7. npm命令 VS yarn命令

    npm yarn 说明 npm init yarn init  在项目中引导创建一个package.json文件 npm install yarn install/yarn  安装所有依赖包(依据pa ...

  8. Python网络编程(线程通信、GIL、服务器模型)

    什么是进程.进程的概念? 进程的概念主要有两点: 第一,进程是一个实体.每一个进程都有它自己的地址空间, 一般情况下,包括文本区域(text region).数据区域(data region)和堆栈( ...

  9. coreos install megacli

    基于官方的coreos ramdisk安装dell raid管理工具,其版本为debian8 jessie root@c64c7df05677:/# more /etc/apt/sources.lis ...

  10. 牛客 小a与星际探索

    链接:https://ac.nowcoder.com/acm/contest/317/C来源:牛客网 小a正在玩一款星际探索游戏,小a需要驾驶着飞船从1号星球出发前往n号星球.其中每个星球有一个能量指 ...