1.使用archetype建立app框架
2. 在pom.xml中添加build plugin (exec-maven-plugin)
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>com.example.app.App</mainClass> </configuration> </plugin> </plugins> </build>
3. 执行application
4. 添加springframework的dependency
<dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency>
5. 建立一个java class
package com.example.app; public class SayHello { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void greet(){ System.out.println("Hello,"+name); } }
6. 在src/main下面增加resources.然后建立application-context.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="hello" class="com.example.app.SayHello"> <property name="name" value="Pookey" /> </bean> </beans>
7. 修改App.java,使用hello bean
package com.example.app; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; /** * Hello world! * */ public class App { public static void main( String[] args ) { BeanFactory factory = new XmlBeanFactory(new ClassPathResource("application-context.xml")); SayHello hello = (SayHello)factory.getBean("hello"); hello.greet(); System.out.println( "Hello World!" ); } }
参考:http://pookey.co.uk/wordpress/archives/50-getting-started-with-maven-and-spring