Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring-在idea下开发helloworld程序 #3

Open
mkdir1 opened this issue Dec 16, 2015 · 0 comments
Open

Spring-在idea下开发helloworld程序 #3

mkdir1 opened this issue Dec 16, 2015 · 0 comments

Comments

@mkdir1
Copy link
Owner

mkdir1 commented Dec 16, 2015

Spring在idea下的搭建

示例程序

package spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloController{
public static void main(String[] args) {
    //ApplicationContext context = new ClassPathXmlApplicationContext( new String[] {"spring-config.xml"});
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    HelloApi api = (HelloApi)context.getBean("helloBean");
    String s = api.helloSpring(3);
    System.out.println("the s="+s);
}
}

这里注意几点:

  • 1.ClassPathXmlApplicationConttext()的路径放在了resource/下面,但这个要在设置中设置为resource文件类型
    1. 涉及到的类有两项,在新建项目的时候,创建一个Spring项目,就会下载相关的包

BeanFactory和ApplicationContext
org.springframework.beans及org.springframework.context包是Spring IoC容器的基础。BeanFactory提供的高级配置机制,使得管理任何性质的对象成为可能。 ApplicationContext是BeanFactory的扩展,功能得到了进一步增强,比如更易与Spring AOP集成、消息资源处理(国际化处理)、事件传递及各种不同应用层的context实现(如针对web应用的WebApplicationContext)。

xml

Resource resource = new FileSystemResource("beans.xml");  
BeanFactory factory = new XmlBeanFactory(resource);  

2

ClassPathResource resource = new ClassPathResource("beans.xml");  
BeanFactory factory = new XmlBeanFactory(resource);  
ApplicationContext context = new ClassPathXmlApplicationContext(  
        new String[] {"applicationContext.xml", "applicationContext-part2.xml"});  
// of course, an ApplicationContext is just a BeanFactory  
BeanFactory factory = (BeanFactory) context;  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant