切记:不骄不躁 不浮不沉 学会思考
简介:spring中的bean管理 主要有两种方式 基于配置文件和基于注解方式 本文主要介绍基于配置文件方式的bean管理
在Java中 使用对象 先是要new一个对象出来 在spring中 通过配置文件实现 不需要new ,它的过程是:
1.在xml文件中 定义class
2.dom4j解析xml
3.对应的工厂创建class
该方法的好处在于 class文件位置变换后 只需要修改xml文件中对应的bean路径,而不需要修改java代码 下面举个栗子:
第一步:创建web工程 导入基本的springjar包和需要的java包
第二步:创建spring核心配置文件 我这里是放在src目录下 名为applicationContext.xml
附上工程图:
第三步:配置spring的核心配置文件
(1)创建spring的xml配置文件,该文件是spring的核心配置文件 名称和位置不固定 但是一般建议位置放在src下 名称建议为applicationContex (名称位置可自定义,路径放对就好)
(2)引入spring约束,如下。这些约束不需要记 只需要在你下的spring包里面找到xsd-configuration.html文件打开 直接拉到
文末,从后往前找,比较方便,这个文件里面就有对每个约束的介绍。我这里的文件路径是;F:\spring-framework-4.2.4.RELEASE-dist\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.htmlxml约束引入后如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- bean definitions here -->
</beans>
第四步 创建class
package per.spring.beans;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* spring 基于配置文件的bean实现
*
*/
public class User {
public void add() {
System.out.println("这是一个add方法");
}
}
第五步:在xml中配置bean对象 代码如下 在xml中 主要通过<bean>标签来配置java对象
id:是唯一标识bean.不能用特殊字符:×#@ ,不能用数字开头,一般本人喜欢用类名小写来作id
class:是类的路径
scope:是指创建的对象 是单例还是多例 默认值是singleton(单例) prototype(多例)以下举例说明
单例:
java代码:测试方法中 创建了两个user对象
package per.spring.beans;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* spring 基于配置文件的bean实现
*
*/
public class User {
public void add() {
System.out.println("这是一个add方法");
}
@Test
public void test() {
//加载spring配置文件 根据文件 创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//得到创建的对象
User user = (User) context.getBean("user");
User user2 = (User) context.getBean("user");
user.add();
System.out.println("根据xml创建对象成功!");
//默认是singleton 创建的对象是单实例对象 prototype 多实例对象
System.out.println(user);
System.out.println(user2);
}
}
xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置bean -->
<bean id="user" class="per.spring.beans.User" ></bean>
</beans>
输出结果:如下图 创建 user对象成功 且在单例模式下 对象是同一个 多例:
xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置bean -->
<bean id="user" class="per.spring.beans.User" socpe="prototype"></bean>
</beans>
java代码:
package per.spring.beans;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* spring 基于配置文件的bean实现
*
*/
public class User {
public void add() {
System.out.println("这是一个add方法");
}
@Test
public void test() {
//加载spring配置文件 根据文件 创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//得到创建的对象
User user = (User) context.getBean("user");
User user2 = (User) context.getBean("user");
user.add();
System.out.println("根据xml创建对象成功!");
//默认是singleton 创建的对象是单实例对象 prototype 多实例对象
System.out.println(user);
System.out.println(user2);
}
}
输出结果:如下 多例模式下 创建了两个不同的user对象此处 只介绍了常用的bean创建 多个bean 即在配置文件里面加bean标签 对应做法如上