注解的容器配置可以减少容器的写入,在后续大型开发中,注解的使用也会常常使用,注解注入会在xml注入之前执行。
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 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的命令空间--> <!--context:annotation-config只会在当前容器中上下查找bean的注释--> <context:annotation-config/> </beans>
@Autowired
注意:@Autowired是通过byType的方式实现,必须要这个对象存在,常用。
@Autowired与@Resource
创建三个类
Address类
public class Address { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Address{" + "name='" + name + '\'' + '}'; } }
Game类
public class Game { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Game{" + "name='" + name + '\'' + '}'; } }
User类
import org.springframework.beans.factory.annotation.Autowired; public class User { private String name; @Autowired private Game game; @Autowired private Address address; public String getName() { return name; } public void setName(String name) { this.name = name; } public Game getGame() { return game; } public void setGame(Game game) { this.game = game; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", game=" + game + ", address=" + address + '}'; } }
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:p="http://www.springframework.org/schema/p" 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 class="com.yc.ch.Address" id="address" p:name="游泳"/> <bean class="com.yc.ch.Game" id="game" p:name="计算机"/> <bean class="com.yc.ch.User" id="user" p:name="Tom"/> </beans>
创建测试类,输出
User{name=‘Tom’, game=Game{name=‘计算机’}, address=Address{name=‘游泳’}}
@Qualifier的使用
@Qualifier注释主要是碰见多个相同的bean注入,但属性名不同,并不意味就会直接报错,@Autowired注释会去查找上下文与byName元素相同的属性,找到与@Qualifier指定的value值相同的bean。
xml的容器配置
<bean class="com.yc.ch.Address" id="address" p:name="游泳"/> <bean class="com.yc.ch.Address" id="address2" p:name="打排球"/> <bean class="com.yc.ch.Game" id="game" p:name="计算机"/> <bean class="com.yc.ch.User" id="user" p:name="Tom"/>
User类
@Autowired @Qualifier("address2") private Address address; //直接引用address2属性值的bean
创建测试类
User{name=‘Tom’, game=Game{name=‘计算机’}, address=Address{name=‘打排球’}}