轻量级Java_EE企业应用实战-Bean实例的创建方式及依赖配置

1.1使用构造器创建Bean实例

package com.jtwl.office.service;
/**
 * @todo
 * @author 成明俊
 * @email 1047914020@qq.com
 * @date 2020年1月21日
 */
public interface Person {
	public void useAxe();

}

 

package com.jtwl.office.service.imp;

import com.jtwl.office.service.Axe;
import com.jtwl.office.service.Person;

/**
 * @todo
 * @author 成明俊
 * @email 1047914020@qq.com
 * @date 2020年1月21日
 */
public class Chinese implements Person{
	private Axe axe;
/*	public void setAxe(Axe axe) {
		this.axe = axe;
	}*/
	public Chinese(){
		System.out.println("Spring实例化主调Bean:Chinese实例...");
	}
	public void setAxe(Axe axe) {
		System.out.println("Spring执行依赖关系注入");
		this.axe = axe;
	}
	public void useAxe() {
		System.out.println(axe.chop());
	}

}
package com.jtwl.office.service;
/**
 * @todo
 * @author 成明俊
 * @email 1047914020@qq.com
 * @date 2020年1月21日
 */
public interface Axe {
	public String chop();
}
package com.jtwl.office.service.imp;

import com.jtwl.office.service.Axe;

/**
 * @todo
 * @author 成明俊
 * @email 1047914020@qq.com
 * @date 2020年1月21日
 */
public class SteelAxe implements Axe{
	public SteelAxe() {
		System.out.println("Spring实例化依赖Bean:SteelAxe实例...");
	}
	public String chop() {
		return "钢斧砍柴真快";
	}

}
<bean id="chinese" class="com.jtwl.office.service.imp.Chinese">
<property name="axe" ref="steelAxe"/> 
</bean>
<bean id="stonAxe" class="com.jtwl.office.service.imp.StoneAxe"/>
<bean id="steelAxe" class="com.jtwl.office.service.imp.SteelAxe"/>

 

package com.jtwl.office.controller;

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

import com.jtwl.office.entity.PersonService;
import com.jtwl.office.service.Person;

/**
 * @todo
 * @author 成明俊
 * @email 1047914020@qq.com
 * @date 2020年1月2日
 */
public class SpringTest {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
		Person p = ctx.getBean("chinese", Person.class);
		p.useAxe();
	}

}

结果为

结论:

1.2使用静态工厂方法创建Bean

 静态工厂方法所产生的产品是该接口的实例

package com.jtwl.office.service;
/**
 * @todo
 * @author 成明俊
 * @email 1047914020@qq.com
 * @date 2020年2月24日
 */
public interface Being {
	//接口定义testBeing方法
	public void testBeing();
}

实现类(产生这两个实现类的实例)

package com.jtwl.office.service.imp;

import com.jtwl.office.service.Being;

/**
 * @todo
 * @author 成明俊
 * @email 1047914020@qq.com
 * @date 2020年2月24日
 */
public class Dog implements Being{
	private String msg;
	//依赖注入时候必须的setter方法
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public void testBeing() {
		System.out.println(msg + "狗爱啃骨头");
	}

}

返回Being实例

package com.jtwl.office.factory;

import com.jtwl.office.service.Being;
import com.jtwl.office.service.imp.Dog;

/**
 * @todo
 * @author 成明俊
 * @email 1047914020@qq.com
 * @date 2020年2月24日
 */
public class BeingFactory {
	/**
	 * 获取Being实例的静态工厂方法
	 * @param arg决定返回那个Being实例的参数
	 */
	public static Being getBeing(String arg) {
		//调用此静态方法的参数为dog,则返回Dog实例
		if(arg.equalsIgnoreCase("dog")) {
			return new Dog();
		}else {
			return new Dog();
		}
	}

}

配置文件

<bean id="dog" class="com.jtwl.office.factory.BeingFactory" factory-method="getBeing">
	<constructor-arg value="dog"/>
	<property name="msg" value="我是狗"/>	
</bean> 

主程序

ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
Being b1 = ctx.getBean("dog",Being.class);
b1.testBeing();

结果

与构造器模式比较 

 

1.3调用实例工厂方法创建Bean 

与静态工厂区别

配置静态工厂方法时指定静态工厂类,配置实例工厂方法则指定工厂实例,Spring容器不在直接实例化该Bean,Spring容器仅仅调用实例工厂的工厂方法,工厂方法负责创建Bean实例。

实例工厂方法所产生的对象实现接口

package com.jtwl.office.service;
/**
 * @todo
 * @author 成明俊
 * @email 1047914020@qq.com
 * @date 2020年2月24日
 */
public interface Person1 {
	public String sayHello(String name);
	public String sayGoodBye(String name);

}

实现类

package com.jtwl.office.service.imp;

import com.jtwl.office.service.Person1;

/**
 * @todo
 * @author 成明俊
 * @email 1047914020@qq.com
 * @date 2020年2月24日
 */
public class Chinese1 implements Person1{
	public String sayHello(String name) {
		return name + ",你好!";
	}
	public String sayGoodBye(String name) {
		return name + ",下次再见!";
	}
	

}
package com.jtwl.office.service.imp;

import com.jtwl.office.service.Person1;

/**
 * @todo
 * @author 成明俊
 * @email 1047914020@qq.com
 * @date 2020年2月24日
 */
public class American implements Person1{
	public String sayHello(String name) {
		return name + ",Hello!";
	}
	public String sayGoodBye(String name) {
		return name + ",Good Bye!";
	}

}

实例工厂

package com.jtwl.office.factory;

import com.jtwl.office.service.Person1;
import com.jtwl.office.service.imp.American;
import com.jtwl.office.service.imp.Chinese1;

/**
 * @todo
 * @author 成明俊
 * @email 1047914020@qq.com
 * @date 2020年2月24日
 */
public class PersonFactory {
	public Person1 getPerson(String ethnic) {
		if(ethnic.equalsIgnoreCase("chin")) {
			return new Chinese1();
		}else {
			return new American();
		}
	}

}

配置文件

  <bean id="personFactory" class="com.jtwl.office.factory.PersonFactory" />
  <bean id="chinese1" factory-bean="personFactory" factory-method="getPerson">
  <constructor-arg value="chin"/>
  </bean>

两者区别与相似 

 

 

附件是本书的下载地址,格式为pdf 222M,有书签分类,清晰度高。 ========================= 本书是《轻量级Java EE企业应用实战》的第3版,第3版保持了第2版内容全面、深入的特点,主要完成全部知识的升级。 本书介绍了Java EE领域的三个开源框架:Struts 2、Spring和Hibernate。其中Struts 2升级到2.2.1,Spring升级到3.0.5,Hibernate升级到了3.6.0。本书还全面介绍了Servlet 3.0的新特性,以及Tomcat 7.0的配置和用法,本书的示例应该在Tomcat 7.0上运行。 本书重点介绍如何整合Struts 2.2+Spring 3.0+Hibernate 3.6进行Java EE开发,主要包括三部分,第一部介绍Java EE开发的基础知识,以及如何搭建开发环境。第二部分详细讲解Struts 2.2、Spring 3.0和Hibernate 3.6三个框架的用法,介绍三个框架时,从Eclipse IDE的使用来上手,一步步带领读者深入三个框架的核心。这部分内容是笔者讲授“疯狂Java实训”的培训讲义,因此是本书的重点部分,既包含了笔者多年开发经历的领悟,也融入了丰富的授课经验。第三部分示范开发了一个包含7个表、表之间具有复杂的关联映射、继承映射等关系,且业务也相对复杂的工作流案例,希望让读者理论联系实际,将三个框架真正运用到实际开发中去,该案例采用目前最流行、最规范的Java EE架构,整个应用分为领域对象层、DAO层、业务逻辑层、MVC层和视图层,各层之间分层清晰,层与层之间以松耦合的方法组织在一起。该案例既提供了IDE无关的、基于Ant管理的项目源码,也提供了基于Eclipse IDE的项目源码,最大限度地满足读者的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值