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

上一篇:Python内置函数(18)——bin


下一篇:[原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件