spring-framework-3.2.4.RELEASEspring
jdk1.7.0_11apache
Maven3.0.5eclipse
eclipse-jee-juno-SR2-win32maven
mvn archetype:generate -DgroupId= com.lei.demo -DartifactId=Spring3-Example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
而後轉換成Eclipse項目:mvn eclipse:eclipse測試
或者直接在Eclipse中建立maven-archetype-quickstart項目。ui
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lei.demo</groupId> <artifactId>spring3-Example</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring3-Example</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Spring3配置 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.4.RELEASE</version> </dependency> </dependencies> </project>
一個簡單的spring bean以下this
package com.lei.demo.helloworld; public class HelloWorld { private String name; public void setName(String name) { this.name = name; } public void printHello() { System.out.println("第一個Spring 3 : Hello ! " + name); } }
建立文件SpringBeans.xml,配置bean以下。文件位於src/main/resources下url
<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-3.0.xsd"> <bean id="helloBean" class="com.lei.demo.helloworld.HelloWorld"> <property name="name" value="leiOOlei" /> </bean> </beans>
package com.lei.demo.helloworld; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { private static ApplicationContext context; public static void main( String[] args ) { context = new ClassPathXmlApplicationContext("SpringBeans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloBean"); obj.printHello(); } }