spring 注解实现自动装配
2021-04-14 09:26
标签:bbb ref ase package pack http version col 官网 官网:https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/core.html#beans-annotation-config 1、导入约束 2、配置注解支持 1、pojo person.java 2、xml配置文件 如果@Autowrited自动装配的环境比较复杂,自动装配无法通过一个注解 @Autowrited 完成的时候 使用@Qualifier(value = "xx")配合使用 spring 注解实现自动装配 标签:bbb ref ase package pack http version col 官网 原文地址:https://www.cnblogs.com/wt7018/p/13337984.htmlxml 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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
context:annotation-config/>
beans>
package com.wt.pojo;
import org.springframework.beans.factory.annotation.Autowired;
public class Person {
private String name;
@Autowired
private Dog dog;
@Autowired
private Cat cat;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
}
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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
context:annotation-config/>
bean id="cat" class="com.wt.pojo.Cat"/>
bean id="dog" class="com.wt.pojo.Dog"/>
bean id="person" class="com.wt.pojo.Person" name="per">
property name="name" value="win"/>
bean>
beans>
上一篇:JavaScript 函数定义显式参数时没有指定数据类型
下一篇:spring boot:使用spring cache+caffeine做进程内缓存(本地缓存)(spring boot 2.3.1)