Spring XML配置示例

时间:2020-02-23 14:36:02  来源:igfitidea点击:

在本教程中,我们将看到如何创建Spring Hello World XML基于XML的配置示例。

以下是创建Spring XML配置示例的简单步骤。

1.创建一个简单的Java Maven项目。

2. Maven依赖

将Spring和Cglib Maven依赖于Pom.xml。

<dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${spring.version}</version>
  </dependency>
<!-- cglib required for @configuration annotation -->
   <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.0</version>
        </dependency>
 </dependencies>
 <properties>
  <spring.version>4.2.1.RELEASE</spring.version>
 </properties>

所以你的pom.xml看起来像:

<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>org.igi.theitroad</groupId>
  <artifactId>SpringHelloWorldJavaBasedConfiguration</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringHelloWorldJavaBasedConfiguration</name>
  <description>It provides hello world program for java based config
</description>
 
 <dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${spring.version}</version>
  </dependency>
<!-- cglib required for @configuration annotation -->
   <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.0</version>
    </dependency>
 </dependencies>
 <properties>
  <spring.version>4.2.1.RELEASE</spring.version>
 </properties>
</project>

3.创建bean类

在package org.igi.theitroad.model中创建名为country.java的bean类.model。

package org.igi.theitroad.model;
 
public class Country {
	private String name;
	private long population;
	
	public Country(String name, long population) {
		super();
		this.name = name;
		this.population = population;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getPopulation() {
		return population;
	}
	public void setPopulation(long population) {
		this.population = population;
	}
	
	
}

4. applicationscontext.xml.

在SRC/Main/Resources中创建ApplicationContext.xml,如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<?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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    ">
 
	<bean id="country" class="org.igi.theitroad.model.Country">
		<constructor-arg index="0" value="Netherlands"></constructor-arg>
		<constructor-arg index="1" value="20000"></constructor-arg>
	</bean>
	<context:annotation-config 
</beans>

请注意,我们正在使用构造函数注入来在此注入依赖项。

5.创建SpringXmlConfigurationMain.java.

在org.igi.theitroad包中创建名为"springxmlconfigurationmain.java"的类。

package org.igi.theitroad;
 
import org.igi.theitroad.model.Country;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
 * Spring XML based configuration example
 *
 */
public class SpringXMLConfigurationMain 
{
	public static void main( String[] args )
	{
		ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		Country countryBean = (Country) ac.getBean("country");
		String name = countryBean.getName();
		System.out.println("Country name: "+name);
		long population = countryBean.getPopulation();
		System.out.println("Country population: "+population);
		ac.close();	
	}
}

6.运行

运行上面的程序时,我们将得到以下输出。

Jun 15, 2016 11:21:58 AM Jun 15, 2016 9:04:19 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@210366b4: startup date [Fri Jun 15 21:04:19 IST 2016]; root of context hierarchy
Jun 15, 2016 9:04:20 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [ApplicationContext.xml]

Country name: Netherlands
Country population: 20000

Jun 15, 2016 9:04:20 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@210366b4: startup date [Fri Jun 15 21:04:19 IST 2016]; root of context hierarchy