(一)spring的bean简单的配置
来源:互联网 发布:php项目开发实战入门 编辑:程序博客网 时间:2024/05/23 01:59
第一步:建立一个类
package com.atguigu.spring.beans;public class HelloWorld { private String name; public void setName(String name){ this.name=name; } public void hello(){ System.out.println("hello:"+name); }}
第二部配置资源文件
<?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:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!--配置bean class:bean的全类名通过反射的方式在IOC容器中创建Bean所以要求Bean中必须有无参的构造器--> <bean id="h" class="com.atguigu.spring.beans.HelloWorld"> <property name="name" value="Spring----》fangxinde,you are successful"></property> </bean></beans>
第三步:main方法中获取bean
package com.atguigu.spring.beans;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) { //ClassPathXmlApplicationContext接口的实现类,该实现类从类路径下来加载配置文件 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); //利用类型返回IOC容器中的Bean,但要求IOC容器中必须智能一个该类型的Bean HelloWorld helloworld=ctx.getBean(HelloWorld.class); //HelloWorld helloworld=(HelloWorld) ctx.getBean("helloworld1"); helloworld.hello(); }}
第四步:运行main方法,得到打印结果
hello:Spring----》fangxinde,you are successful
阅读全文
0 0
- (一)spring的bean简单的配置
- spring bean的配置(一)
- spring的配置一(bean的配置)
- spring配置bean的细节一
- spring bean的配置
- Spring bean的配置
- spring-bean的配置
- spring 的 bean 配置
- 【Spring】Bean的配置
- Spring-简单bean的配置(使用bean.xml手动完成)
- spring(一)---bean的学习上篇
- Spring中Bean的配置
- Spring配置Bean的问题
- Spring的bean属性配置
- Spring的Bean配置说明
- Spring bean.xml的配置
- spring中bean的配置
- Spring继承Bean的配置
- 匈牙利算法
- graph
- jQuery的简单特效
- 秘钥认证、远程登录
- 互联网高可用架构技术实践二
- (一)spring的bean简单的配置
- hive-3
- Keil中建立新工程
- java-动态代理
- 在命令行中使用vs编译器
- Java:输出乘法口诀(上三角)
- Cable master(二分查找)
- 轻量级插件sdstorage用于操作localStorage支持过期、批量搜索删除等
- 动态背景页面登录--(软件实训)