Spring配置文件

1.alias:设置别名,为bean设置别名,并且可以设置多个别名;

<!-- 设置别名 -->
<alias name="user" alias="user1"/>

2.bean的配置;

    <!--id是bean的标识符,要唯一,如果没有配置id,name默认为标识符
如果配置了id,又配置了name,那么name就是别名
name可以设置多个别名并且别名可以是空格 逗号 分号
class是bean的全限定名=包名+类名
如果不配置id和name,则可以根据applicationContext.getBean(Class)获取对象
-->
<bean id="h1" name="hello h2,h3;h4" class="cn.sxt.bean.Hello">
<property name="name" value="张三"></property>
</bean>

3.团队协作开发,通过import来实现;

在我们的beans.xml中就可以引入和使用entity.xml文件中的bean配置(代码中config/spring为我们新建的包config.spring)

<import resource="config/spring/entity.xml"/>

而entity.xml中进行bean的详细配置:

    <bean id="h1" name="hello h2,h3;h4" class="cn.sxt.bean.Hello">
<property name="name" value="张三"></property>
</bean>

Bean的作用域

scope指bean的作用域,在配置bean时,有scope属性来配置bean的作用

注意:在整合struts和spring时,需要将action设为scope="prototype";

 <bean id="addr" class="cn.sxt.vo.Address" scope="singleton">
<!-- scope:bean的作用域 (默认是singleton):
singleton:单例(用计数器记录),整个容器中只有一个对象的实例;
prototype原型:每次获取bean都产生一个新的对象;
request:每次请求时,创建一个新的对象;
session:在回话的范围内是一个对象(servlet的session);
global session: 只在portlet下有用,表示是application
application:在应用范围内,只有一个对象;
-->

Bean的自动装配(spring4)

是用来简化spring的配置文件,在配置bean时,可以配置bean的autowire属性,用于指定装配类型

<bean id="userDao" class="cn.sxt.dao.impl.UserDaoMySqlImpl"></bean>
<!-- autowire:自动装配,用于简化spring的配置
no 不使用自动装配
byname 根据名称(set方法名)来查找相应的bean,如果有则装配上去
使用形式:xml头文件最后面加上default-autowire="byName"
byType 根据类型进行装配,不同去管bean的id(bean的id可以写任意)
但是同一种类型的bean只能有一个(尽量慎用byType)
constructor 当使用构造器实例化bean时,适用byType的方式装配构造方法
-->
<bean id="service" class="cn.sxt.service.impl.UserServiceImpl" autowire="constructor"></bean>

可以配置全局的自动装配类型,在xml文件的头部:default-autowire="byname"

【注意】推荐不使用自动装配,而使用annotation

依赖注入 DI
1.依赖注入--dependency injection;

 依赖:指bean对象创建依赖于容器,Bean对象的依赖资源

 注入:指bean对象依赖的资源由容器来设置和装配

2.spring注入--构造器注入

见IOC创建对象,也就是上一篇笔记中IOC构造方法创建对象的内容(有参和无参)

3.spring注入--setter注入(重点)

要求被注入的属性必须有set方法。set方法的方法名由set+属性(属性首字母大写),如果属性是boolean类型,没有get方法(是is);

 3.1 常量注入

 Student.java:

package cn.sxt.vo;

public class Student {
private String name;
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}

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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="cn.sxt.vo.Student">
<property name="name" value="张三丰"></property>
</bean>
</beans>

Test.java:

package cn.sxt.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.sxt.vo.Student; public class Test { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
Student stu=(Student)ac.getBean("student");
stu.show();
} }

 3.2 Bean注入

新建一个类Address:

package cn.sxt.vo;

public class Address {
private String address; public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} }

Student类中引用Address类的对象(Student的实例变量是另外一个类Address的对象):

private Address addr;
public void setAddr(Address addr) {
this.addr = addr;
}

beans.xml:

 <bean id="addr" class="cn.sxt.vo.Address">
<property name="address" value="北京优衣库"/>
</bean>
<bean id="student" class="cn.sxt.vo.Student">
<property name="name" value="张三丰"></property>
<property name="addr" ref="addr"></property>
</bean>

 3.3 数组注入
Student类继续添加一个实例变量books以及books的set方法:

private String[] books;
public void setBooks(String[] books) {
this.books = books;
}

beans.xml中做以下修改:

    <bean id="student" class="cn.sxt.vo.Student">
<property name="name" value="张三丰"></property>
<property name="addr" ref="addr"></property>
<property name="books">
<array>
<value>傲慢与偏见</value>
<value>仲夏夜之梦</value>
<value>雾都孤儿</value>
</array>
</property>
</bean>

 3.4 List注入

Student类继续添加实例变量及其set方法:

private List<String> hobbies;
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}

beans.xml做以下配置:

<bean id="student" class="cn.sxt.vo.Student">
<property name="name" value="张三丰"></property>
<property name="addr" ref="addr"></property>
<property name="books">
<array>
<value>傲慢与偏见</value>
<value>仲夏夜之梦</value>
<value>雾都孤儿</value>
</array>
</property>
<property name="hobbies">
<list>
<value>羽毛球</value>
<value>乒乓球</value>
<value>玻璃球</value>
<value>排球</value>
</list>
</property>
</bean>

    3.5 Map注入

Student继续添加实例变量Map<String,String> cards;

private Map<String, String> cards;
public void setCards(Map<String, String> cards) {
this.cards = cards;
}

beans.xml做以下配置:

    <bean id="student" class="cn.sxt.vo.Student">
<property name="name" value="张三丰"></property>
<property name="addr" ref="addr"></property>
<property name="books">
<array>
<value>傲慢与偏见</value>
<value>仲夏夜之梦</value>
<value>雾都孤儿</value>
</array>
</property>
<property name="hobbies">
<list>
<value>羽毛球</value>
<value>乒乓球</value>
<value>玻璃球</value>
<value>排球</value>
</list>
</property>
<property name="cards">
<map>
<entry key="中国银行" value="1545615345415"></entry>
<entry>
<key><value>农业银行</value></key>
<value>54654861231543</value>
</entry>
</map>
</property> </bean>

   3.6 Set注入

Student类添加实例变量Set<String>games及其set方法;

private Set<String> games;
public void setGames(Set<String> games) {
this.games = games;
}

beans.xml做以下配置(都是些重复的步骤):

    <bean id="student" class="cn.sxt.vo.Student">
<property name="name" value="张三丰"></property>
<property name="addr" ref="addr"></property>
<property name="books">
<array>
<value>傲慢与偏见</value>
<value>仲夏夜之梦</value>
<value>雾都孤儿</value>
</array>
</property>
<property name="hobbies">
<list>
<value>羽毛球</value>
<value>乒乓球</value>
<value>玻璃球</value>
<value>排球</value>
</list>
</property>
<property name="cards">
<map>
<entry key="中国银行" value="1545615345415"></entry>
<entry>
<key><value>农业银行</value></key>
<value>54654861231543</value>
</entry>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>dota</value>
<value>cs</value>
<value>dnf</value>
<value>cf</value> </set>
</property> </bean>

  3.7 Null注入 
Student类添加实例变量String wife及其set方法;

private String wife;
public void setWife(String wife) {
this.wife = wife;
}

beans.xml做以下修改:

<bean id="student" class="cn.sxt.vo.Student">
<property name="name" value="张三丰"></property>
<property name="addr" ref="addr"></property>
<property name="books">
<array>
<value>傲慢与偏见</value>
<value>仲夏夜之梦</value>
<value>雾都孤儿</value>
</array>
</property>
<property name="hobbies">
<list>
<value>羽毛球</value>
<value>乒乓球</value>
<value>玻璃球</value>
<value>排球</value>
</list>
</property>
<property name="cards">
<map>
<entry key="中国银行" value="1545615345415"></entry>
<entry>
<key><value>农业银行</value></key>
<value>54654861231543</value>
</entry>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>dota</value>
<value>cs</value>
<value>dnf</value>
<value>cf</value> </set>
</property>
<property name="wife"><null/></property>
</bean>

   3.8 Properties 注入

Student继续添加实例变量properties info;

private Properties info;
public void setInfo(Properties info) {
this.info = info;
}

beans.xml做以下修改:

    <bean id="student" class="cn.sxt.vo.Student">
<property name="name" value="张三丰"></property>
<property name="addr" ref="addr"></property>
<property name="books">
<array>
<value>傲慢与偏见</value>
<value>仲夏夜之梦</value>
<value>雾都孤儿</value>
</array>
</property>
<property name="hobbies">
<list>
<value>羽毛球</value>
<value>乒乓球</value>
<value>玻璃球</value>
<value>排球</value>
</list>
</property>
<property name="cards">
<map>
<entry key="中国银行" value="1545615345415"></entry>
<entry>
<key><value>农业银行</value></key>
<value>54654861231543</value>
</entry>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>dota</value>
<value>cs</value>
<value>dnf</value>
<value>cf</value> </set>
</property>
<property name="wife"><null/></property>
<property name="info">
<props>
<prop key="学号">2015534001</prop>
<prop key="sex">男</prop>
<prop key="name">张三</prop>
</props>
</property>
</bean>

 3.9 p命名空间注入

beans.xml头文件添加以下内容:

我们新建了一个User类:包含name和age属性

beans.xml配置形式如下所示:

 <bean id="user" class="cn.sxt.vo.User" p:name="风清扬" p:age="230"></bean>

   3.10 c注命名空间入(构造函数注入)
beans.xml头文件添加以下内容:

c命名空间注入要求有对应参数的构造方法:

public User(String name, int age) {
super();
this.name = name;
this.age = age;
}

beans.xml做以下配置:

    <bean id="u1" class="cn.sxt.vo.User" c:name="coco" c:age="16"></bean>

总结我们之前学习的内容:

spring--桥梁

spring--轻量级,易学,ioc,aop,事务,整合框架等

spring--ioc:控制反转(权限的反转)

spring--Di(和ioc一个概念,只不过是站在不同的角度)

Spring学习笔记--Spring配置文件和依赖注入的更多相关文章

  1. Spring学习笔记(8)——依赖注入

    spring依赖注入使用构造器注入使用属性setter方法注入使用Field注入(用于注解方式) 注入依赖对象可以采用手工装配或自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发 ...

  2. go微服务框架kratos学习笔记八 (kratos的依赖注入)

    目录 go微服务框架kratos学习笔记八(kratos的依赖注入) 什么是依赖注入 google wire kratos中的wire Providers injector(注入器) Binding ...

  3. Spring学习笔记--spring+mybatis集成

    前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...

  4. Angular4学习笔记(四)- 依赖注入

    概念 依赖注入是一种设计思想,并不是某一类语言所特有的,因此可以参考开涛大神关于学习Java语言的Spring框架时对其的解释: DI-Dependency Injection,即"依赖注入 ...

  5. Spring学习笔记——Spring依赖注入原理分析

    我们知道Spring的依赖注入有四种方式,各自是get/set方法注入.构造器注入.静态工厂方法注入.实例工厂方法注入 以下我们先分析下这几种注入方式 1.get/set方法注入 public cla ...

  6. Spring学习-理解IOC和依赖注入

    最近刚买了一本介绍ssm框架的书,里面主要对Mybatis.spring.springmvc和redis做了很多的讲解,个人觉得虽然有的内容我看不懂,但是整体上还是不错的.最近正在学习中,一边学习一边 ...

  7. Spring学习:简单实现一个依赖注入和循环依赖的解决

    依赖注入 什么是依赖注入 使用一个会创建和查找依赖对象的容器,让它负责供给对象. 当a对象需要b对象时,不再是使用new创建,而是从容器中获取,对象与对象之间是松散耦合的关系,有利于功能复用. 依赖: ...

  8. Spring学习笔记--Spring IOC

    沿着我们上一篇的学习笔记,我们继续通过代码学习IOC这一设计思想. 6.Hello类 第一步:首先创建一个类Hello package cn.sxt.bean; public class Hello ...

  9. Spring学习笔记—Spring之旅

    1.Spring简介     Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...

随机推荐

  1. YUV 4:2:0 格式和YUV411格式区别

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/coloriy/article/details/6668447 MPEG 储存的 YU(Cb)V(Cr ...

  2. LINK:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 (转)

    很多伙伴在更新VS2010,或者卸载VS2012安装2010后,建立Win32 Console Project/MFC项目时会出现"LINK : fatal error LNK1123: 转 ...

  3. GraphQL入门1

    1. 资源: 主站: https://graphql.org/ 中文站: http://graphql.cn 入门视频: https://graphql.org/blog/rest-api-graph ...

  4. UE.getEditor('editor')

    <script type="text/javascript"> $().ready(function(){ var editor = document.getEleme ...

  5. Dubbo的使用入门

    一.包引入 1.父模块pom.xml中加入依赖: <!-- dubbo --> <dependency> <groupId>com.alibaba.boot< ...

  6. (亲测解决)每次打开excel文件都会出现两个窗口,一个是空白的sheet1,另一个是自己的文档

    版权声明:本文为博主原创文章,转载请注明出处! https://blog.csdn.net/sinat_34104446/article/details/80210424 一.问题描述每次打开Exce ...

  7. Eclipse 插件开发 -- 深入理解菜单(Menu)功能及其扩展点( FROM IBM)

    Eclipse 插件开发 -- 深入理解菜单(Menu)功能及其扩展点 菜单是各种软件及开发平台会提供的必备功能,Eclipse 也不例外,提供了丰富的菜单,包括主菜单(Main Menu),视图 / ...

  8. Tuxedo低版本客户端(Tuxedo 9)连接到高版本Tuxedo服务端(Tuxedo 12.1.3)的问题

    经过我实测,是没问题的.但是客户端的Tuxedo DLL必须全部是Tuxedo 9的DLL,不能混用.不然即使用Dependency Walker 分析DLL依赖完全正确,但是实际运行时结果也会出现奇 ...

  9. Mac MySQL 数据库配置(关系型数据库管理系统)

    前言 MySQL 关系型数据库管理系统. 1.配置准备工作 1)配置数据库准备工作 下载相关软件 mysql-5.7.21-1-macos10.13-x86_64.dmg mysql-workbenc ...

  10. CentOS 7 安装配置OpenVPN 2.3.12

    1.下载安装包 #wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.09.tar.gz#wget http://swupdate. ...