<?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 http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
@Autowired
public void setSchool(School school) {
this.school = school;
}
<?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 http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id = "yoona" class = "com.sjf.bean.Student">
<property name="name" value="yoona"/>
<property name="age" value="24"/>
</bean>
<bean id = "xidianSchool" class = "com.sjf.bean.School">
<property name="name" value="西安电子科技大学"/>
<property name="location" value="西安"/>
</bean>
</beans>
name:yoona age:24 school:西安电子科技大学[西安] |
使用了@Autowired注解时,Spring就会尝试对该方法执行byType自动装配。 |
使用@Autowired注解不只可以使用它标注setter方法,还可以标注需要自动装配Bean引用的任意方法。甚至是构造器,@Autowired注解表示当创建Bean时,即使Spring XML文件中没有使用<constructor-arg>元素配置Bean,该构造器也需要进行自动装配。Spring会从所有满足装配条件的构造器中选择入参最多的那个构造器。还可以标注属性,并可以删除setter方法。 |
@Autowired
public void setSchool(School school) {
this.school = school;
}
@Autowired
private School school;
@Autowired
public void assemblySchool(School school) {
this.school = school;
}
@Autowired
public Student(School school) {
this.school = school;
}
应用中必须只能有一个Bean适合装配到@Autowired注解所标注的属性或者参数中。如果没有匹配的Bean,或者存在多个匹配的Bean,@Autowired注解就会遇到一些麻烦。 |
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException
:
No qualifying bean of type [com.sjf.bean.School] found for dependency: expected
at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
raiseNoSuchBeanDefinitionException(
DefaultListableBeanFactory.java:1373
)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
doResolveDependency(
DefaultListableBeanFactory.java:1119
)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
resolveDependency(
DefaultListableBeanFactory.java:1014
)
at org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(
AutowiredAnnotationBeanPostProcessor.java:618
)
... 15 more
|
@Autowired(required = false)
public void setSchool(School school) {
this.school = school;
}
required属性可以用于@Autowired注解所使用的任意地方。但是当使用构造器装配时,只有一个构造器可以将@Autowired的required属性设置true。其他使用@Autowired注解所标注的构造器只能将required属性设置为false。 |
<?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 http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id = "yoona" class = "com.sjf.bean.Student">
<property name="name" value="yoona"/>
<property name="age" value="24"/>
</bean>
<bean id = "xidianSchool" class = "com.sjf.bean.School">
<property name="name" value="西安电子科技大学"/>
<property name="location" value="西安"/>
</bean>
<bean id = "shandaSchool" class = "com.sjf.bean.School">
<property name="name" value="山东大学"/>
<property name="location" value="山东"/>
</bean>
</beans>
Caused by:
org.springframework.beans.factory.NoUniqueBeanDefinitionException
:
No qualifying bean of type [com.sjf.bean.School] is defined: expected single matching bean but found 2: xidianSchool,shandaSchool
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(
DefaultListableBeanFactory.java:1126
)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(
DefaultListableBeanFactory.java:1014
)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.
inject(
AutowiredAnnotationBeanPostProcessor.java:618
)
... 15 more
|
@Autowired
@Qualifier("shandaSchool")
public void setSchool(School school) {
this.school = school;
}
<?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 http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id = "yoona" class = "com.sjf.bean.Student">
<property name="name" value="yoona"/>
<property name="age" value="24"/>
</bean>
<bean id = "xidianSchool" class = "com.sjf.bean.School">
<property name="name" value="西安电子科技大学"/>
<property name="location" value="西安"/>
<qualifier value="ShanXi"/>
</bean>
<bean id = "shandaSchool" class = "com.sjf.bean.School">
<property name="name" value="山东大学"/>
<property name="location" value="山东"/>
<qualifier value="ShanDong"/>
</bean>
</beans>
@Autowired
@Qualifier("ShanXi")
public void setSchool(School school) {
this.school = school;
}
name:yoona age:24 school:西安电子科技大学[西安] |
@Value("yoona")
private String name;
@Value("#{systemProperties.myFavoriteSong}")
private String song;
参考:《Spring实战》
@Value("yoona")
private String name;
@Value("#{systemProperties.myFavoriteSong}")
private String song;