spring 第3天使用java类和XML配置bean

下面采用java类来配置bean,前面都采用xml进行配置bean

package cn.sh.springmvc.model;

import cn.sh.springmvc.model.interfaces.Axe;
import cn.sh.springmvc.model.interfaces.Person;

public class Chinese implements Person {
	private Axe axe;
	private String name;
	public Chinese() {
		System.out.println("Spring 实例化");
	}
	@Override
	public void useAxe() {
		System.out.println(axe.chop());
	}
	public void setAxe(Axe axe) {
		this.axe = axe;
	}
	public Axe getAxe() {
		return axe;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

采用xml
<bean id="stoneAxe" class="cn.sh.springmvc.model.StoneAxe">
		<property name="name" value="没有打磨的"/>
	</bean>
	<bean id="steelAxe" class="cn.sh.springmvc.model.SteelAxe"/>
	<!-- 第二阶段 -->
	<bean id="chinese" class="cn.sh.springmvc.model.Chinese">
<property name="axe" ref="stoneAxe"/>
		<property name="name" ref="刘诗诗"/>
</bean>

采用java类来配置bean


package cn.sh.springmvc_java;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.ImportResource;

import cn.sh.springmvc.model.SteelAxe;
import cn.sh.springmvc.model.StoneAxe;
import cn.sh.springmvc.model.interfaces.Axe;
import cn.sh.springmvc.model.interfaces.Person;

/**
 * 使用一个特殊的 配置类.来代替XML的配置
 * @author Bin
 */

/*
 * @Configuration  用于修饰一个Java类 为配置类
 * @Bean:修饰一个方法,将该方法的返回值,定义成容器中的一个bean;
 * @Value:修饰一个filed,用于给该字段 赋值
 * @import:修饰一个Java配置类,用于向当前配置类中导入其他 Java配置类
 * @Scope:修饰一个方法,定义该方法返回的Bean的生命域
 * @Lazy:修饰一个方法,指定该方法返回的对应Bean是否需要延迟初始化
 * @DepondOn:修饰一个方法,指定在初始化对应方法返回Bean之前,初始化指定Bean
 * 
 */
@Configuration //用于修饰一个Java配置类
@ImportResource("classpath*:/applicationContent.xml") //可以看出来采用 XML 和 Java配置类混合,并一 Java配置类为"主"
public class AppConfig {
	
	@Value("孙悟空")
	String personName;
	
	public Person person(){
		Chinese p=new Chinese();
		p.setAxe(stoneAxe());
		p.setName(personName);
		return p;
	}
	
	@Bean(name="stoneAxe")
	@DependsOn("steelAxe")
	public Axe stoneAxe(){
		return new StoneAxe();
	}
	
	@Bean(name="steelAxe")
	public Axe steelAxe(){
		System.out.println("create SteelAxe");
		return new SteelAxe();
	}
	
}

	@Test
	public void test11(){ //采用Java配置类,来管理 依赖关系, 同时可以采用 XML 和 Java配置类混合
		ApplicationContext act=new AnnotationConfigApplicationContext(AppConfig.class);
		StoneAxe saxe=act.getBean("stoneAxe",StoneAxe.class);
		System.out.println(saxe.chop());
		Chinese c=act.getBean("chinese",Chinese.class);
		c.test();
	}


使用静态工厂方法创建bean

package cn.sh.springmvc_java.factory;

import cn.sh.springmvc.model.SteelAxe;
import cn.sh.springmvc.model.StoneAxe;
import cn.sh.springmvc.model.interfaces.Axe;

/**
 * 使用静态工厂类,创建 Bean
 *
 */
public class AxeFactory {

	public static Axe getAxe(String arg){
		if(arg.equals("stone")){
			return new StoneAxe();
		}else if(arg.equals("steel")){
			return new SteelAxe();
		}
		return null;
	}
}

 <!-- 使用静态工厂staticFactory 创建Bean
      	1:不需要为工厂类配置bean
      	2:可以直接使用 class来指定 工厂类
       -->
      <bean id="stoneAxe_sf" class="cn.sh.springmvc_java.factory.AxeFactory" factory-method="getAxe" >
      		<constructor-arg value="stone"/>
      		<property name="name" value="小石头"/>
      </bean>
      
      <bean id="steelAxe_sf" class="cn.sh.springmvc_java.factory.AxeFactory" factory-method="getAxe" >
      		<constructor-arg value="steel"/>
      		<property name="name" value="不锈钢"/>
      </bean>


采用实例工厂方法创建bean

package cn.sh.springmvc_java.factory;

import cn.sh.springmvc_java.American;
import cn.sh.springmvc_java.China;
import cn.sh.springmvc_java.People;

/**
 * 使用实例工厂创建bean
 *
 */
public class PeopleFactory {

	public People getPeople(String type){
		if(type.equalsIgnoreCase("chin")){
			return new China();
		}else{
			return new American();
		}
	}
}

 <!-- 使用 实例工厂 创建Bean
      	1:必须将工厂配置成一个Bean,因为 工厂需要实例化
      	2:必须使用 factory-bean 指定 工厂bean
       -->
      <bean id="peopleFactory" class="cn.sh.springmvc_java.factory.PeopleFactory"/>
      <bean id="china" factory-bean="peopleFactory" factory-method="getPeople">
      	<constructor-arg value="chin"/>
      </bean>
      <bean id="american" factory-bean="peopleFactory" factory-method="getPeople" scope="prototype" init-method="init" destroy-method="close">
      	<constructor-arg value="ame"/>
      </bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值