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. 你的项目真的需要Session吗? redis保存session性能怎么样?

    在web开发中,Session这个东西一直都很重要,至少伴随我10年之久, 前一段时间发生一个性能问题,因为Redis session 问题,后来想想 其实我的项目session 是不需要的. 先看看 ...

  2. centos7下安装gcc7

    之前写过在linux下升级gcc 4.8至gcc 4.9的过程,现在gcc最新的版本是8,有些软件必须是gcc 7或者以上的版本才可以编译,比如clickhouse,gcc 7的安装过程和之前基本上一 ...

  3. error C2039: 'SetWindowTextA' : is not a member of 'CString'

    m_OpenPath.SetWindowText(strPath);   错误原因:在给控件关联变量m_OpenPath时,变量类型选择错误   解决办法:

  4. redis 基本信息查询

    在客户端可以用telnet命令 telnet ip port 再输入info 返回如下信息:

  5. Spark LDA实战

    选取了10个文档,其中4个来自于一篇论文,3篇来自于一篇新闻,3篇来自于另一篇新闻. 首先在pom文件中加入mysql-connector-java: <dependency> <g ...

  6. 基于Centos7.5搭建Docker环境

    docker很火,基于容器化技术,实现一次编译到运行.实现运行环境+服务的一键式打包! 00.部署环境 centos7.5(基于vmware搭建的测试环境,可以跟互联网交互,桥接方式联网) docke ...

  7. JAVA 自定义注解在自动化测试中的使用

    在UI自动化测试中,相信很多人都喜欢用所谓的PO模式,其中的P,也就是page的意思,于是乎,在脚本里,或者在其它的page里,会要new很多的page对象,这样很麻烦,前面我们也讲到了注解的使用,很 ...

  8. GuavaCache学习笔记三:底层源码阅读

    申明:转载自 https://www.cnblogs.com/dennyzhangdd/p/8981982.html 感谢原博主的分享,看到这个写的真好,直接转载来,学习了. 另外也推荐另外一篇Gua ...

  9. 基于mindwave脑电波进行疲劳检测算法的设计(4)

    上一次的实验做到可以从pc端读取到MindWave传输过来的脑电波原始数据了. 我是先定义一个结构体,该结构体对应保存所有能从硬件中取到的原始数据. struct FD_DATA { int batt ...

  10. RelativeLayout 布局参数

    今天调布局的时候 想把界面做成横屏竖屏都可以的 突然发现之前理解的android:布局参数都是有问题的 今天贴出来 下次自己也记得 以下大部为用在RelativeLayout中的一些参数: andro ...