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的一些情况,均来自尚硅谷视频教程