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. Java调用使用SSL/HTTPS协议来传输的axis webservice服务

    使用SSL/HTTPS协议来传输 Web服务也可以使用SSL作为传输协议.虽然JAX-RPC并没有强制规定是否使用SSL协议,但在tomcat 下使用HTTPS协议. 1.使用JDK自带的工具创建密匙 ...

  2. 290.单词模式。给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。(c++方法)

    题目描述: 给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式. 这里的遵循指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之 ...

  3. Netty重要概念介绍

    Netty重要概念介绍 Bootstrap Netty应用程序通过设置bootstrap(引导)类开始,该类提供了一个用于网络成配置的容器. 一种是用于客户端的Bootstrap 一种是用于服务端的S ...

  4. android: 在android studio中使用retrolambda的步骤

    找了各种说明,包括retrolambda官方文档都没有试成功 最后在这个链接中找到答案:http://blog.csdn.net/qq_26819733/article/details/5222565 ...

  5. wcstombs_s 宽字节转多字节

    // crt_wcstombs_s.c // This example converts a wide character // string to a multibyte character str ...

  6. 基于redis 实现分布式锁(二)

    https://blog.csdn.net/xiaolyuh123/article/details/78551345 分布式锁的解决方式 基于数据库表做乐观锁,用于分布式锁.(适用于小并发) 使用me ...

  7. password、文件MD5加密,passwordsha256、sha384、sha512Hex等加密

    package encryption; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io. ...

  8. C# System.Collections.Stack

    using System; using System.Collections; public class SamplesStack { public static void Main() { // C ...

  9. Rocket Typist for Mac(增强型文本快速输入工具)破解版安装

    1.软件简介    Rocket Typist 是 macOS 系统上一款增强型文本快速输入工具,我们可以利用这款工具预先设置保存好很多日常生活学习或是工作中常用的文本片段,还能设定部分内容为变量,当 ...

  10. 译: 5. RabbitMQ Spring AMQP 之 Topic 主题

    在上一个教程中,我们提高了消息传递的灵活 我们使用direct交换而不是使用仅能够进行虚拟广播的fanout交换, 并且获得了基于路由key 有选择地接收消息的可能性. 虽然使用direct 交换改进 ...