软件工程搭建Spring 的入门程序
Posted yyyyfly1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件工程搭建Spring 的入门程序相关的知识,希望对你有一定的参考价值。
搭建Spring 的入门程序
摘抄内容
运行结果
相关代码
pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.ssm</groupId> 5 <artifactId>test</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>test Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <dependencies> 11 <dependency> 12 <groupId>junit</groupId> 13 <artifactId>junit</artifactId> 14 <version>3.8.1</version> 15 <scope>test</scope> 16 </dependency> 17 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> 18 <dependency> 19 <groupId>org.springframework</groupId> 20 <artifactId>spring-core</artifactId> 21 <version>5.0.4.RELEASE</version> 22 </dependency> 23 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-context</artifactId> 27 <version>5.0.4.RELEASE</version> 28 </dependency> 29 <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --> 30 <dependency> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring-expression</artifactId> 33 <version>5.0.4.RELEASE</version> 34 </dependency> 35 <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> 36 <dependency> 37 <groupId>org.springframework</groupId> 38 <artifactId>spring-beans</artifactId> 39 <version>5.0.4.RELEASE</version> 40 </dependency> 41 42 </dependencies> 43 <build> 44 <finalName>test</finalName> 45 </build> 46 </project>
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 <bean id="helloSpring" class="com.ssm.HelloSpring"> 7 <property name="userName" value="张三"> 8 9 </property> 10 </bean> 11 12 </beans>
HelloSpring.java
1 package com.ssm; 2 3 public class HelloSpring { 4 private String userName; 5 public void setUserName(String userName) { 6 this.userName=userName; 7 } 8 public void show() { 9 System.out.println(userName + ":欢迎您来学习Spring 框架"); 10 } 11 }
TestHelloSpring.java
1 package com.ssm; 2 import org.springframework.context.ApplicationContext; 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 public class TestHelloSpring { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); 9 HelloSpring helloSpring =(HelloSpring)ctx.getBean("helloSpring"); 10 helloSpring.show(); 11 } 12 13 }
以上是关于软件工程搭建Spring 的入门程序的主要内容,如果未能解决你的问题,请参考以下文章
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段
阶段3 2.Spring_03.Spring的 IOC 和 DI_3 spring基于XML的IOC环境搭建和入门
springboot 入门篇第1篇 第一个spring-boot程序(多种搭建方式)