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的用法的更多相关文章

  1. 利用beans.xml进行简单的Spring应用上下文创建与使用

    继上次配置Spring完成后,我们来创建一个简单的例程来理解Spring中利用beans.xml创建应用上下文的方法. 程序路径包为:com.spring.kinghts(kinght单词拼写错误,怕 ...

  2. Spring配置文件的加载,及装载多个beans.xml文件

    applicationContext.xml 是spring的全局配置文件,用来控制srping的特性 1  手动加载自定义的beans.xml文件 @Test public void testAut ...

  3. spring的beans.xml的配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. SQL FOR XML PATH 用法

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  5. spring入门:beans.xml不提示、别名、创建对象的三种方式

    spring的版本是2.5 一.beans.xml文件不提示 Location:spring-framework-2.5.6.SEC01\dist\resources\spring-beans-2.5 ...

  6. spring,hibernate配置事务 beans.xml

    beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  7. Spring配置文件beans.xml头部配置解释

    Spring配置文件beans.xml头部配置解释 - EasonJim - 博客园https://www.cnblogs.com/EasonJim/p/6880329.html

  8. IoC COntainer Create Javabeans 可以通过读取beans.xml 文件来创建一个应用程序上下文对象 依赖反转

    Spring初学快速入门 - Spring教程™ https://www.yiibai.com/spring/spring-tutorial-for-beginners.html# pom <? ...

  9. 自定义beans.xml文件实现Spring框架

    经过一天的补习,学习文件加载,java反射,JDom等知识,到了晚上终于能够搭出一个基于配置文件的简单spring框架实现! 首先我们先看看这个问题: 下面是两副图左边是项目结构图,右边是UML图: ...

随机推荐

  1. datagridview 单元格类型转换【备忘】

    datagridview  在设定列类型后,其下面所有行的该列都与设定的列类型相同. 在需要改变某一行的某个单元格时,遇到了一些问题,再次进行备忘: 之前在遇到该问题时参考别人的博客解决过,但是时间久 ...

  2. QT creator出现 no executable specified时解决办法

    1.     本人是在linux/ubutun中使用QT creator,一次编译程序的时候出现了“no executable specified”的错误而无法运行程序.解决办法: 在用QT crea ...

  3. 如何高效的将excel导入sqlserver?

    大部分人都知道用oledb来读取数据到dataset,但是读取之后怎么处理dataset就千奇百怪了.很多人通过循环来拼接sql,这样做不但容易出错而且效率低下,System.Data.SqlClie ...

  4. leetCode 86.Partition List(分区链表) 解题思路和方法

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  5. LoadRunner录制:脚本调试

    优化思路 1. 先录制 2. 然后进行清理.清理掉多余的cookie.lr_think_time等冗余脚本. 3. 调试脚本,让脚本可以跑通过. 4. 逐步优化添加 调试思路 1. 单用户单迭代 录制 ...

  6. volist 自增序号 分页如何实现?

    TP框架模板中如何生成自增数据 {$_GET['p']*10-10+$i} /* 分页序号计算    */ function addnum($k,$num){     return ($k +1 ) ...

  7. Ajv JSON数据格式校验 使用

    1.官网 https://ajv.js.org/ 2.示例代码 <!DOCTYPE html> <html lang="zh"> <head> ...

  8. sql server 操作xml例子

    sql server 操作xml例子 /* sql xml 入门: --by jinjazz --http://blog.csdn.net/jinjazz 1.xml: 能认识元素.属性和值 2.xp ...

  9. 【Excle数据透视】多列分别分类计数

    需求 今天碰到一个很特殊的需求,如下(分别对每一列的值去重并统计个数): 预期结果 实现方法 推荐使用第三种方案,因为不用写公式,比较简单! 方法一:使用countif函数 在单元格J2输入公式COU ...

  10. 公用的css

    /*公共开始*/ body, ol, ul, h1, h2, h3, h4, h5, h6, p, th, td, dl, dd, form, fieldset, legend, input, tex ...