spring 第3天使用java类和XML配置bean
2017-03-27 11:35
316 查看
下面采用java类来配置bean,前面都采用xml进行配置bean
采用xml
采用java类来配置bean
使用静态工厂方法创建bean
采用实例工厂方法创建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>
相关文章推荐
- 译--Spring中 bean定义profiles的使用 javaConfig 和XML配置
- spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
- Spring中使用xml配置bean原理
- Spring中使用xml配置bean
- 详解Spring中使用xml配置bean的细节
- Spring-Context之三:使用XML和Groovy DSL配置Bean
- Spring中使用 PropertyPlaceholderConfigurer 替换xml配置文件中的Bean配置项
- Spring学习笔记 使用XML配置实现Bean的auto-wiring (自动绑定)
- Spring装配Bean---使用xml配置
- Spring(18)——使用Java类的形式配置bean(三)
- Springboot 中同时使用mybatis注解和springbean-xml配置方式
- Spring学习(1)--使用xml配置Bean属性
- SpringBoot入门系列篇(十二):使用XML配置Bean
- Spring根据XML配置文件注入属性 其实也是造bean,看看是使用constructor还是setter顺带完成属性赋值
- Spring(18)——使用Java类的形式配置bean(二)
- Spring(18)——使用Java类的形式配置bean
- Spring中使用xml配置bean的细节
- Spring使用xml配置bean,主要是constructor-arg
- spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
- 简化Spring的XML配置(二)——使用注解装配bean