使用Maven配置spring

1、 创建maven项目

NewàProjectàMaven Projectà不勾选 Create a simple project

à选择maven-archetype-quickstart

à填写Group ID,artifact id

至此。Maven项目创建完成,目录结果如下。

2、 加入spring 依赖

打开pom.xml文件,最后一个标签 

<dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-context</artifactId>

 <version>3.1.1.RELEASE</version>

</dependency>  

效果:

找到maven setting.xmlmaven  .m2目录下),

加入库

<repository>

    <id>springsource-repo</id>

    <name>SpringSource Repository</name>

    <url>http://repo.springsource.org/release</url>

</repository>

具体配置可以参考http://www.springsource.org/spring-framework

说明:关于为什么要配置spring-context 网站上没有给出明确说明,只有下面一段看不懂的话。我的理解是只要用基础的spring引这个就行了。

spring-context.jar 
  这个jar文件为Spring核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类,UI方面的用来与模板(Templating)引擎如 VelocityFreeMarkerJasperReports集成的类,以及校验Validation方面的相关类。

3、 spring配置文件和类

新建sourse folder 名字 src/main/resources

新建xml文件

目录结构:

编辑applicationContext.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:context="http://www.springframework.org/schema/context"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

</beans>

************以上代码可以在spring的例子中找到***********


加入Person

package com.syz.test01;

public class Person {

public String name;

public String age;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAge() {

return age;

}

public void setAge(String age) {

this.age = age;

}

}

编写app

package com.syz.test01;

/**

 * Hello world!

 *

 */

public class App 

{

public Person person;

    public Person getPerson() {

return person;

}

public void setPerson(Person person) {

this.person = person;

}

public static void main( String[] args )

    {

          System.out.println( "Hello World!" );

    }

}

配置好的applicationContext.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:context="http://www.springframework.org/schema/context"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

<bean id="person" class="com.syz.test01.Person">

       <property name="name" value="zhangsan"></property>

       <property name="age" value="12"></property>

</bean>

<bean id="app" class="com.syz.test01.App">

   <property name="person" ref="person"></property>

</bean>

</beans>

4、 编写测试代码

AppTest.java 代码

package com.erik.spring.test;

import junit.framework.Test;

import junit.framework.TestCase;

import junit.framework.TestSuite;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

 * Unit test for simple App.

 */

public class AppTest 

    extends TestCase

{

    /**

     * Create the test case

     * @param testName name of the test case

     */

    public AppTest( String testName )

    {

        super( testName );

    }

    /**

     * @return the suite of tests being tested

     */

    public static Test suite()

    {

        return new TestSuite( AppTest.class );

    }

    /**

     * Rigourous Test :-)

     */

    public void testApp()

    {

     BeanFactory bf = new ClassPathXmlApplicationContext(

"applicationContext.xml");

     App at = (App) bf.getBean("app");

     System.out.println(at.getPerson().getName());

     System.out.println(at.getPerson().getAge());

        assertTrue( true );

    }

}

junit运行该测试类

具体使用参考下一篇:

使用Maven使用spring(注解版)



  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值