1. 在pom.xml的依赖
  <dependencies>
<!--测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.11</version>
</dependency>
<!--spring 上下文-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
<!--织入包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!--高速代理生成包-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency> </dependencies>

2.创建实体类Person和Computer两个实体类

package com.qf.pojo;

public class Computer {
String disk;
String cpu;
String memory;
String gpu; public Computer() {
System.out.println("空参构造");
} public Computer(String disk, String cpu, String memory, String gpu) {
this.disk = disk;
this.cpu = cpu;
this.memory = memory;
this.gpu = gpu;
} public String getDisk() {
return disk;
} public void setDisk(String disk) {
this.disk = disk;
} public String getCpu() {
return cpu;
} public void setCpu(String cpu) {
this.cpu = cpu;
} public String getMemory() {
return memory;
} public void setMemory(String memory) {
this.memory = memory;
} public String getGpu() {
return gpu;
} public void setGpu(String gpu) {
this.gpu = gpu;
} @Override
public String toString() {
return "Computer{" +
"disk='" + disk + '\'' +
", cpu='" + cpu + '\'' +
", memory='" + memory + '\'' +
", gpu='" + gpu + '\'' +
'}';
}
}
package com.qf.pojo;

import java.util.List;

public class Person {
String name;
int age;
Computer computer;
List<String> girlFriend; 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 Computer getComputer() {
return computer;
} public void setComputer(Computer computer) {
this.computer = computer;
} public List<String> getGirlFriend() {
return girlFriend;
} public void setGirlFriend(List<String> girlFriend) {
this.girlFriend = girlFriend;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", computer=" + computer +
", girlFriend=" + girlFriend +
'}';
}
}

3.配置spring-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--Id和name:使用基本一致,name允许有一些特殊字符,name和id都唯一-->
<!--scope默认为singleton-->
<!--如果是属性注入,需要为每一个依赖类创建相应的getter和setter方法
-->
<!--属性装配-->
<bean class="com.qf.pojo.Computer" name="myComputer" scope="prototype" id="computer1">
<property name="disk" value="i7 7700HQ"></property>
<property name="memory" value="16GB"></property>
<property name="cpu" value="256G SSD 1TB"></property>
<property name="gpu" value="NVIDIA 1060"></property>
</bean> <!--构造装配,Computer必须有有参构造和无参构造-->
<bean class="com.qf.pojo.Computer" name="computer">
<constructor-arg name="cpu" value="i9 9700HQ"></constructor-arg>
<constructor-arg name="disk" value="512G SSD"></constructor-arg>
<constructor-arg name="gpu" value="NVIDIA 2080"></constructor-arg>
<constructor-arg name="memory" value="64G"></constructor-arg>
</bean> <!--自动装配autowire,默认按byType自动装配-->
<!--下面没有装配Computer,autowire会自动装配-->
<bean class="com.qf.pojo.Person" name="zhangsan" autowire="byName">
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
<property name="girlFriend">
<!--集合装配-->
<list>
<value>小娜</value>
<value>小莎</value>
<value>小琪</value>
</list>
</property>
</bean> </beans>

自动装配

  • byName:通过反射到Person类,找到没有装配的Computer computer,根据computer和每个bean的name/id匹配,相同就装配,如果没有相同的就抛出异常。
  • byType:通过反射到Person类,找到没有装配的Computer属性, 按Bean的包名.class的class来匹配的,如果有多个class相同抛异常。
  • autowired按byType自动注入.

4.创建测试类TestSpringIoc

public class TestSpringIoc {
public static void main(String[] args) {
<!--可以初始化多个xml-->
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-ioc.xml"); Computer computer1 =(Computer)context.getBean("computer1");
System.out.println(computer1); Person person = (Person)context.getBean("zhangsan",Person.class);
System.out.println(person); }
}

集合装配:如果对应bean的属性是一个集合则需要使用集合装配,Spring支持多种集合装配类型:
  • 对应类型为java.util.List、java.util.Vector 、java.util.Collection 、数组的属性
  • 对应类型为java.util.Set的属性
  • 对应类型为java.util.Map的属性
  • 对应类型为java.util.Properties的属性
示例
<!--List装配:-->
<property name=“list">
<list>
<value>a</value>
<value>b</value>
</list>
</property> <!--Set装配:(HashSet如何去重复,hashCode)-->
<property name=“set">
<set>
<value>a</value>
<value>b</value>
</set>
</property> <!--Map装配:-->
<property name=“map">
<map>
<entry key="1" value=“a"></entry>
<entry key="2" value=“b"></entry>
</map>
</property>

Annotation注解模式实现装配

1.修改spring-ioc.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--声明使用注解开发-->
<context:annotation-config></context:annotation-config>
<!--组件扫描,扫描com.qf.pojo,如果含有spring组件注解,将被spring容器管理-->
<context:component-scan base-package="com.qf.pojo"></context:component-scan> </beans>

2.创建新的PersonAnnotation类

<!--定义为spring组件-->
@Component("personAnnotation")
public class PersonAnnotation { @Value("18")
int age;
@Value("张三")
String name; public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "PersonAnnotation{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}

3.创建新的测试类

public class AnnotationTest {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc-annotation.xml"); PersonAnnotation personAnnotation = applicationContext.getBean("personAnnotation", PersonAnnotation.class);
System.out.println(personAnnotation);
}
}
  • Annotation的优缺点:

    • 优点:灵活、简单、开发效率高
    • 缺点:在不合适的情况下,滥用注解会违反ioc的思想

(四)SpringIoc之Bean装配的更多相关文章

  1. Spring Boot源码(四):Bean装配

    为了演示Spring中对象是如何创建并放到spring容器中,这里新建一个maven项目: 其中pom.xm文件中只引入了一个依赖: <dependencies> <dependen ...

  2. Spring使用笔记(二)Bean装配

    Bean装配 Spring提供了3种装配机制: 1)隐式的Bean发现机制和自动装配 2)在Java中进行显示装配 3)在XML中进行显示装配 一)自动化装配 1.指定某类为组件类: @Compone ...

  3. 使用Spring IoC进行Bean装配

    Spring概述 Spring的设计严格遵从的OCP(开闭原则),保证对修改的关闭,也就是外部无法改变spring内部的运行流程:提供灵活的扩展接口,也就是可以通过extends,implements ...

  4. Spring Bean装配(下)——注解

    @Repository,@Service,@Controller这三个注解是基于component定义的注解 component-scan:组件扫描 base-package:扫描这个下的所有类 &l ...

  5. 【Spring】Spring的bean装配

    前言 bean是Spring最基础最核心的部分,Spring简化代码主要是依赖于bean,下面学习Spring中如何装配bean. 装配bean Spring在装配bean时非常灵活,其提供了三种方式 ...

  6. 使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”

    使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”. 原 ...

  7. Spring中四种实例化bean的方式

    本文主要介绍四种实例化bean的方式(注入方式) 或者叫依赖对象实例化的四种方式.上面的程序,创建bean 对象,用的是什么方法 ,用的是构造函数的方式 (Spring 可以在构造函数私有化的情况下把 ...

  8. Spring对Bean装配详解

    1.Spring提供了三种装配bean的方式: 2.自动装配bean: 3.通过Java代码装配bean 4.通过XML装配bean 前言:创建对象的协作关系称为装配,也就是DI(依赖注入)的本质.而 ...

  9. Spring Bean装配学习

    解释:所谓装配就是把一个类需要的组件给它设置进去,英文就是wire,wiring:注解Autowire也叫自动装配. 目前Spring提供了三种配置方案: 在XML中进行显式的配置 在Java中进行显 ...

随机推荐

  1. oracle官方文档_查看初始化參数(举例)

    原创作品,出自 "深蓝的blog" 博客.深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46864217 记录 ...

  2. 暴力破解unix/linux平台上采用crypt加密的口令

    # coding=utf-8 ''' 暴力破解crypt模块加密的密码 ''' import crypt import optparse usage = 'Usage: %prog [optinos] ...

  3. 【iOS系列】-使用CAGradientLayer设置渐变色

    有时候iOS开发中需要使用到渐变色,来给图片或者view盖上一层,使其显示效果更好,我们这里使用的CAGradientLayer来设置渐变色 要实现的效果如下: Demo地址---下载 // 创建渐变 ...

  4. 2016/3/30 租房子 ①建立租房子的增、删、改php页面 ②多条件查询 ③全选时 各部分全选中 任意checkbox不选中 全选checkbox不选中

    字符串的另一种写法:<<<AAAA; 后两个AA回车要求顶格  不然报错 例子: <!DOCTYPE html> <html lang="en" ...

  5. 正向代理tinyproxy使用总结

    使用tinyproxy的问题背景: 其实以前代理一直用的是apache,后来,那次有个任务要给ios的推送设置代理,任务很紧急,可是apache报错. 原因如下:APNS发送通知的端口2195,但是A ...

  6. lambda和抽象类

    lambda的使用条件是‘一个接口仅有一个待实现的方法’: so,lambda不能使用在抽象类上,使用后或提示‘Target type of a lambda conversion must be a ...

  7. [10.27_P3] 简单题 (脑洞)

    Description dzy 手上有一张n 个点m 条边的联通无向图,仙人掌是一张每条边最多在一个简单环内的联通无向图.他想求这个无向图的生成仙人掌中最多有多少条边. 但是dzy 觉得这个问题太简单 ...

  8. 删除oracle数据库用户的dba权限(当出现同一用户DBA可以登录,normal不能登录)“无法对SYS拥有的对象创建触发器”

    系统报错:“无法对SYS拥有的对象创建触发器”,搞不懂是什么原因了,到底这触发器要用什么用户才能建立啊? ORA-04089: 无法对 SYS 拥有的对象创建触发器 第一种方式: 首先,用sys用户a ...

  9. APACHE2 服务器配置 (二) 默认端口***

    如将默认的80端口修改为9000 不管怎样,只要你选择合适的端口(电信商没有封的),进行如下的设置即可: 1, 修改 /etc/apache2/ports.conf 将 NameVirtualHost ...

  10. python3 + selenum 环境搭建

    一.安装python3 打开python3官网https://www.python.org/,选择下载相应平台版本. 下载完成之后,根绝需要安装.注意:在安装时需勾选左下角“add python to ...