beans.xml的用法
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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--<bean id="car" class="com.TestSpringDemo.Car"-->
<!--p:brand="宝马X5"-->
<!--p:maxSpeed="300"/>-->
<context:annotation-config/>
<context:component-scan base-package="com.TestSpringDemo" /> </beans>
2. 实体类
package com.TestSpringDemo;
public class Car {
private String brand;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getMaxSpeed() {
return MaxSpeed;
}
public void setMaxSpeed(String maxSpeed) {
MaxSpeed = maxSpeed;
}
private String MaxSpeed;
}
3.解析beans.xml
package com.TestSpringDemo;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; public class TestSpring { public static void main(String[] args) {
// ApplicationContext ctx = new AnnotationConfigApplicationContext(Beans.class);
// Car car = ctx.getBean("car", Car.class);
// System.out.println(car.getBrand());
// System.out.println(car.getMaxSpeed());
ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:beans.xml");
Car car = ctx.getBean("car", Car.class);
System.out.println(car.getBrand());
System.out.println(car.getMaxSpeed()); }
} @Service("animalService")
class AnimalService{ public void say(){
System.out.println("AnimalService SAY");
}
} //@Configurable
@Component
class Beans { @Bean(name = "car")
public Car buildCar() {
Car car = new Car();
car.setBrand("英菲迪尼");
car.setMaxSpeed("3000"); return car;
}
}
beans.xml的用法的更多相关文章
- 利用beans.xml进行简单的Spring应用上下文创建与使用
继上次配置Spring完成后,我们来创建一个简单的例程来理解Spring中利用beans.xml创建应用上下文的方法. 程序路径包为:com.spring.kinghts(kinght单词拼写错误,怕 ...
- Spring配置文件的加载,及装载多个beans.xml文件
applicationContext.xml 是spring的全局配置文件,用来控制srping的特性 1 手动加载自定义的beans.xml文件 @Test public void testAut ...
- spring的beans.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- SQL FOR XML PATH 用法
FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...
- spring入门:beans.xml不提示、别名、创建对象的三种方式
spring的版本是2.5 一.beans.xml文件不提示 Location:spring-framework-2.5.6.SEC01\dist\resources\spring-beans-2.5 ...
- spring,hibernate配置事务 beans.xml
beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- Spring配置文件beans.xml头部配置解释
Spring配置文件beans.xml头部配置解释 - EasonJim - 博客园https://www.cnblogs.com/EasonJim/p/6880329.html
- IoC COntainer Create Javabeans 可以通过读取beans.xml 文件来创建一个应用程序上下文对象 依赖反转
Spring初学快速入门 - Spring教程™ https://www.yiibai.com/spring/spring-tutorial-for-beginners.html# pom <? ...
- 自定义beans.xml文件实现Spring框架
经过一天的补习,学习文件加载,java反射,JDom等知识,到了晚上终于能够搭出一个基于配置文件的简单spring框架实现! 首先我们先看看这个问题: 下面是两副图左边是项目结构图,右边是UML图: ...
随机推荐
- 比較不错的一个ios找茬游戏源代码
找茬游戏源代码 .这个是一款很不错的ios找茬游戏源代码,该游戏的兼容性很好的.并且还能够支持ipad和iphone.UI界面设计得也很美丽,游戏源代码真的是一款很完美.并且又很完整的一款休闲类的游戏 ...
- Zookeeper常用命令 (转)
原文链接:ZooKeeper系列之二:Zookeeper常用命令 ZooKeeper服务命令: 在准备好相应的配置之后,可以直接通过zkServer.sh 这个脚本进行服务的相关操作 1. 启动ZK服 ...
- 在Spark程序中使用压缩
当大片连续区域进行数据存储并且存储区域中数据重复性高的状况下,数据适合进行压缩.数组或者对象序列化后的数据块可以考虑压缩.所以序列化后的数据可以压缩,使数据紧缩,减少空间开销. 1. Spark对压缩 ...
- leetcode mock interview-two sum II
package com.company; import java.util.LinkedList; import java.util.List; public class Main { public ...
- vue父组件异步传递prop到子组件echarts画图问题踩坑总结
效果图: 大致思路:考虑到5张图都是折线图,所以准备用一个子组件承接echarts画图,然后父组件通过prop传递不同数据来展示不同的图 踩坑问题: 1.引入line子组件,画了5个元素,但是只显示一 ...
- SQL Server更改排序规则的实现过程
摘自: http://www.2cto.com/database/201112/115138.html 以下的文章主要向大家描述的是SQL Server更改排序规则的实现过程,以及在实现其实际操作过程 ...
- JVM垃圾回收(GC)整理总结学习
基本回收算法 1. 引用计数(Reference Counting)比较古老的回收算法.原理是此对象有一个引用,即增加一个计数,删除一个引用则减少一个计数.垃圾回收时,只用收集计数为0的对象.此算法最 ...
- Android4.2.2 Gallery2源码分析(2)——发现Gallery.java
上文中,main.xml是我直接提出来的,并没有说明是怎么找到它的,现在说明发现它的理由: 一般我们分析界面布局会用到hierarchyviewer这个工具,从工具中,我们对应到视图,最主要的视图id ...
- http响应报文,如果响应的内容比较大,客户端怎么样判断接收完了呢?
1. http协议有正文大小说明的content-length 2. 或者分块传输chunked的话 读到0\r\n\r\n 就是读完了 ---------------------------- ...
- Android Studio中mac上面的安装
Android Studio中mac上面的安装 学习了:https://blog.csdn.net/xianrenli38/article/details/79347170 http://www.an ...