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) : 自动装配的更多相关文章

  1. Spring(六)之自动装配

    一.自动装配模型 下面是自动连接模式,可以用来指示Spring容器使用自动连接进行依赖注入.您可以使用元素的autowire属性为bean定义指定autowire模式. 可以使用 byType 或者  ...

  2. Spring 由构造函数自动装配

    Spring 由构造函数自动装配,这种模式与 byType 非常相似,但它应用于构造器参数. Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 ...

  3. 【面试普通人VS高手系列】Spring Boot中自动装配机制的原理

    最近一个粉丝说,他面试了4个公司,有三个公司问他:"Spring Boot 中自动装配机制的原理" 他回答了,感觉没回答错误,但是怎么就没给offer呢? 对于这个问题,看看普通人 ...

  4. Spring基础知识之装配Bean

    装配(wiring):创建应用对象之间协作关系的行为.这是依赖注入的本质. Spring配置的可选方案 Spring提供了三种装配机智: 1)在XML中进行显示装配 2)在java中进行显示装配 3) ...

  5. Spring基于注解自动装配

    前面我们介绍Spring IoC装载的时候,使用XML配置这种方法来装配Bean,这种方法可以很直观的看到每个Bean的依赖,但缺点也很明显:写起来非常繁琐,每增加一个组件,就必须把新的Bean配置到 ...

  6. Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...

  7. Spring 学习——Spring注解——Autowiring(自动装配)

    装配方式 方式一:默认 方式二:byName:根据属性名称自动装配.会查找Bean容器内部所有初始化的与属性名成相同的Bean,自动装配.(需要通过set方法注入,注入Bean的id名称需要和实体类的 ...

  8. spring bean autowire自动装配

    转自:http://blog.csdn.net/xiao_jun_0820/article/details/7233139 autowire="byName"会自动装配属性与Bea ...

  9. Spring学习笔记--自动装配Bean属性

    Spring提供了四种类型的自动装配策略: byName – 把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中. byType – 把与Bean的属性具有相同类型 ...

随机推荐

  1. java 异步机制与同步机制的区别

    所谓异步输入输出机制,是指在进行输入输出处理时,不必等到输入输出处理完毕才返回.所以异步的同义语是非阻塞(None Blocking). 网上有很多网友用很通俗的比喻  把同步和异步讲解的很透彻 转过 ...

  2. c# file 上传EXCEL文件,以流的形式读取数据

    1.引入  Aspose.Cells public void test() { HttpFileCollection filelist = HttpContext.Current.Request.Fi ...

  3. 几款移动跨平台App开发框架比较

    整理目前流行的跨平台WebApp开发技术的特点,仅供参考. 每个框架几乎都包含以下特性: 使用 HTML5 + CSS + JavaScript 开发 跨平台重用代码 丰富的UI库 提供访问设备原生A ...

  4. 用C#中的键值对遍历数组或字符串元素的次数

    代码如下: string strs = "ad6la4ss42d6s3"; Dictionary<char, int> dic = new Dictionary< ...

  5. Git安装学习记录

    1.下载: https://gitforwindows.org/ 2.安装:https://blog.csdn.net/chengyuqiang/article/details/54178683 3. ...

  6. kls与flag(map)

    题目传送门 这道题还挺搞笑的,\(map\)常数贼大还是把它水过了. 可以发现这道题求的是 \((j>i)j-i=h_i+h_j,j-i=|h_i-h_j|\)的对数. 那么显然,因为高度大于\ ...

  7. Windows下Mongodb安装及配置

    安装文件:MongoDB-win32-x86_64-2008plus-ssl-3.2.6-signed.msi 电脑配置:win7 64位 mongodb的安装很简单,设置好安装路径后,一直Next直 ...

  8. eclipse上搭建mybatis

    1..在help中打开 2.搜索mybatipse 3:功能简介 1:要查找某一个方法        在dao接口中某一个方法中 按住 Ctrl键 鼠标指到方法名称上 选择open xml 就会自动跳 ...

  9. Shell - 简明Shell入门09 - 重定向(Redirection)

    示例脚本及注释 #!/bin/bash pwd > 1.log # 输出重定向到指定文件 date 1> 1.log # ">"与"1>" ...

  10. Linux - route & traceroute & ip

    route route - show / manipulate the IP routing table route 命令常用命令示例 #显示路由 route route -n # 不解析名字,快速显 ...