@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去掉,则会有空指针异常。
@Resource注解完成自动装配的更多相关文章
- (转)用@Resource注解完成属性装配
http://blog.csdn.net/yerenyuan_pku/article/details/52858878 前面我们讲过spring的依赖注入有两种方式: 使用构造器注入. 使用属性set ...
- Spring学习记录(十一)---使用注解和自动装配
Spring支持用注解配置Bean,更简便. 上面的组件,是根据实际情况配的.比如写的一个类,是做业务处理的,那就用注解@Service表示服务层组件,以此类推.将整体分成不同部分. 要在xml加入c ...
- Spring中@Autowired注解与自动装配
1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...
- 使用Spring的JavaConfig 和 @Autowired注解与自动装配
1 JavaConfig 配置方法 之前我们都是在xml文件中定义bean的,比如: 1 2 3 4 5 6 7 8 <beans xmlns="http://www.springf ...
- (转)@Autowire注解与自动装配
http://blog.csdn.net/yerenyuan_pku/article/details/52860713 前面我们已经学会使用@Resource注解注入属性,并且我们还编码剖析了@Res ...
- springboot自动装配(1)---@SpringBootApplication注解怎么自动装配各种组件
1.对于springboot个人认为它就是整合了各种组件,然后提供对应的自动装配和启动器(starter) 2.@SpringBootApplication注解其实就是组合注解,通过它找到自动装配的注 ...
- Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- 【转】Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- Spring 学习——Spring注解——Autowiring(自动装配)
装配方式 方式一:默认 方式二:byName:根据属性名称自动装配.会查找Bean容器内部所有初始化的与属性名成相同的Bean,自动装配.(需要通过set方法注入,注入Bean的id名称需要和实体类的 ...
随机推荐
- 以源码安装的lamp环境为依托,源码安装zabbix监控系统
1.源码安装lamp环境 1)安装httpd, 以源码httpd-2.4.33为基础,解压后,执行./configure --prefix=/usr/local/ --sysconfdir=/etc/ ...
- mysql学习第四天(高级查询)
-- 第七章-- 1.查询入职日期最早和最晚的日期select min(hiredate),max(hiredate)from emp -- 2.查询职位以SALES开头的所有员工平均工资,最低工资, ...
- 发布npm包 登录报错 E409 Conflict
1.到官网注册个账号,并且验证完邮箱:https://www.npmjs.com/ 2.打开cmd命令行 登录:$npm login 根据提示 一步步完成登录. 3.新建一个项目文件夹: npmtes ...
- Qt irrlicht(鬼火)3D引擎 摄像机旋转问题
点击打开链接Irrlicht中的摄像有一个函数 setUpVector() if (m_device != 0 ) { core::vector3df rotation(y,x,0.f); m_cam ...
- Git创建project
1.登录创建新仓库 命名 2.https://gitforwindows.org/ 下载git的windows客户端,输入git查看是否成功 3.创建文件夹,写内容并查看,和linux指令一样 4. ...
- ASP NET Core ---REST & HTTP GET
参照 草根专栏- ASP.NET Core + Ng6 实战:https://v.qq.com/x/page/h0764n405ll.html 一.REST (Representational Sta ...
- linux学习笔记---学习总结②
table ----> 展示数据 table --->表格 border cellspacing cellpadding width height tr --->行 align th ...
- m个苹果放在n个盘子中有多少种结果
题目 m个苹果放在n个盘子中有多少种结果,前置条件: 允许存在空盘 重复的摆放结果忽略不计 根据题意,也就是有3种情况,的确完全重复的摆放方式是没多大意义的 思路 这题可以用枚举的描述方式进行尾递归求 ...
- 问题 C: 质因数的个数
1947: 质因数的个数 时间限制: 1 Sec 内存限制: 32 MB提交: 245 解决: 114[提交][状态][讨论版][命题人:外部导入] 题目描述 求正整数N(N>1)的质因数的 ...
- Linux SPI总线和设备驱动架构之一:系统概述
SPI是"Serial Peripheral Interface" 的缩写,是一种四线制的同步串行通信接口,用来连接微控制器.传感器.存储设备,SPI设备分为主设备和从设备两种,用 ...