引用类型的注入可以使用@Autowired
@Autowired:spring框架提供的注解,实现引用类型的赋值。
spring中通过注解给引用类型赋值,使用的是自动注入原理,支持byName,byType
@Autowired:默认使用的是byType自动注入
位置:1)在属性定义的上面,无需set方法,推荐使用
2)在set方法的上面
@Autowired第一种使用方式---byType
1.在Student中先声明引用类型school,在声明school类的语句上面加入@Autowired
package com.example.ba03; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("myStudent") public class Student { @Value(value = "张飞") private String name; @Value(value = "27") private Integer age; /*引用类型 * @Autowired:spring框架提供的注解,实现引用类型的赋值。 * spring中通过注解给引用类型赋值,使用的是自动注入原理,支持byName,byType * @Autowired:默认使用的是byType自动注入 * * 位置:1)在属性定义的上面,无需set方法,推荐使用 * 2)在set方法的上面 * */ @Autowired private School school; public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", school=" + school + '}'; } }
2.声明School对象,通过注解在School类上面加入@Component以及给属性的对象赋值;另外也可以通过配置文件来赋值
package com.example.ba03; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("mySchool") public class School { @Value(value = "北京大学") private String name; @Value(value = "北京海淀区") private String address; public void setName(String name) { this.name = name; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "School{" + "name='" + name + '\'' + ", address='" + address + '\'' + '}'; } }
<?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.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/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.example.ba03"/> <bean id="myXueXiao" class="com.example.ba03.School"> <property name="name" value="清华大学"/> <property name="address" value="北京"/> </bean> </beans>
@Autowired第二种使用方式---byName
如果要使用byName方式,需要做的是:
1.在属性上面加入@Autowired
2.在属性上面加入@Qualifier(value="bean的id"):表示使用指定名称的bean完成赋值
package com.example.ba04; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("myStudent") public class Student { @Value(value = "张飞") private String name; @Value(value = "27") private Integer age; /*引用类型 * @Autowired:spring框架提供的注解,实现引用类型的赋值。 * spring中通过注解给引用类型赋值,使用的是自动注入原理,支持byName,byType * @Autowired:默认使用的是byType自动注入 * * 位置:1)在属性定义的上面,无需set方法,推荐使用 * 2)在set方法的上面 * * */ @Autowired @Qualifier("mySchool") private School school; public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", school=" + school + '}'; } }
声明School对象,通过注解在School类上面加入@Component以及给属性的对象赋值
package com.example.ba04; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("mySchool") public class School { @Value(value = "中山大学") private String name; @Value(value = "广州") private String address; public void setName(String name) { this.name = name; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "School{" + "name='" + name + '\'' + ", address='" + address + '\'' + '}'; } }
补充:当@Qualifier内的名称与引用类型中声明的@Component内的名称不同时,执行代码会出现这样的错误提示:
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'myStudent': Unsatisfied dependency expressed through field 'school';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.example.ba05.School' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=mySchool-01)}
这里Autowired(required=true)是Autowired的属性
属性:required,是boolean类型的,默认true
required=true:表示引用类型赋值失败,程序报错,并终止执行
required=fasle:表示引用类型赋值失败,程序正常执行,引用类型是null
标签:String,Autowired,springframework,快捷键,使用,org,public,name From: https://www.cnblogs.com/ttyypjt/p/17651684.html