目录
使用注解的两个必要步骤
正文
Cat
Dog
Animal
beans.xml
测试
@Qualifier
使用注解的两个必要步骤
1.导入约束
<?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">
</beans>
2.配置注解的支持
<?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>
<context:annotation-config/> //配置注解的支持
正文
Cat
package org.example;
public class Cat {
public void eat(){
System.out.println("猫吃鱼干");
}
}
Dog
package org.example;
public class Dog {
public void sleep(){
System.out.println("小狗睡大觉");
}
}
Animal
package org.example;
import org.springframework.beans.factory.annotation.Autowired;
public class Animal {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Animal{" +
"cat=" + cat +
", dog=" + dog +
'}';
}
}
注意:要加上自动装配注解
set方法可以去掉也可以不用去掉
去掉的话如下图
package org.example;
import org.springframework.beans.factory.annotation.Autowired;
public class Animal {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
@Override
public String toString() {
return "Animal{" +
"cat=" + cat +
", dog=" + dog +
'}';
}
}
beans.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.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="cat" class="org.example.Cat" />
<bean id="dog" class="org.example.Dog" />
<bean id="animal" class="org.example.Animal" />
</beans>
测试
package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test9 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Animal animal=(Animal) context.getBean("animal");
animal.getCat().eat();
animal.getDog().sleep();
}
}
@Qualifier
与注解@Autowired配合使用
<bean id="cat111" class="org.example.Cat" /> <bean id="cat222" class="org.example.Cat" /> <bean id="cat333" class="org.example.Cat" />如果有多个关于cat的bean的话,使用@Qualifier可以显式指定哪一个bean注入,