5、Spring配置

5.1、别名

<!--别名,如果添加了别名,我们也可以使用别名获取到这个对象-->
<alias name="user" alias="userAlias"/>

5.2、bean的配置

<!--
id:bean的唯一标识符,也就是相当于我们学的对象名
class:bean对象所对应的全限定名:包名+类名
name:也是别名,而且name可以同时取多个别名
-->
<bean id="userT" class="com.rui.pojo.UserT" name="user2 u2,u3;u4">
</bean>

5.3、import

这个import,一般用于团队开发使用,他可以将多个配置文件,导入合并为一个

假设现在项目中有多个人开发,这三个人负责不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个总的!

  • 张三
  • 李四
  • 王五
  • applicationContext.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
</beans>

使用的时候,直接使用总的配置就可以了

6、依赖注入

6.1、构造器注入

前面已经说过

6.2、Set方式注入【重点】

  • 依赖注入:Set注入!

    • 依赖:bean对象的创建依赖于容器
    • 注入:bean对象中的所有属性,由容器来注入!

【环境搭建】

  1. 复杂类型

    package com.rui.pojo;
    
    public class Address {
    private String address; public String getAddress() {
    return address;
    } public void setAddress(String address) {
    this.address = address;
    }
    }
  2. 真实测试对象

    package com.rui.pojo;
    
    import java.util.*;
    
    public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info; public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public Address getAddress() {
    return address;
    } public void setAddress(Address address) {
    this.address = address;
    } public String[] getBooks() {
    return books;
    } public void setBooks(String[] books) {
    this.books = books;
    } public List<String> getHobbys() {
    return hobbys;
    } public void setHobbys(List<String> hobbys) {
    this.hobbys = hobbys;
    } public Map<String, String> getCard() {
    return card;
    } public void setCard(Map<String, String> card) {
    this.card = card;
    } public Set<String> getGames() {
    return games;
    } public void setGames(Set<String> games) {
    this.games = games;
    } public String getWife() {
    return wife;
    } public void setWife(String wife) {
    this.wife = wife;
    } public Properties getInfo() {
    return info;
    } public void setInfo(Properties info) {
    this.info = info;
    } @Override
    public String toString() {
    return "Student{" +
    "name='" + name + '\'' +
    ", address=" + address +
    ", books=" + Arrays.toString(books) +
    ", hobbys=" + hobbys +
    ", card=" + card +
    ", games=" + games +
    ", wife='" + wife + '\'' +
    ", info=" + info +
    '}';
    }
    }
  3. beans.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
    https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="student" class="com.rui.pojo.Student">
    <!--第一种,普通注入,value-->
    <property name="name" value="尹锐"/>
    </bean>
    </beans>
  4. 测试类

    package com.rui;
    
    import com.rui.pojo.Student;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest {
    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    Student student = (Student) context.getBean("student");
    System.out.println(student.getName());
    }
    }

完善注入信息

<?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
https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.rui.pojo.Address">
<property name="address" value="杭州"></property>
</bean>
<bean id="student" class="com.rui.pojo.Student">
<!--第一种,普通注入,value-->
<property name="name" value="尹锐"/> <!--第二种,Bean注入,ref-->
<property name="address" ref="address"/> <!--数组注入,ref-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property> <!--List-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>看电影</value>
</list>
</property> <!--Map-->
<property name="card">
<map>
<entry key="身份证" value="111222333344445555"/>
<entry key="银行卡" value="11123123123123123123"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property> <!--null-->
<property name="wife">
<null/>
</property> <!--Properties-->
<property name="info">
<props>
<prop key="driver">171030338</prop>
<prop key="url">171030338</prop>
<prop key="username">171030338</prop>
<prop key="password">171030338</prop>
</props>
</property>
</bean>
</beans>

6.3、拓展方式注入

我们可以使用p命名空间和c命名空间进行注入

官方解释:

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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.rui.pojo.User" p:name="尹锐" p:age="18"/> <!--c命名空间注入,通过构造器注入:construct-args-->
<bean id="user2" class="com.rui.pojo.User" c:age="18" c:name="尹锐"/>
</beans>

测试:

@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = (User) context.getBean("user2");
System.out.println(user);
}

注意点:p命名空间和c命名空间不能直接使用,需要导入xml约束!

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

6.4、bean的作用域

  1. 单例模式(Spring默认机制)

        <bean id="user2" class="com.rui.pojo.User" c:age="18" c:name="尹锐" scope="singleton"/>
  2. 原型模式:每次从容器中get的时候,都会产生一个新对象

        <bean id="user2" class="com.rui.pojo.User" c:age="18" c:name="尹锐" scope="prototype"/>
    
    
  3. 其余的request、session、application、这些只能在web开发中用到!

Spring-Spring配置-依赖注入的更多相关文章

  1. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework的依赖注入和控制反转

    Dependency Injection and Inversion of Control 1.概述: 1.1相关概念 bean:由IoC容器所管理的对象,也即各个类实例化所得对象都叫做bean 控制 ...

  2. 谈谈自己了解的spring.NET的依赖注入

         spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection),以达到解耦的目的,实现模块的组件化.程序 ...

  3. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  4. spring六种种依赖注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  5. spring.NET的依赖注入

    谈谈自己了解的spring.NET的依赖注入   spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection), ...

  6. spring 四种依赖注入方式以及注解注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  7. JavaEE开发之Spring中的依赖注入与AOP

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  8. JavaEE开发之Spring中的依赖注入与AOP编程

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  9. spring几种依赖注入方式以及ref-local/bean,factory-bean,factory-method区别联系

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  10. 【Spring IoC】依赖注入DI(四)

    平常的Java开发中,程序员在某个类中需要依赖其它类的方法.通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由程 ...

随机推荐

  1. springboot使用activemq同时接收queue和topic消息

    原文链接:https://blog.csdn.net/jia_costa/article/details/79354478 新建springboot项目, pom文件如下 <?xml versi ...

  2. Java面试 - 重载(Overload)和重写(Override)的区别?

    1.重载是在同一个类中,可声明多个同名方法,但参数列表不同(参数顺序,个数,类型).而重写是在子类中,对从父类中继承的方法进行重新编写,但方法名,参数列表(参数顺序,个数,类型),返回值类型必须保持一 ...

  3. 汉字在unicode中的位置

    在www.unicode.org中查找汉字.china找不到,后来查资料才明白,应该查CJK,为什么内? unicode这个组织吧中国日本韩国的字合并了   中日韩统一表意文字(CJK Unified ...

  4. Python 安装包时选择 python版本

    安装了两个版本的python 其中一个版本为2.7 专门为python 2.7安装包使用的语句为 升级pip E:\Python27\python -m pip install --upgrade p ...

  5. 使用不同代理IP刷票的脚本---requests

    投票功能限制刷票是通过限制单个IP的投票次数实现的,所以写了个脚本用于测试此功能. #-*- coding=utf-8 -*- ''' 功能:此脚本用于用不同的IP刷票 作者:Elle 最后修改日期: ...

  6. python学习-34 内置函数的补充

    其他内置函数 1.ord()    与chr()相反 2.pow() print(pow(3,3)) # 相当于3**3 print(pow(3,3,2)) # 相当于3*3%2 运行结果: 27 1 ...

  7. go 构造切片slice

    定义切片 make([]int, 5)  长度和容量均为5 make([]int, 0, 5) 长度为0 容量为0 切片 slice2[3:5] 对slice2进行切片返回 第3 4 两个元素 不包含 ...

  8. IDEA忽略不必要提交的文件

    1.在idea中安装插件用来生成和管理 .gitignore 文件,安装成功后重启idea 2.新建.gitignore 文件 3.将不需要提交的文件添加到.gitignore  4.删除缓冲文件 . ...

  9. office2019激活码 最新各个版本激活码

    office2019专业版激活码 激活秘钥 一.office2019激活6月更新 [Key]:F4QWT-NMMKH-XPTV9-W9HFB-B4JCQ [剩余次数:900000+] office20 ...

  10. 最近公共祖先 LCA (Lowest Common Ancestors)-树上倍增

    树上倍增是求解关于LCA问题的两个在线算法中的一个,在线算法即不需要开始全部读入查询,你给他什么查询,他都能返回它们的LCA. 树上倍增用到一个关键的数组F[i][j],这个表示第i个结点的向上2^j ...