Spring實戰(zhàn)之依賴關(guān)系注入之后的行為示例
本文實例講述了Spring實戰(zhàn)之依賴關(guān)系注入之后的行為。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
<!-- 配置chinese Bean,使用init-method="init"
指定該Bean所有屬性設(shè)置完成后,自動執(zhí)行init方法 -->
<bean id="chinese" class="org.crazyit.app.service.impl.Chinese"
init-method="init">
<property name="axe" ref="steelAxe"/>
</bean>
</beans>
二 接口
1 Axe
package org.crazyit.app.service;
public interface Axe
{
public String chop();
}
2 Person
package org.crazyit.app.service;
public interface Person
{
public void useAxe();
}
三 Bean
1 Chinese
package org.crazyit.app.service.impl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.*;
import org.springframework.context.*;
import org.crazyit.app.service.*;
public class Chinese implements Person , InitializingBean
, BeanNameAware, ApplicationContextAware
{
private Axe axe;
public void setBeanName(String beanName)
{
System.out.println("===setBeanName===");
}
public void setApplicationContext(ApplicationContext ctx)
{
System.out.println("===setApplicationContext===");
}
public Chinese()
{
System.out.println("Spring實例化主調(diào)bean:Chinese實例...");
}
// axe的setter方法
public void setAxe(Axe axe)
{
System.out.println("Spring調(diào)用setAxe()執(zhí)行依賴注入...");
this.axe = axe;
}
public void useAxe()
{
System.out.println(axe.chop());
}
// 測試用的初始化方法
public void init()
{
System.out.println("正在執(zhí)行初始化方法 init...");
}
// 實現(xiàn)InitializingBean接口必須實現(xiàn)的方法
public void afterPropertiesSet() throws Exception
{
System.out.println("正在執(zhí)行初始化方法 afterPropertiesSet...");
}
}
2 SteelAxe
package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class SteelAxe
implements Axe
{
public SteelAxe()
{
System.out.println("Spring實例化依賴bean:SteelAxe實例...");
}
public String chop()
{
return "鋼斧砍柴真快";
}
}
四 測試類
package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
public class BeanTest
{
public static void main(String[] args)throws Exception
{
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
Person p = ctx.getBean("chinese" , Person.class);
p.useAxe();
}
}
五 測試結(jié)果
Spring實例化依賴bean:SteelAxe實例...
Spring實例化主調(diào)bean:Chinese實例...
Spring調(diào)用setAxe()執(zhí)行依賴注入...
===setBeanName===
===setApplicationContext===
正在執(zhí)行初始化方法 afterPropertiesSet...
正在執(zhí)行初始化方法 init...
鋼斧砍柴真快
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
- Spring 中 @Service 和 @Resource 注解的區(qū)別
- Spring框架中 @Autowired 和 @Resource 注解的區(qū)別
- 詳解Spring注解--@Autowired、@Resource和@Service
- 詳解Spring關(guān)于@Resource注入為null解決辦法
- Spring注解@Resource和@Autowired區(qū)別對比詳解
- 詳解Spring依賴注入:@Autowired,@Resource和@Inject區(qū)別與實現(xiàn)原理
- spring boot的maven配置依賴詳解
- 詳解Spring Boot配置排序依賴技巧
- 簡單了解Spring循環(huán)依賴解決過程
- spring依賴注入知識點分享
- spring依賴注入原理與用法實例分析
- Spring實戰(zhàn)之使用@Resource配置依賴操作示例

