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

Spring的配置项及其作用分析

2016-07-14 11:59 495 查看
</pre>日期: 2016-7-14<p></p><p></p><p>内容: Spring的配置项及其作用分析</p><h2><span style="color:#3366ff">1、 Bean的配置项:</span></h2><div><span style="white-space:pre"></span>①、ID:</div><div><span style="white-space:pre"></span>在整个IOC容器中Bean的唯一标识:</div><div><span style="white-space:pre"></span></div><div><span style="white-space:pre"></span>②、Class:</div><div><span style="white-space:pre"></span>指具体要实例化的哪一个类。</div><div><span style="white-space:pre"></span></div><div><span style="white-space:pre"></span>③、Scope:</div><div><span style="white-space:pre"></span>值范围,作用域;</div><div></div><div><span style="white-space:pre"></span>④、Constructor arguments:</div><div><span style="white-space:pre"></span>指构造器的参数;</div><div></div><div><span style="white-space:pre"></span>⑥、Properties:</div><div><span style="white-space:pre"></span>指属性,比如就是设值注入的属性设值;</div><div></div><div><span style="white-space:pre"></span>⑦、Autowiring mode</div><div><span style="white-space:pre"></span>自动装配的模式;<span style="white-space:pre"> </span></div><div></div><div><span style="white-space:pre"></span>⑧、lazy-initialization mode:</div><div><span style="white-space:pre"></span>懒加载模式;</div><div></div><div><span style="white-space:pre"></span>⑨、Initialization/destruction method:</div><div><span style="white-space:pre"></span>初始化和销毁方法;</div><div></div><div></div><div><span style="white-space:pre"></span>假如需要在Bean中获取一个实例有两种方式,一种是通过ID去获取,另一种是通过class获取;</div><h2><span style="white-space:pre"></span><span style="color:#3366ff">2、 Bean的作用域:</span></h2><div><span style="white-space:pre"></span>Bean的作用域通常有五种类型:分别描述如下</div><div></div><div><span style="white-space:pre"></span>①、singleton: 单例模式,指一个Bean容器中只存在一份实例。</div><div></div><div><span style="white-space:pre"></span>②、prototype:每次请求(每一次使用)创建新的实例,destory方式不生效。</div><div></div><div><span style="white-space:pre"></span>③、request: 每一次http请求都会创建新的实例,并且只在当前的request内有效。</div><div></div><div><span style="white-space:pre"></span>④、session: 同上,每次http请求之后创建,并且只在当前session内有效。</div><div></div><div><span style="white-space:pre"></span>⑤、global session: 基于portlet的web中有效(portlet定义了global session),如果实在web中就和session相同。</div><div></div><div></div><div><span style="white-space:pre"></span>以下测试singleton和prototype两个作用域的使用:</div><div></div><h4><span style="white-space:pre"></span>1)、singleton单例:</h4><div><span style="white-space:pre"></span>在com.bean.BeanScope.java:</div><div><span style="white-space:pre"></span><pre name="code" class="java">package com.bean;

//测试singleton的作用域
public class BeanScope {

public void say()
{
//使用hashCode()测试是不是单例的--->在配置文件applicationContext_Beanscope.xml中配置Bean
System.out.println("BeanScope say: "+this.hashCode());
}
}


在applicationContext_BeanScope.xml中添加scope="singleton"的选项,这里也可以不添加,spring默认的scope就是singleton。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<bean id="beanScope" class="com.bean.BeanScope" scope="singleton"></bean>
</beans>


com.test.BeanScopeTest.java中添加测试方法:
package com.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bean.BeanScope;

//测试BeanScope的测试用例
public class BeanScopeTest {

@Test
public void testSay()
{
//加载BeanScope的配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_BeanScope.xml");
//获得BeanScope实例
BeanScope bs = (BeanScope)ac.getBean("beanScope");
//调用方法:say()
bs.say();

BeanScope bs1 = (BeanScope)ac.getBean("beanScope");
//调用方法:say()
bs1.say();

}
}


运行结果显示: 两个bean对象的hashCode值是相同的,因此属于单实例模式:
2016/07/14 14:16:00 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1be2d65: startup date [Thu Jul 14 14:16:00 CST 2016]; root of context hierarchy
2016/07/14 14:16:00 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_BeanScope.xml]
<span style="color:#ff0000;">BeanScope say: 6588476
BeanScope say: 6588476</span>

2)、prototype:每次请求(每一次使用)创建新的实例,destory方式不生效。

这里的测试只需要将applicationContext_BeanScope.xml里的scope选项改成prototype之后就可以了。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<bean id="beanScope" class="com.bean.BeanScope" scope="prototype"></bean>
</beans>


添加测试用例:
@Test
public void testSay2()
{
//加载BeanScope的配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_BeanScope.xml");
//获得BeanScope实例
BeanScope bs = (BeanScope)ac.getBean("beanScope");
//调用方法:say()
bs.say();

BeanScope bs1 = (BeanScope)ac.getBean("beanScope");
//调用方法:say()
bs1.say();

}

测试结果如下:
2016/07/14 14:24:10 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1be2d65: startup date [Thu Jul 14 14:24:10 CST 2016]; root of context hierarchy
2016/07/14 14:24:10 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_BeanScope.xml]
<span style="color:#ff0000;">BeanScope say: 14306161
BeanScope say: 26156414</span>
很明显两个bean实例的hashCode已经不再一样了,因此是属于不同的对象实例。


3、Bean的生命周期:

分四个部分:定义,初始化,使用,销毁;

初始化: 
--实现org.springframework.beans.factory.InitializingBean接口,覆盖afterPropertiesSet方法。
--配置init-method
<bean id="exampleInitBean" class="example.ExampleBean" init-method="init"></bean>


public classExampleBean{
public void init()
{
//do somethings
}
}


销毁: 
--实现org.springframework.beans.factory.DisposableBean接口,覆盖sestory方法。
--配置destory-method
<bean id="exampleInitBean" class="example.ExampleBean" destroy-method="cleanup"></bean>

public classExampleBean{
public void cleanup()
{
//do somethings for destroy
}
}

定义全局默认初始化和销毁方法:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
default-init-method="init" defaule-destroy-method="destroy">

<bean id="beanScope" class="com.bean.BeanScope" scope="prototype"></bean>

</beans>


具体的测试用例设置:

com.bean.BeanLifeCycle.java:
package com.bean;

public class BeanLifeCycle {

public void start()
{
System.out.println("Bean Start!");
}

public void stop()
{
System.out.println("Bean Stop!");
}
}


applicationContext_BeanLifeCycle.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<!-- 配置初始化方法(start)和销毁方法(stop) -->
<bean id="beanLifeCycle" class="com.bean.BeanLifeCycle" scope="prototype" init-method="start" destroy-method="stop"></bean>

</beans>


测试代码:com.test.TestBeanCycle.java:
<pre name="code" class="java">package com.test;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bean.BeanLifeCycle;

//测试Bean的生命周期
public class BeanLifeCycleTest {

//加载xml配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_LifeCycle.xml");

@Before
public void doBefore()
{
//获得当前时间
Date date = new Date();

//格式化格式
String formatStyle = "yyyy年mm月dd日 hh时:mm分:SS秒";
//格式化时间
SimpleDateFormat sdf = new SimpleDateFormat(formatStyle);

//输出当前日期:
System.out.println("开始日期: "+sdf.format(date));
}

@After
public void doAfter()
{
//获得当前时间
Date date = new Date();

//格式化格式
String formatStyle = "yyyy年mm月dd日 hh时:mm分:SS秒";
//格式化时间
SimpleDateFormat sdf = new SimpleDateFormat(formatStyle);

//输出当前日期:
System.out.println("结束日期: "+sdf.format(date));

}

@Test
public void testBeanLifeCycle()
{
BeanLifeCycle blc = (BeanLifeCycle)ac.getBean("beanLifeCycle");

}
}



输出的结果:
2016/07/14 15:14:27 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@b8df17: startup date [Thu Jul 14 15:14:27 CST 2016]; root of context hierarchy
2016/07/14 15:14:27 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_LifeCycle.xml]
开始日期: 2016年14月14日 03时:14分:83秒
Bean Start!
Hello world!
结束日期: 2016年14月14日 03时:14分:100秒


没有执行销毁方法? 为什么呢!

使用第二种方法实现初始化和销毁操作:
--实现org.springframework.beans.factory.DisposableBean接口,覆盖sestory方法。
--实现org.springframework.beans.factory.InitializingBean接口,覆盖afterPropertiesSet方法。

xml配置文件的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<!-- 配置初始化方法(start)和销毁方法(stop) -->
<!--  init-method="start" destroy-method="stop" -->
<bean id="beanLifeCycle" class="com.bean.BeanLifeCycle" scope="prototype"></bean>

</beans>

com.bean.BeanLifeCycle.java:

package com.bean;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class BeanLifeCycle implements InitializingBean,DisposableBean{

//方法1: 编写初始化和销毁方法
//	public void start()
//	{
//		System.out.println("Bean Start!");
//	}
//
//	public void stop()
//	{
//		System.out.println("Bean Stop!");
//	}
//
//	public void say()
//	{
//		System.out.println("Hello world!");
//	}

//方法2: 实现DisposableBean,InitializingBean接口

@Override
public void destroy() throws Exception {
System.out.println("Bean Destory!");

}

@Override
public void afterPropertiesSet() throws Exception {

System.out.println("Bean afterPropertiesSet!");
}
}

执行结果:
2016/07/14 15:21:40 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@b8df17: startup date [Thu Jul 14 15:21:40 CST 2016]; root of context hierarchy
2016/07/14 15:21:40 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_LifeCycle.xml]
开始日期: 2016年21月14日 03时:21分:181秒
Bean afterPropertiesSet!
结束日期: 2016年21月14日 03时:21分:200秒


同样没有执行销毁方法!

使用默认初始化和销毁方法的情况:在此省略。


假如同时使用了三种初始化和销毁方法之后的先后顺序是什么?

在测试之后得出的结论是:实现接口的优先级最高,其次就是init-method,destory.默认的初始化和销毁方法会被省略。



4、Bean的自动装配:

自动装配的选项:

①、NO: 不做任何操作(这个是默认的选项)。

②、byName: 根据属性名自动装配。 此选项将检查容器并根据名字查找与属性完全一致的Bean,并将其与属性自动装配。

③、byType: 如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配;如果存在多个该类型的bean,那么跑出异常,并指出不能使用byType的
方式进行自动装配;如果没有找到相匹配的bean,那么什么事都不做。

④、Constructor: 与byType的方式类似,不同之处在于它英语于构造器参数。如果容器中没有找到与构造器参数类型一致的bean,那么抛出异常。


使用byName的方式演示:

实例演示:之前未使用自动装配bean的装配方式:
com.autoWiring.AutoWiringDAO.java:
package com.autowiring;

public class AutoWiringDAO {

public void say(String word)
{
System.out.println("AutoWiringDAO: "+word);
}
}

com.autoWiring.AutoWirinService.java:
package com.autowiring;

public class AutoWiringService {

<span style="white-space:pre">	</span>private AutoWiringDAO autowiringDAO;

<span style="white-space:pre">	</span>//生成set方法
<span style="white-space:pre">	</span>public void setAutowiringDAO(AutoWiringDAO autowiringDAO) {
<span style="white-space:pre">		</span>this.autowiringDAO = autowiringDAO;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>public void say(String word)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>this.autowiringDAO.say(word);
<span style="white-space:pre">	</span>}
}

applicationContext_AutoWiring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
><!-- 设置默认装配方式为byName的方式装载 -->

<bean id="autowiringService" class="com.autowiring.AutoWiringService">
<!-- 未使用自动转配之前的配置方式是这样的通过property属性配置 -->
<property name="autowiringDAO" ref="autowiringDAO"></property>
</bean>

<bean id="autowiringDAO" class="com.autowiring.AutoWiringDAO"></bean>
</beans>


测试类代码:
@Test
public void testAutoWiring()
{
//加载Spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_AutoWiring.xml");

AutoWiringService aws = (AutoWiringService)ac.getBean("autowiringService");

aws.say("Spring Bean!");
}


运行结果:
2016/07/15 12:36:35 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1be2d65: startup date [Fri Jul 15 12:36:35 CST 2016]; root of context hierarchy
2016/07/15 12:36:35 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_LifeCycle.xml]
开始日期: 2016年36月15日 12时:36分:34秒
2016/07/15 12:36:36 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@175d6ab: startup date [Fri Jul 15 12:36:36 CST 2016]; root of context hierarchy
2016/07/15 12:36:36 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_AutoWiring.xml]
AutoWiringDAO: Spring Bean!
结束日期: 2016年36月15日 12时:36分:115秒


设置byName自动装配的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
<span style="color:#ff0000;">default-autowire="byName"</span>><!-- 设置默认装配方式为byName的方式装载 -->

<bean id="autowiringService" class="com.autowiring.AutoWiringService">
<!-- 未使用自动转配之前的配置方式是这样的通过property属性配置
<property name="autowiringDAO" ref="autowiringDAO"></property>-->
</bean>

<bean id="autowiringDAO" class="com.autowiring.AutoWiringDAO"></bean>
</beans>


测试类运行结果:
2016/07/15 12:36:35 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1be2d65: startup date [Fri Jul 15 12:36:35 CST 2016]; root of context hierarchy
2016/07/15 12:36:35 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_LifeCycle.xml]
开始日期: 2016年36月15日 12时:36分:34秒
2016/07/15 12:36:36 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@175d6ab: startup date [Fri Jul 15 12:36:36 CST 2016]; root of context hierarchy
2016/07/15 12:36:36 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_AutoWiring.xml]
AutoWiringDAO: Spring Bean!
结束日期: 2016年36月15日 12时:36分:115秒


运行结果是一样的。


使用byType的方式演示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
<span style="color:#ff0000;">default-autowire="byType"</span>><!-- 设置默认装配方式为byName的方式装载 -->

<bean id="autowiringService" class="com.autowiring.AutoWiringService">
<!-- 未使用自动转配之前的配置方式是这样的通过property属性配置
<property name="autowiringDAO" ref="autowiringDAO"></property>-->
</bean>

<bean id="autowiringDAO" class="com.autowiring.AutoWiringDAO"></bean>
</beans>
其它的代码不变,演示结果如下:
2016/07/15 12:42:52 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1be2d65: startup date [Fri Jul 15 12:42:52 CST 2016]; root of context hierarchy
2016/07/15 12:42:52 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_LifeCycle.xml]
2016/07/15 12:42:53 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@160a26f: startup date [Fri Jul 15 12:42:53 CST 2016]; root of context hierarchy
2016/07/15 12:42:53 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_AutoWiring.xml]
开始日期: 2016年42月15日 12时:42分:145秒
AutoWiringDAO: Spring Bean!
结束日期: 2016年42月15日 12时:42分:228秒


byType的注入与bean的id是没有直接关系的。即使将以下xml文件里的Id改变也不会影响注入。但是byName是要寻找Id的。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
default-autowire="byType"><!-- 设置默认装配方式为byName的方式装载 -->

<bean id="autowiringService" class="com.autowiring.AutoWiringService">
<!-- 未使用自动转配之前的配置方式是这样的通过property属性配置
<property name="autowiringDAO" ref="autowiringDAO"></property>-->
</bean>

<bean <span style="color:#ff0000;">id="autowiringDAO@22221"</span> class="com.autowiring.AutoWiringDAO"></bean>
</beans>


构造器(constructor)注入:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
<span style="color:#ff0000;">default-autowire="constructor"</span>><!-- 设置默认装配方式为byName的方式装载 -->

<bean id="autowiringService" class="com.autowiring.AutoWiringService">
<!-- 未使用自动转配之前的配置方式是这样的通过property属性配置
<property name="autowiringDAO" ref="autowiringDAO"></property>-->
</bean>

<bean id="autowiringDAO111" class="com.autowiring.AutoWiringDAO"></bean>
</beans>


要在com.autoWiring.AutoWirinService.java:中添加构造方法:
package com.autowiring;

public class AutoWiringService {

private AutoWiringDAO autowiringDAO;

public AutoWiringService(AutoWiringDAO autowiringDAO)
{
this.autowiringDAO = autowiringDAO;
}

//生成set方法
public void setAutowiringDAO(AutoWiringDAO autowiringDAO) {
this.autowiringDAO = autowiringDAO;
}

public void say(String word)
{
this.autowiringDAO.say(word);
}
}
使用同样的一个测试方法运行结果如下:
2016/07/15 12:51:09 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1be2d65: startup date [Fri Jul 15 12:51:09 CST 2016]; root of context hierarchy
2016/07/15 12:51:09 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_LifeCycle.xml]
2016/07/15 12:51:09 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@160a26f: startup date [Fri Jul 15 12:51:09 CST 2016]; root of context hierarchy
开始日期: 2016年51月15日 12时:51分:730秒
2016/07/15 12:51:09 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_AutoWiring.xml]
AutoWiringDAO: Spring Bean!
结束日期: 2016年51月15日 12时:51分:818秒


5、Resources和ResourceLoader:

1、Resources:针对于资源文件的统一接口。

①,UrlResource:URL下的资源,根据一个URL可以获取。

②、ClassPathResource:获取类路径下的资源。

③、FileSystemResource:获取文件系统下的资源。

④、ServletContextResource:ServletContext封装的资源,用于访问ServletContext环境下的资源。

⑤、InputStreamResource:这对于输入流封装的资源。

⑥、ByteArrayResource:针对于字节数组封装的资源。

2、ResourceLoader:是对Resource进行加载的一个接口,在Spring的IOC容器中所有的ApplicationContext都实现了ResourceLoader这个接口。也就是说所有的applicatio context都可以用来获取Resource的实例;

public interfact ResourceLoader()
{
Resource getResource(String location);
}
Resource template = ctx.getResource("some/resource/path/mytemplate.txt");
Resource template = ctx.getResource("classpath:some/resource/path/mytemplate.txt");

测试Resource的实例一:使用classpath的方式。

com.resource_Resourceloader._Resource.java

package com.resource_Resourceloader;

import java.io.IOException;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;

public class _Resource implements ApplicationContextAware{

ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;

}

public void resource() throws IOException
{
Resource resource = applicationContext.getResource("classpath:config.txt");

System.out.println("文件名: "+resource.getFilename());//获得文件名
System.out.println("长度: "+resource.contentLength());//输出长度
}

}

application_Resource.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<bean id="_resource" class="com.resource_Resourceloader._Resource"></bean>

</beans>
测试类:

package com.resource_Resourceloader;

import java.io.IOException;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bean.BadMan;
import com.bean.GoodMan;
import com.service.InjectionService;

//测试环境配置是否成功
public class TestResource {

@Test
public void testResource()
{
//加载Spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_Resource.xml");

_Resource resource = (_Resource)ac.getBean("_resource");
try {
resource.resource();
} catch (IOException e) {
e.printStackTrace();
}

}
}

测试结果:
2016/07/15 16:23:10 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@13e8d89: startup date [Fri Jul 15 16:23:10 CST 2016]; root of context hierarchy
2016/07/15 16:23:10 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_Resource.xml]
文件名: config.txt
长度: 12


测试Resource的实例二:使用file的方式。

改变类里的文件路径:

package com.resource_Resourceloader;

import java.io.IOException;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;

public class _Resource implements ApplicationContextAware{

ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;

}

public void resource() throws IOException
{
//使用calsspath的方式
//Resource resource = applicationContext.getResource("classpath:config.txt");

//使用file的方式C:\EasyLife\workspace\Spring_4WebProjectTest\src\config.txt
<span style="color:#ff0000;">Resource resource = applicationContext.getResource("file:C:\\EasyLife\\workspace\\Spring_4WebProjectTest\\src\\config.txt");//使用的是double slash(双斜杠)</span>
System.out.println("文件名: "+resource.getFilename());//获得文件名
System.out.println("长度: "+resource.contentLength());//输出长度
}

}

运行测试类的结果:

2016/07/15 16:37:32 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@13e8d89: startup date [Fri Jul 15 16:37:32 CST 2016]; root of context hierarchy
2016/07/15 16:37:32 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_Resource.xml]
文件名: config.txt
长度: 12


测试Resource的实例三: 使用URL的方式:

测试的uRL:http://spring.io/understanding/JSON

package com.resource_Resourceloader;

import java.io.IOException;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;

public class _Resource implements ApplicationContextAware{

ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;

}

public void resource() throws IOException
{
//使用calsspath的方式
//Resource resource = applicationContext.getResource("classpath:config.txt");

//使用file的方式C:\EasyLife\workspace\Spring_4WebProjectTest\src\config.txt
//Resource resource = applicationContext.getResource("file:C:\\EasyLife\\workspace\\Spring_4WebProjectTest\\src\\config.txt");//使用的是double slash(双斜杠)

//使用URL的方式
Resource resource = applicationContext.getResource("url:http://spring.io/understanding/JSON");
System.out.println("文件名: "+resource.getFilename());//获得文件名
System.out.println("长度: "+resource.contentLength());//输出长度
}

}

测试结果:

2016/07/15 16:41:06 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@13e8d89: startup date [Fri Jul 15 16:41:06 CST 2016]; root of context hierarchy
2016/07/15 16:41:06 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext_Resource.xml]
文件名: JSON
长度: -1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息