@Resource注解完成自动装配

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

时间: 2024-11-06 03:52:02

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

用@Resource注解完成属性装配

使用Field注入(用于注解方式):注入依赖对象可以采用手工装配或者手工自动装配.在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发人员无法预见最终的装配结果. 依赖注入—手工装配 手工装配依赖对象,在这种方式中又有两种编程方式. 1.在xml配置文件中,通过bean节点配置,如: 1 <bean id="orderService" class="cn.itcast.service.OrderServiceBean"> 2 //构造器注入 3

[email&#160;protected]注解与自动装配

1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss 拥有 Office 和 Car 类型的两个属性:       清单 3. Boss.java [java] view plaincopy package com.baobaotao; public class Boss { private Car car; private Office office

Spring框架中利用注解进行自动装配的环境配置步骤和常见问题

第1步:配置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.spring

Spring(七)用@Resource注解完成属性装配

使用到注解需导入jar包:common-annotations.jar 手工装配依赖对象有两种编程方式: 一.在xml配置文件中通过bean节点进行配置,如: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001

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

Spring支持用注解配置Bean,更简便. 上面的组件,是根据实际情况配的.比如写的一个类,是做业务处理的,那就用注解@Service表示服务层组件,以此类推.将整体分成不同部分. 要在xml加入context命名空间 1 <!-- 指定Spring IOC容器扫描的包 --> 2 <context:component-scan base-package="package com.guigu.spring.beans.annotation"></cont

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

1 JavaConfig  配置方法 之前我们都是在xml文件中定义bean的,比如: 1 2 3 4 5 6 7 8 <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://www.springframework

Spring2.5学习3.1_用@Resource注解完成属性装配

几个概念解析: 1.Spring的依赖三种依赖注入方式: 使用构造器注入 使用属性setter方法注入 使用Field方式注入(用于注解方式) 2.注入依赖对象可以使用手工装配和自动装配,在实际应用在建议使用手工装配,因为自动装配会产生未知情况,开发人员无法预知最终的装配结果. (1)手工装配依赖对象的两种编程方式: 在XML配置文件中通过bean节点的方式实现: <span style="white-space:pre"> </span><bean id

Spring中@Autowired注解与自动装配

1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法.比如:Boss 拥有 Office 和 Car 类型的两个属性:public class Boss { private Car car; private Office office; // 省略 get/setter @Override public String toString() { retu

【Spring基础学习】注解实现自动装配

在IOC容器中学习相关注解(常用) 1. @Autowired a.作用对象:(官网解释) 1. You can apply the @Autowired annotation to constructors: 2.you can also apply the @Autowired annotation to "traditional" setter methods: 3.You can also apply the annotation to methods with arbitra