Spring基础(1) : 自动装配
1.自动装配
1.1 byType
1.1.1根据类型自动匹配,若当前没有类型可以注入或者存在多个类型可以注入,则失败。必须要有对于的setter方法
public class Person{
public String name;
public int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString(){
return this.name+" "+this.age;
}
}
public class Group {
public Person pafter;
public Person getPafter() {
return pafter;
}
public void setPafte(Person pafter) {
this.pafter = pafter;
}
}
public class Main {
static ApplicationContext context = new ClassPathXmlApplicationContext("a.xml");
public static void main(String[] args){
Group g = context.getBean("g1",Group.class);
System.out.println(g.getPafter().toString());
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="pafter" class="com.Person">
<property name="name" value="liaohang"/>
<property name="age" value="26"/>
</bean>
<bean id="p2" class="com.Person" autowire-candidate="false">
<property name="name" value="ZhangSan"/>
<property name="age" value="26"/>
</bean>
<bean id="g1" class="com.Group" autowire="byType">
</bean>
</beans>
这个xml存在两个person bean,为了避免歧义,将p2设置autowire-candidate="false",则容器会自动过滤p2,最终p1被注入到group中。
1.1.2 根据名称自动注入,是指 bean名称与setter方法后缀名称 匹配
<bean id="p1234" class="com.Person">
<property name="name" value="liaohang"/>
<property name="age" value="26"/>
</bean>
<bean id="g1" class="com.Group" autowire="byName"/> public class Group {
public Person pafter;
public Person getPafter() {
return pafter;
}
public void setP1234(Person pafter) {
this.pafter = pafter;
}
}
上面的xml配置和 bean则可以自动注入。
1.13 根据构造函数注入,是按构造函数参数中的类型进行自动注入,与构造函数参数名称无关。
<bean id="p2" class="com.Person" autowire-candidate="false">
<property name="name" value="liaohang"/>
<property name="age" value="26"/>
</bean>
<bean id="g1" class="com.Group" autowire="constructor"/> public class Group {
public Person pafter;
public Person getPafter() {
return pafter;
} public Group(Person p1){
this.pafter =p1;
}
} public class Main {
static ApplicationContext context = new ClassPathXmlApplicationContext("a.xml");
public static void main(String[] args){
Group g = context.getBean("g1",Group.class);
System.out.println(g.getPafter().toString());
}
}
2.自动装配注解使用
@Configuration
@ComponentScan
public class Config1 { } @Component
public class Leader {
public String lead ="hello leader";
} @Component
@PropertySource("p.properties")
public class Person { @Value("${name}")
public String name; @Autowired
public Leader leader; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
} public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(Config1.class);
Person p = context.getBean("person",Person.class);
System.out.println(p.leader.lead);
}
打印:
hello leader
Spring基础(1) : 自动装配的更多相关文章
- Spring(六)之自动装配
一.自动装配模型 下面是自动连接模式,可以用来指示Spring容器使用自动连接进行依赖注入.您可以使用元素的autowire属性为bean定义指定autowire模式. 可以使用 byType 或者 ...
- Spring 由构造函数自动装配
Spring 由构造函数自动装配,这种模式与 byType 非常相似,但它应用于构造器参数. Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 ...
- 【面试普通人VS高手系列】Spring Boot中自动装配机制的原理
最近一个粉丝说,他面试了4个公司,有三个公司问他:"Spring Boot 中自动装配机制的原理" 他回答了,感觉没回答错误,但是怎么就没给offer呢? 对于这个问题,看看普通人 ...
- Spring基础知识之装配Bean
装配(wiring):创建应用对象之间协作关系的行为.这是依赖注入的本质. Spring配置的可选方案 Spring提供了三种装配机智: 1)在XML中进行显示装配 2)在java中进行显示装配 3) ...
- Spring基于注解自动装配
前面我们介绍Spring IoC装载的时候,使用XML配置这种方法来装配Bean,这种方法可以很直观的看到每个Bean的依赖,但缺点也很明显:写起来非常繁琐,每增加一个组件,就必须把新的Bean配置到 ...
- Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...
- Spring 学习——Spring注解——Autowiring(自动装配)
装配方式 方式一:默认 方式二:byName:根据属性名称自动装配.会查找Bean容器内部所有初始化的与属性名成相同的Bean,自动装配.(需要通过set方法注入,注入Bean的id名称需要和实体类的 ...
- spring bean autowire自动装配
转自:http://blog.csdn.net/xiao_jun_0820/article/details/7233139 autowire="byName"会自动装配属性与Bea ...
- Spring学习笔记--自动装配Bean属性
Spring提供了四种类型的自动装配策略: byName – 把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中. byType – 把与Bean的属性具有相同类型 ...
随机推荐
- 关于2011年meng-meng组产品《豆酱》的Review
这个组是一个做手机应用的组,比较有特色. 经过我们的一致讨论,得出我们组对前辈的有关选题.团队.产品等几个方面的看法,以及我们的感想. 选题的特点: 这个选题对于一个短期项目来说是很合适的,经过较为详 ...
- C#如何在List里求某一列的數值的和SUM
var X=Xlist.Sum(key => key.XXX);
- JQuery Mobile - 导航栏选中状态代码
class="ui-btn-active" 参考: https://wizardforcel.gitbooks.io/w3school-jqmobile/content/8.htm ...
- Java并发编程总结5——ThreadPoolExecutor
一.ThreadPoolExecutor介绍 在jdk1.8中,构造函数有4个.以 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, ...
- spring-boot集成thymeleaf。
thymeleaf是前台页面展示,原来一直是jsp,jsp中包含很多服务器端的逻辑,逐渐淘汰.同样功能的还有freemarker.孰好孰坏不予评价,只做简单实现. 1.基本思路 (1)pom.xml中 ...
- 原子操作--sync/atomic的用法
golang 通过sync/atomic库来支持cpu和操作系统级别的原子操作.但是对要操作类型有如下要求 int32, int64,uint32, uint64,uintptr,unsafe包中的P ...
- python实用库:PrettyTable 学习
python实用库:PrettyTable 学习 PrettyTable说明 PrettyTable 是python中的一个第三方库,可用来生成美观的ASCII格式的表格,十分实用. 以下为官方介绍: ...
- 3DMax——室内设计:墙体+吊顶
1.导入CAD平面图 2.将导入的平面图全部选中→颜色设置为其他颜色→设置为组(设置为组,是为了后期选材质方便) 3.选中图形,选择移动工具,输入坐标为0,右键选择冻结当前选择 4.右键“角度捕捉切换 ...
- 菜鸟--shell脚本编写之解决问题篇
一.执行时发现adb shell进入设备后不再继续往下执行了 adb shell cd /system/plugin/....exit 在网上查到的都是bat文件调用adb shell,没有sh文件调 ...
- 12-02 java String类
String构造方法 package cn.itcast_01; /* * 字符串:就是由多个字符组成的一串数据.也可以看成是一个字符数组. * 通过查看API,我们可以知道 * A:字符串字面值&q ...