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的属性具有相同类型 ...
随机推荐
- IIS日志存入数据库之一:ODBC
园内@Fish Li的文章<IIS日志-网站运维的好帮手>中介绍将IIS的文本格式的文件导入数据库的方法.在实践中,我们发现导数据的速度很慢,一个200M的日志文件居然要近100分钟.我们 ...
- await和async在C#一般处理程序(ashx)中的使用
public class hello : HttpTaskAsyncHandler, IReadOnlySessionState { public IFetchServise fetch { get; ...
- 代码面试集锦 1 - Uber
Given an array of integers, return a new array such that each element at index i of the new array is ...
- Django:haystack全文检索详细教程
参考:https://blog.csdn.net/AC_hell/article/details/52875927 一.安装第三方库及配置 1.1 安装插件 pip install whoosh dj ...
- 通过JS拦截 pushState 和 replaceState 事件
history.pushState 和 history.replaceState 可以在不刷新当前页面的情况下更改URL,但是这样就无法获取通过AJAX得到的新页面的内容了.虽然各种HTML5文档说 ...
- 重写TreeView,自定义图标,生成通行的下划线,取消默认获得焦点失去焦点的效果,并支持拖拽节点到外界
1.运行效果: 2.前端代码 <UserControl x:Class="iPIS.UI.Base.Tree.VideoTreeControl" xmlns="ht ...
- MVC 5使用ViewBag(对象)显示数据
前面Insus.NET有演示使用ViewData来实现控制器与视图的通讯.如果想了解的话,可以从下面两个链接可以查看:<MVC 5使用ViewData(对象)显示数据>http://www ...
- css中如何实现左边的高度随着右边改变而改变
css中如何实现左边的高度随着右边改变而改变 html结构: <div class="main"> <div class="main_left" ...
- 《Python自动化运维之路》 业务服务监控(二)
文件内容差异对比方法 使用diffie模块实现文件内容差异对比.dmib作为 Python的标准库模块,无需安装,作用是对比文本之间的差异,且支持输出可读性比较强的HTML文档,与 Linux下的di ...
- Django(母版和继承)
day66 参考:http://www.cnblogs.com/liwenzhou/p/7931828.html#autoid-2-3-6 内容回顾 1. 模板系统(字符串替换) ...