Spring 是一个开源框架.

Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能。
Spring 是一个 IOC(DI) 和 AOP 容器框架。

具体描述 Spring:
轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
控制反转(依赖注入) (IOC----inverse of control 、DI --- dependency injection)
面向切面编程(AOP --- aspect oriented programming)
容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)

所需jar包

1
2
3
4
5
6
7
8
9
<?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">
 
    <bean name="dog" class="com.zhiqi.entity.Dog"></bean>
     
</beans>

  装配一个Bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.zhiqi.entity;
 
public class Dog {
 
    private String name;
 
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.zhiqi.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.zhiqi.entity.Dog;
 
public class T {
 
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
        Dog dog=(Dog)ac.getBean("dog");
        System.out.println(dog.getName());
    }
}

  

此时装配了Dog这个类,但是为null

需要进行依赖注入(属性注入、构造方法注入,工厂注入)

1、属性注入:

装配一个员工类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.zhiqi.entity;
 
public class Employee {
 
    private int id;
    private String name;
    private String sex;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.zhiqi.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.zhiqi.entity.Dog;
import com.zhiqi.entity.Employee;
 
public class T {
 
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
        Employee employee=(Employee)ac.getBean("employee");
        System.out.println(employee.getName());
    }
}

  

属性注入如下:<property name="" value=""></property>

1
2
3
4
5
<bean name="employee" class="com.zhiqi.entity.Employee">
        <property name="id" value="10050"></property>
        <property name="name" value="小鲜肉"></property>
        <property name="sex" value="男"></property>
    </bean>

构造方法注入一(通过类型):<constructor-arg type="" value=""></constructor-arg>

1
2
3
4
5
<bean name="employee" class="com.zhiqi.entity.Employee">
        <constructor-arg type="int" value="10010"></constructor-arg>
        <constructor-arg type="String" value="小萝莉"></constructor-arg>
        <constructor-arg type="String" value="女"></constructor-arg>
    </bean>

  此时需要写构造函数否则会报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Employee {
 
    private int id;
    private String name;
    private String sex;
     
    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Employee(int id, String name, String sex) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
    }
}

  

构造方法注入二(通过索引):<constructor-arg index="" value=""></constructor-arg>   索引从0开始

1
2
3
4
5
<bean name="employee" class="com.zhiqi.entity.Employee">
        <constructor-arg index="0" value="10010"></constructor-arg>
        <constructor-arg index="1" value="小萝莉"></constructor-arg>
        <constructor-arg index="2" value="女"></constructor-arg>
    </bean>

 可以混合使用 类型+索引

1
2
3
4
5
<bean name="employee" class="com.zhiqi.entity.Employee">
        <constructor-arg type="int" index="0" value="10010"></constructor-arg>
        <constructor-arg type="String" index="1" value="小萝莉"></constructor-arg>
        <constructor-arg type="String" index="2" value="女"></constructor-arg>
    </bean>

通过工厂注入:<bean name="employee" factory-bean="" factory-method=""></bean>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.zhiqi.factory;
 
import com.zhiqi.entity.Employee;
 
public class EmployeeFactory {
 
    public Employee createEmployee(){
        Employee employee=new Employee();
        employee.setId(10080);
        employee.setName("小霸王");
        employee.setSex("男");
        return employee;
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
<?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">
 
    <bean name="dog" class="com.zhiqi.entity.Dog"></bean>
     
    <bean name="employeeFactory" class="com.zhiqi.factory.EmployeeFactory"></bean>
    <bean name="employee" factory-bean="employeeFactory" factory-method="createEmployee"></bean>
     
</beans>

  

通过静态工厂注入:

注入参数:

1、基本类型值

2、注入bean

3、内部bean

4、null值

5、级联属性

6、集合注入(List、Set、Map)

员工有一辆车

1
2
3
4
5
6
7
8
9
<bean id="employee" class="com.zhiqi.entity.Employee">
        <property name="id" value="10060"></property>
        <property name="name" value="脑残粉"></property>
        <property name="sex" value="女"></property>
        <property name="car" ref="car"></property>
    </bean>
    <bean id="car" class="com.zhiqi.entity.Car">
        <property name="name" value="别克"></property>
    </bean>

  

一辆车只属于某员工,内部bean

1
2
3
4
5
6
7
8
9
10
<bean id="employee" class="com.zhiqi.entity.Employee">
        <property name="id" value="10060"></property>
        <property name="name" value="脑残粉"></property>
        <property name="sex" value="女"></property>
        <property name="car">
            <bean class="com.zhiqi.entity.Car">
                <property name="name" value="雪佛兰"></property>
            </bean>
        </property>
    </bean>

  

有一个员工没有车

1
2
3
4
5
6
7
8
<bean id="employee" class="com.zhiqi.entity.Employee">
        <property name="id" value="10060"></property>
        <property name="name" value="脑残粉"></property>
        <property name="sex" value="女"></property>
        <property name="car">
            <null></null>
        </property>
    </bean>

  

级联

1
2
3
4
5
6
<bean id="employee" class="com.zhiqi.entity.Employee">
        <property name="id" value="10060"></property>
        <property name="name" value="脑残粉"></property>
        <property name="sex" value="女"></property>
        <property name="car.name" value="好车"></property>
    </bean>

  此时在Car中new一个

一个员工有多个girlFriend

1
2
3
4
5
private int id;
    private String name;
    private String sex;
    private Car car;
    private List<String> girlFriend=new ArrayList<String>();

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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">
     
    <bean id="employee" class="com.zhiqi.entity.Employee">
        <property name="id" value="10060"></property>
        <property name="name" value="脑残粉"></property>
        <property name="sex" value="女"></property>
        <property name="car" value="宾利"></property>
        <property name="girlFriend">
            <list>
                <value>琪琪</value>
                <value>丽丽</value>
                <value>小莫</value>
            </list>
        </property>
    </bean>
 
</beans>

  

一个员工有多套房子

private Set<String> house=new HashSet<String>();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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">
     
    <bean id="employee" class="com.zhiqi.entity.Employee">
        <property name="id" value="10060"></property>
        <property name="name" value="脑残粉"></property>
        <property name="sex" value="女"></property>
        <property name="car" value="宾利"></property>
        <property name="girlFriend">
            <list>
                <value>琪琪</value>
                <value>丽丽</value>
                <value>小莫</value>
            </list>
        </property>
        <property name="house">
            <set>
                <value>房子1</value>
                <value>别墅2</value>
            </set>
        </property>
    </bean>
 
</beans>

  一个员工的工作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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">
     
    <bean id="employee" class="com.zhiqi.entity.Employee">
        <property name="id" value="10060"></property>
        <property name="name" value="脑残粉"></property>
        <property name="sex" value="女"></property>
        <property name="car" value="宾利"></property>
        <property name="girlFriend">
            <list>
                <value>琪琪</value>
                <value>丽丽</value>
                <value>小莫</value>
            </list>
        </property>
        <property name="house">
            <set>
                <value>房子1</value>
                <value>别墅2</value>
            </set>
        </property>
        <property name="works">
            <map>
                <entry>
                    <key><value>上午</value></key>
                    <value>撸代码</value>
                </entry>
                <entry>
                    <key><value>下午</value></key>
                    <value>找BUG</value>
                </entry>
            </map>
        </property>
    </bean>
 
</beans>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.zhiqi.test;
 
import java.util.Iterator;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.zhiqi.entity.Employee;
 
public class T {
 
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
        Employee employee=(Employee)ac.getBean("employee");
        for(String str:employee.getHouse()){
            System.out.println(str);
        }
        //entrySet()将map转为set,然后迭代器遍历
        Iterator it=employee.getWorks().entrySet().iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}
  •   

Spring属性注入、构造方法注入、工厂注入以及注入参数(转)的更多相关文章

  1. (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)

    Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...

  2. Spring 属性注入(三)AbstractNestablePropertyAccessor

    Spring 属性注入(三)AbstractNestablePropertyAccessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117 ...

  3. Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用

    Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/101174 ...

  4. Spring 属性注入(二)BeanWrapper 结构

    Spring 属性注入(二)BeanWrapper 结构 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) BeanWrap ...

  5. Spring 属性注入(四)属性键值对 - PropertyValue

    Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...

  6. spring属性注入

    1,set方法注入 (1)对于值类型的属性: 在对象中一定要有set方法 package com.songyan.demo1; import com.songyan.injection.Car; /* ...

  7. spring属性注入方式

    一.使用有参构造注入属性 配置文件 constructor-arg标签是需注入属性的名字 User类 生成了User的有参构造函数 测试类 结果 打印出了name属性的值 二.使用set方法注入属性 ...

  8. Spring IOC源代码具体解释之容器依赖注入

    Spring IOC源代码具体解释之容器依赖注入 上一篇博客中介绍了IOC容器的初始化.通过源代码分析大致了解了IOC容器初始化的一些知识.先简单回想下上篇的内容 加载bean定义文件的过程.这个过程 ...

  9. 三大框架 之 Spring(IOC控制反转、DI依赖注入)

    目录 常用词汇 left join与left outer join的区别 Struts2的标签库导入 Spring Spring概述 什么是Spring spring特点 下载 IOC 什么IOC 传 ...

  10. Spring升级案例之IOC介绍和依赖注入

    Spring升级案例之IOC介绍和依赖注入 一.IOC的概念和作用 1.什么是IOC 控制反转(Inversion of Control, IoC)是一种设计思想,在Java中就是将设计好的对象交给容 ...

随机推荐

  1. Hbase 学习笔记1----shell

    Hbase 是一个分布式的.面向列的开源数据库,其实现是建立在google 的bigTable 理论之上,并基于hadoop HDFS文件系统.     Hbase不同于一般的关系型数据库(RDBMS ...

  2. iOS学习之二维码扫描

    这几天刚好将本人高仿新浪微博的事情进行一个阶段性的tag,在此也将这个项目在实现二维码扫描这个功能来做一个简要的记录.关于高仿新浪微博的源代码,本人已经将全部代码托管到github,地址在这里.欢迎大 ...

  3. 微信小程序将带来web程序员的春天!

    微信之父张小龙在年初那次演讲中曾表示:“我自己是很多年的程序员,我觉得我们应该为开发的团体做一些事情.”几个月后,微信正式推出微信应用号(即微信小程序)在互联网中掀起又一波热潮. 过去,对于很多开发者 ...

  4. Canvas:橡皮筋线条绘制

    Canvas:橡皮筋线条绘制 效果演示 实现要点 事件监听 [说明]: 在Canvas中检测鼠标事件是非常简单的,可以在canvas中添加一个事件监听器,当事件发生时,浏览器就会调用这个监听器. 我们 ...

  5. iptables打开22,80,8080,3306等端口

    systemctl stop firewalld systemctl mask firewalld Then, install the iptables-services package: yum i ...

  6. 用HAProxy和KeepAlived构建高可用的反向代理系统

    对于访问量较大的网站来说,随着流量的增加单台服务器已经无法处理所有的请求,这时候需要多台服务器对大量的请求进行分流处理,即负载均衡.而如果实现负载均衡,必须在网站的入口部署服务器(不只是一台)对这些请 ...

  7. linux命令详解之(at)

    在Linux下,有两个命令可以用来作为计划任务而执行,at:一次性定时任务计划执行crontab :每天定时任务计划执行 以下仅说一下一次性任务计划执行(at)要使用一次性任务计划,linux必须要有 ...

  8. 微信小程序:其中wxml和wxss的样式说明

    微信小程序:其中wxml和wxss的样式说明 一.简介 对于css不熟悉的Android程序员来说,开发微信小程序面临的一个比较困难的问题就是界面的排版了.微信小程序的排版就跟wxml和wxss有关了 ...

  9. EasyUI:年份、月份下拉框Demo

    EasyUI:年份.月份下拉框Demo jsp中定义: <td width="10%" height="25px" style="text-al ...

  10. ASP.NET MVC 处理404与500错误页面的方法

    第一步创建ErrorPageController 第二步添加Oops页面 @{ ViewBag.Title = "Oops"; Layout = "~/Areas/Adm ...