Spring通过不同的工厂创建对象
本文最后更新于 1524 天前,其中的信息可能已经有所发展或是发生改变。

结构图:

创建工程并导入Spring框架。代码如下:
[sourcecode language=”java” title=”com.biz.GoodsBIZ.java”]
package com.biz;

import com.dao.GoodsDao;

public class GoodsBIZ {
private GoodsDao goodsDao;

public GoodsDao getGoodsDao() {
return goodsDao;
}

public void setGoodsDao(GoodsDao goodsDao) {
this.goodsDao = goodsDao;
}
public void save(){
System.out.println("GoodsBIZ.save() is called…");
this.goodsDao.save();
}
}
[/sourcecode]
[sourcecode language=”java” title=”com.controller.GoodsController.java”]
package com.controller;

import com.biz.GoodsBIZ;

public class GoodsController {
private GoodsBIZ goodsBIZ;
public GoodsController(GoodsBIZ goodsBIZ){
System.out.println("GoodsController的有参构造被调用");
this.goodsBIZ = goodsBIZ;
}

public void save(){
System.out.println("GoodsController.save() is called…");
goodsBIZ.save();
}
}
[/sourcecode]
[sourcecode language=”java” title=”com.dao.GoodsDao.java”]
package com.dao;

public interface GoodsDao {
public void save();
}
[/sourcecode]
[sourcecode language=”java” title=”com.dao.impl.GoodsDaoImpl.java”]
package com.dao.impl;

import com.dao.GoodsDao;

public class GoodsDaoImpl implements GoodsDao {

@Override
public void save() {
System.out.println("GoodsDaoImpl.save() is called…");
}
}
[/sourcecode]
[sourcecode language=”java” title=”com.dao.factory.DaoFactory.java”]
package com.factory;

import com.dao.GoodsDao;
import com.dao.impl.GoodsDaoImpl;

public class DaoFactory {
public static GoodsDao createDao(){
return new GoodsDaoImpl();
}
public GoodsDao getDao(){
return new GoodsDaoImpl();
}
}
[/sourcecode]
xml文件:
[sourcecode language=”xml” title=”applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<bean name="goodsDao" class="com.dao.impl.GoodsDaoImpl"/>

<bean name="goodsBIZ" class="com.biz.GoodsBIZ">
<property name="goodsDao"> <!– GoodsBIZA有个属性叫goodsDAO –>
<ref bean="goodsDao"/> <!–依赖。引用了上面那个bean(1) –>
</property>
</bean>

<bean name="goodsController" class="com.controller.GoodsController">
<constructor-arg name="goodsBIZ" ref="goodsBIZ"/>
</bean>

<bean name="dao1" class="com.factory.DaoFactory" factory-method="createDao"/>

<bean name="daoFactory" class="com.factory.DaoFactory"/>
<bean name="dao2" factory-bean="daoFactory" factory-method="getDao" scope="prototype"/>
<!– scope属性默认为”singleton”,表示单例;取prototype代表多例 –>
</beans>
[/sourcecode]
测试类:
[sourcecode language=”java” title=”com.test.SpringTest.java”]
package com.test;

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

import com.biz.GoodsBIZ;
import com.controller.GoodsController;
import com.dao.GoodsDao;

public class SpringTest {

public static void main(String[] args) {

@SuppressWarnings("resource")
ApplicationContext context =
new ClassPathXmlApplicationContext("/applicationContext.xml");

//注释掉的是不同的方法创建出的对象

/*GoodsDao goodsDao = (GoodsDao) context.getBean("goodsDao");
goodsDao.save();*/

/*GoodsBIZ goodsBIZ = (GoodsBIZ) context.getBean("goodsBIZ");
goodsBIZ.save();*/

/*GoodsController goodsController = (GoodsController) context.getBean("goodsController");
goodsController.save();*/

/*GoodsDao dao1 = (GoodsDao) context.getBean("dao1");
System.out.println(dao1);*/

GoodsDao dao21 = (GoodsDao) context.getBean("dao2");
System.out.println(dao21);
GoodsDao dao22 = (GoodsDao) context.getBean("dao2");
System.out.println(dao22);
System.out.println(dao21 == dao22);
}

}
[/sourcecode]

项目源代码:[bdbtn]https://pan.benzhu.xyz/代码/源代码/Spring/源代码/Spring01.rar[/bdbtn]

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇