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. Django的restful api三方包:djangorestframework

    文档位置:https://www.django-rest-framework.org/ 代码位置:https://github.com/encode/django-rest-framework/tre ...

  2. 使用HttpClient发送请求接收响应

    1.一般需要如下几步:(1) 创建HttpClient对象.(2)创建请求方法的实例,并指定请求URL.如果需要发送GET请求,创建HttpGet对象:如果需要发送POST请求,创建HttpPost对 ...

  3. RV BaseRecyclerViewAdapterHelper 总结 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  4. Servlet学习笔记(二):表单数据

    很多情况下,需要传递一些信息,从浏览器到 Web 服务器,最终到后台程序.浏览器使用两种方法可将这些信息传递到 Web 服务器,分别为 GET 方法和 POST 方法. 1.GET 方法:GET 方法 ...

  5. Android怎样设置圆角button

    1. 在res文件夹下的drawable文件夹下新建shape.xml文件 <?xml version="1.0" encoding="utf-8"?&g ...

  6. git/github运用

    了解git和svn很久了,但是一直没有拿来做过版本控制管理,虽然svn有用到过,但是觉得还是运用git的比较多吧,尤其github. Git术语                             ...

  7. DataColumn

    DataColumn 是用于创建 DataTable 的列.下面示例使用不同技巧创建.配置一个列,并把它添加到一个 DataTable 中. using System; using System.Da ...

  8. 截短字符串的函数(JS中适用)

    function cutShort(str){    if(str.length>15){        str=str.substr(0,15)+"...";    }   ...

  9. Android Service+Socket 联网交互

    android中,联网操作有http连接和socket连接两大类.由于项目需要,我们采取的是Socket连接.鉴于平时连接频繁,因此把Socket连接放到Service里,需要从服务器端获取数据时,只 ...

  10. javascript代码在线测试

    目前还不可用,有知道的怎么搞的,请告知我下,谢谢! alert("欢迎使用javascript在线测试工具");