Spring中Bean的装配方式
一、基于xml的装配
Student.java
package com.yh;
public class Student implements People {
public void breath() {
System.out.println("呼吸");
}
}
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <bean id="student" class="com.yh.Student"></bean> </beans>
测试
@Test
public void demo01() {
String xmlPath = "applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
Student stu = (Student) context.getBean("student");
stu.breath();
}
Student,java
package com.yh; import org.springframework.stereotype.Component;
@Component
public class Student implements People { public void breath() {
System.out.println("呼吸");
}
}
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="com.yh"></context:component-scan> </beans>
测试
@Test
public void demo01() {
String xmlPath = "applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
Student stu = (Student) context.getBean("student");
stu.breath();
}
Spring中Bean的装配方式的更多相关文章
- Spring 中Bean的装配方式
最近又买了一本介绍SSM框架的书,是由黑马程序员编写的,书上讲的很好理解,边看边总结一下.主要总结一下bean的装配方式. Bean的装配可以理解为依赖系统注入,Bean的装配方式即Bean依赖注入的 ...
- java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊
Spring 1.1.1.1 创建一个bean package com.zt.spring; public class MyBean { private String userName; privat ...
- Spring中bean的注入方式
首先,要学习Spring中的Bean的注入方式,就要先了解什么是依赖注入.依赖注入是指:让调用类对某一接口的实现类的实现类的依赖关系由第三方注入,以此来消除调用类对某一接口实现类的依赖. Spring ...
- Spring笔记2——Spring中Bean的装配
1.引言 Spring中,对象无需自己负责查找或创建与其关联的其他对象,而是由容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间的协作关系的行为通常称为装配(Wiring),这也是依赖注入 ...
- spring中 Bean的装配 Bean后处理器
- 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)
Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...
- Spring Bean 的装配方式
Spring Bean 的装配方式 装配 Bean 的三种方式 一个程序中,许多功能模块都是由多个为了实现相同业务而相互协作的组件构成的.而代码之间的相互联系又势必会带来耦合.耦合是个具有两面性的概念 ...
- spring中bean实例化时机以及整个运转方式
接上一篇文章,一般在servlet获取到请求之后 在service方法中就可以完成所有的请求处理以及返回,但是我们会采用更高级的MVC框架来做.也就是说所有的MVC框架入口就是serlvet中的ser ...
- Spring中Bean的实例化
Spring中Bean的实例化 在介绍Bean的三种实例化的方式之前,我们首先需要介绍一下什么是Bean,以及Bean的配置方式. 如果 ...
随机推荐
- Linux mem 2.6 Rmap 内存反向映射机制
文章目录 1. 简介 2. 匿名内存 Rmap 的建立 2.1 fork() 2.2 do_page_fault() 3. 文件内存 Rmap 的建立 3.1 fork() 3.2 do_page_f ...
- 『学了就忘』Linux软件包管理 — 42、对RPM软件包的查询操作
目录 1.查询RPM软件包是否安装 2.查询系统中所有已安装的RPM软件包 3.查询RPM软件包的详细信息 4.查询RPM软件包中的文件列表 5.查询系统文件属于哪个RPM包 6.查询RPM软件包所依 ...
- 如何提高C# StringBuilder的性能
本文探讨使用C# StringBuilder 的最佳实践,用于减少内存分配,提高字符串操作的性能. 在 .NET 中,字符串是不可变的类型.每当你在 .NET 中修改一个字符串对象时,就会在内存中创建 ...
- Nginx支持WebSocket反向代理
WebSocket是目前比较成熟的技术了,WebSocket协议为创建客户端和服务器端需要实时双向通讯的webapp提供了一个选择.其为HTML5的一部分,WebSocket相较于原来开发这类app的 ...
- R数据分析:跟随top期刊手把手教你做一个临床预测模型
临床预测模型也是大家比较感兴趣的,今天就带着大家看一篇临床预测模型的文章,并且用一个例子给大家过一遍做法. 这篇文章来自护理领域顶级期刊的文章,文章名在下面 Ballesta-Castillejos ...
- 通过get方法的方式获取配置项信息
这种写法比其他的方法好的一点是,当你需要修改参数名或者参数值的时候,只需要改一个地方就可以了,其他地方根本不用动,面向接口编程. eureka-server.properties archaius.d ...
- nginx安装与配置4-负载均衡
负载均衡: 1.准备俩tomcat,测试环境端口不一样,然后服务名一样然后内容不一样 2.配置nginx的config 在http模块中设置: //举例,以下IP,端口无效 upstream test ...
- ant的xml解释
ant必须以<project>开始和</project>结束 --project(父节点) --target(子节点) ---javac(孙节点) ---echo(孙节点)
- Codeforces 1446D2 - Frequency Problem (Hard Version)(根分)
Codeforces 题面传送门 & 洛谷题面传送门 人菜结论题做不动/kk 首先考虑此题一个非常关键的结论:我们设整个数列的众数为 \(G\),那么在最优子段中,\(G\) 一定是该子段的众 ...
- Codeforces 434E - Furukawa Nagisa's Tree(三元环+点分治)
Codeforces 题面传送门 & 洛谷题面传送门 场号 hopping,刚好是我的学号(指 round 的编号) 注:下文中分别用 \(X,Y,K\) 代替题目中的 \(x,y,k\) 注 ...