spring 简单IOC测试

来源:互联网 发布:网络报装 编辑:程序博客网 时间:2024/05/21 12:43

1、引用spring的四个jar包:

a、spring-core-4.1.3.RELEASE.jar Spring的核心工具包,其他包依赖此包

b、spring-beans-4.1.3.RELEASE.jar 所有应用都用到,包含访问配置文件,创建和管理bean等,是Spring IOC的基础实现。

c、spring-context-4.1.3.RELEASE.jar 提供在基础IOC功能上的扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的支持

d、spring-expression-4.1.3.RELEASE.jar Spring表达式语言

2、创建beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="person" class="com.spring.po.Person">     <property name="age">      <value>1111</value>     </property>     <property name="name">      <value>czx</value>     </property>    </bean>

</beans>

3、创建一个测试类

public class TestSpringSimpleIOC {/** *  * @param args */public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});Person p = context.getBean("person",Person.class);System.out.println(p.getName());}}


0 0