1.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:component-scan base-package="test10month.test1012"/>
</beans>
2.类
interface AttributeSpringInterface {
void testinterface();
}
@Component("attributeSpring")
class AttributeSpring implements AttributeSpringInterface {
@Override
public void testinterface() {
System.out.println("属性注入测试1");
}
}
@Component("attributeSpring2")
class AttributeSpring2 implements AttributeSpringInterface {
@Override
public void testinterface() {
System.out.println("属性注入测试2");
}
}
@Component
class AttributeSpringClass {
@Autowired
@Qualifier(value = "attributeSpring")
private AttributeSpringInterface attributeSpringInterface;
@Value("这是string")
private String string;
public void rod() {
System.out.println("这是rod");
System.out.println(string);
attributeSpringInterface.testinterface();
}
}
3.测试类
package test10month.test1012;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
public class AttributeSpringTest {
public static void main(String[] args) {
var context = new ClassPathXmlApplicationContext("test10month/test1012/AttributeSpring.xml");
var attributeSpringClass = context.getBean("attributeSpringClass", AttributeSpringClass.class);
attributeSpringClass.rod();
}
}