前提
安装jdk
安装maven
安装eclipse
创建maven项目
搭建spring项目
pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
新建hellospring.java
package com.wujialiang.springstu01;
public class HelloSpring {
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void show() {
System.out.println(userName+" 你好");
}
}
修改App.java
package com.wujialiang.springstu01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ApplicationContext ctx =new
ClassPathXmlApplicationContext("applicationContext.xml");
HelloSpring hello =(HelloSpring)ctx.getBean("helloSpring");
hello.show();
}
}
main下新建resources文件夹,该文件下新建applicationContext.xml文章来源:https://www.toymoban.com/news/detail-691585.html
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="helloSpring"
class="com.wujialiang.springstu01.HelloSpring">
<property name="userName" value="李四"></property>
</bean>
</beans>
运行项目
文章来源地址https://www.toymoban.com/news/detail-691585.html
到了这里,关于maven搭建spring项目的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!