JAVA服务器开发

IDEA_Spring入门

2017-07-11  本文已影响259人  Felix_

1、打开"Project Structure",在“library”里面加入spring需要的jar包;

commons-logging-1.1.1

spring-aop-4.1.6.RELEASE

spring-aspects-4.1.6.RELEASE

spring-beans-4.1.6.RELEASE

spring-context-4.1.6.RELEASE

spring-context-support-4.1.6.RELEASE

spring-core-4.1.6.RELEASE

spring-expression-4.1.6.RELEASE

spring-instrument-4.1.6.RELEASE

spring-instrument-tomcat-4.1.6.RELEASE

spring-jdbc-4.1.6.RELEASE

spring-jms-4.1.6.RELEASE

spring-messaging-4.1.6.RELEASE

spring-orm-4.1.6.RELEASE

spring-oxm-4.1.6.RELEASE

spring-test-4.1.6.RELEASE

spring-tx-4.1.6.RELEASE

spring-web-4.1.6.RELEASE

spring-webmvc-4.1.6.RELEASE

spring-webmvc-portlet-4.1.6.RELEASE

spring-websocket-4.1.6.RELEASE

2、创建一个包,然后创建一个类“HelloWorld”,定义一个私有变量“message”;


public classHelloWorld{

privateString message;

publicString getMessage() {

returnmessage;

}

public voidsetMessage(Stringmessage) {

this.message= message;

}

}

3、创建Beans.xml文件(必须在src目录中),并添加对应的配置;

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="helloworld" class="com.tutorialspoint.HelloWorld">
        <property name="message" value="hello world!!!"></property>
    </bean>
</beans>

4、创建一个MainApp的JAVA类,然后写入如下代码:

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloworld");
        System.out.println(obj.getMessage());
    }
}

5、运行即可;
注:
在Beans.xml中可能会出现“idea application context not configured for this file”,进入"Project Structure",
在“Module”里面添加对应的spring的配置文件beans.xml即可。

上一篇下一篇

猜你喜欢

热点阅读