@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去掉,则会有空指针异常。

最新文章

  1. linux 怎么完全卸载mysql数据库
  2. jar 命令 打包装class文件的文件夹
  3. SilverlightERP&amp;CRM源码(可用于开发基于Silverlight的CRM,OA,HR,进销存等)
  4. Android源码分析-全面理解Context
  5. Aspose.Word 操作word复杂表格 拆分单元格 复制行 插入行 文字颜色
  6. ps调色技能
  7. 应该知道的25个非常有用的CSS技巧
  8. 开发者MAC电脑里的十八般兵器
  9. 第三篇、调优之路 Apache调优
  10. 如何在Windows下使用matplotlib
  11. leetcodequestion_56 Merge Intervals
  12. Socket学习笔记
  13. 关于android中postDelayed方法的讲解
  14. FineReport启动后访问404
  15. 企业SVN版本控制服务器搭建
  16. js数组删除元素、json删除元素
  17. [转]Java微服务框架选型(Dubbo 和 Spring Cloud?)
  18. WITH RECOMPILE和OPTION(RECOMPILE)区别
  19. MyBatis3与Spring3无缝集成-从iBatis平滑过渡
  20. .Net2.0部署在IIS8.5上的问题

热门文章

  1. 014---Django的中间件
  2. ABAP CDS ON HANA-(1)CDSビュー作成
  3. 什么是OSS/BSS(电信业务)
  4. ArrayMap java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Object[]
  5. 读取hbase数据到mysql
  6. 【jQuery】 效果
  7. Anytime项目开发记录3
  8. 简单工具 &amp; 杂技
  9. 【题解搬运】PAT_A1016 Phone Bills
  10. JMeter接口响应数据出现乱码的三种解决方法