标签:容器 String Autowired spring Address address 注解 public name
基于spring注解的容器配置
注解的容器配置可以减少容器的写入,在后续大型开发中,注解的使用也会常常使用,注解注入会在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对应的bean只定义了一个构造函数,则不需要在此构造函数上进行注释,但是,如果有多个构造函数,则必须对其中至少一个构造函数进行注释,指明容器要使用哪个构造函数。
- 应用Setter方法。
- 应用于定义属性
- 强类型集合或者数组
注意:@Autowired是通过byType的方式实现,必须要这个对象存在,常用。
@Autowired与@Resource
- @Autowired是通过byType实现,没有对应的属性类型则会报错。
- @Resource可以通过byName和byType两种方式实现,默认是byName方式,如果没有找到名字,则会通过byType方式实现,如果两个都没有找到,则会直接报错。
创建三个类
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=‘打排球’}}
标签:容器,String,Autowired,spring,Address,address,注解,public,name 来源: https://blog.csdn.net/m0_46813809/article/details/120927690
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。