您现在的位置:首页 >> 前端 >> 内容

Spring框架学习

时间:2016/12/10 13:41:00 点击:

  核心提示:1.先导入spring中的核心jar包如图所示:2,我们建立个配置文件,点击new创建一个file文件(是在src目录下创建),可以默认命名为application.Context.xml然后我们可以...

1.先导入spring中的核心jar包如图所示:

2,我们建立个配置文件,点击new创建一个file文件(是在src目录下创建),可以默认命名为application.Context.xml

然后我们可以看到,在这里,配置的xml文件变成了绿色的叶子,这里表示可执行文件,当然这个文件咱们可以随便取名,当xml有可执行的文件 时就会变为绿色,

3,我们先来个实体类 User:

package com.qingmang.test;


public class User {

private int id;

private String name;


public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


}

创建好实体类之后,我们现在来xml中配置我们需要使用到的对象;

----------------------------------------------------------------------------------------------------------------------------------------------

xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:p="https://www.springframework.org/schema/p"
xmlns:context="https://www.springframework.org/schema/context"
xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
https://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
https://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">

 

 

配置好之后我们来个AppDemo测试下;

----------------------------------------------------------------------------------------------------------------------------------

package com.qingmang.test;


import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;


public class AppDemo {

public static void main(String[] args) {

//spring 操作时对象的创建交给spring去操作;
//如何去获取对象
Resource resource=new ClassPathResource("applicationContext.xml");

//创建工厂对象
BeanFactory bf=new XmlBeanFactory(resource);
User user= (User) bf.getBean("user");//你当前需要使用的id
System.out.println(user);
}


}

------------------------------------------------------------------------------------------------------------------------------------------------

显示如下:

2016-12-8 11:20:26 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
com.qing.test.User@44050988

这样我们就创建了了,我们要使用的对象,相当于new了一个对象

----------------------------------------------------------------------------------------------------------------------------------

配置文件中的一个节点,scope里默认是singleton,也就是单例模式

xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:p="https://www.springframework.org/schema/p"
xmlns:context="https://www.springframework.org/schema/context"
xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
https://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
https://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">

我们更改了默认的属性,改为非单例模式:prototype

显示结果如下:

-------------------------------------------------------------------------------------

2016-12-8 12:32:04 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2c41d05d: startup date [Thu Dec 08 12:32:04 CST 2016]; root of context hierarchy
2016-12-8 12:32:04 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
2016-12-8 12:32:04 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@291946c2: defining beans [user]; root of factory hierarchy
com.qing.test.User@290fd7f6
com.qing.test.User@4f2b6c89

 

Tags:SP PR RI IN 
作者:网络 来源:y976181862