您的位置:首页 > 编程语言 > Java开发

J2EE之Eclipse配置Spring插件及Spring建立HelloWord

2016-10-30 17:17 253 查看

J2EE之Eclipse配置Spring插件及Spring建立HelloWord





代码实现:

HelloWord.java:

package com.mooc.spring.beans;

public class HelloWord {
private String name;

public void setName(String name) {
System.out.println("setname:"+name);
this.name = name;
}
public void hello(){
System.out.println("hello"+name);
}

public HelloWord(){
System.out.println("HelloWord's Constractor....");
}

}


main.java:

package com.mooc.spring.beans;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

public class main {

public static void main(String[] args) {
/*
//创建HelloWord的一个对象

HelloWord helloWord=new HelloWord();
//为那么赋值
helloWord.setName("atguigu");
helloWord.hello();

*/

//1.创建SpringIOC的容器对象
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

//      2.从IOC容器中获取Bean对象
//      HelloWord helloWord=(HelloWord) context.getBean("helloWord");

//调用hello方法
//      helloWord.hello();

}

}


applicationContext.xml:

package com.mooc.spring.beans;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

public class main {

public static void main(String[] args) {
/*
//创建HelloWord的一个对象

HelloWord helloWord=new HelloWord();
//为那么赋值
helloWord.setName("atguigu");
helloWord.hello();

*/

//1.创建SpringIOC的容器对象
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

//      2.从IOC容器中获取Bean对象
//      HelloWord helloWord=(HelloWord) context.getBean("helloWord");

//调用hello方法
//      helloWord.hello();

}

}


备注:该学习方法和以后学习spring的一些情况,均来自尚硅谷视频教程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring j2ee