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. 解决Spring MVC报No converter found for return value of type:class java.util.ArrayList问题

    一.背景 在搭建一套Spring+SpringMVC+Mybatis(SSM)的环境(搭建步骤会在以后博客中给出),结果运行 程序时,适用@ResponseBody注解进行返回List<对象&g ...

  2. iOS: 详细的正则表达式

    一.简单的正则规则 1.由数字.26个英文字母或者下划线组成的字符串: ^[-9a-zA-Z_]{,}$ 2.非负整数(正整数 + 0 ): ^/d+$ 3. 正整数: ^[-]*[-][-]*$ 4 ...

  3. ASP.Net生成静态HTML页

    动态网页开发技术中,为了降低网站维护的工作量,常常用到动态页面技术.目前因特网上流行的做法是将网站中需要经常更新的数据存放到数据库中,当客户端浏览器向服务器发出HTTP请求时,服务器通过执行.解释某个 ...

  4. boost.asio学习笔记一、linux下boost库的安装

    欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/46761029 学习开源库第一步就是编译安装好库,然后执行成功一个 ...

  5. 移动端的emoji表情符号插入MySQL数据库失败

    插入数据时候报了错:### Error updating database. Cause: Java.sql.SQLException: Incorrect string value: ‘\xF0\x ...

  6. 关于DrawIndexedPrimitive函数的调用

    函数的原型例如以下所看到的: HRESULT DrawIndexedPrimitive( [in] D3DPRIMITIVETYPE Type, [in] INT BaseVertexIndex, [ ...

  7. Antlr与Regex

    Antlr与Regex都是文本分析工具. Antlr内部分为词法(Lexer)和语法(Parser),在Antlr中,变量第一个字符大写表示词法,变量第一个字符小写表示语法.词法表示哪些是有效的词,语 ...

  8. OpenMeetings(4)----新用户注册

    用户登录与注册的主要代码都在WebContent\src\base\auth\checkLoginData.lzx文件中     <simpleLabelButton labelid=" ...

  9. CLightLock:一个简单AutoLock

    原理: 标准的RAII, 利用构造函数进行加锁,利用析构函数进行解锁. #ifndef _C_LIGTHT_LOCK_HPP #define _C_LIGTHT_LOCK_HPP class CLig ...

  10. 重新安装Drupal?

    因个人需要需要重新安装Drupal.如何操纵呢?Drupal是在_drupal_bootstrap_database()函数里面检查是否已经安装过的.检查的依据是有没有$GLOBALS['databa ...