springframework【1】
第一个spring程序
?
在这里我们使用xmlschema来进行spring的配置文件的描述规则:
package com.itcast.first;//第一个使用spring管理的bean,展示依赖注入public class Hello {private String helloworld;public String getHelloworld() {return helloworld;}public void setHelloworld(String helloworld) {this.helloworld = helloworld;}}
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd "> <!-- 我的第一个依赖注入的bean --> <bean name="hello" name="code">package com.itcast.first;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;//测试程序,测试简单依赖注入public class TestHello {private static final Log logger = LogFactory.getLog(TestHello.class);public static void main(String[] args) {Resource rs = (Resource) new ClassPathResource("bean-config.xml"); BeanFactory factory = new XmlBeanFactory(rs);Hello h = (Hello) factory.getBean("hello"); logger.info(h.getHelloworld());}}