标签:
从事java开发工作已有1年多,期间学了很多,各项技术学的很快,但,忘得更快。如今工作、生活各项都相对稳定,想好好巩固下基础,在此申请了博客园,打算留下学习的足迹。留背将来复习或回味。
(一)声明一个简单bean
public class Juggler implements Performer{ private int beanBags = 3; public Juggler() { } public Juggler(int beanBags) { this.beanBags = beanBags; } @Override public void perform() { System.out.println("JUGGLER " + beanBags + " BEANSBAGS"); } }
(二)创建spring配置
beans.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <bean id = "duke" class = "org.springbaisc.Juggler" /> </beans>
spring-idol.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <bean id = "duke1" class = "org.springbaisc.Juggler" > <constructor-arg value="15"></constructor-arg><!-- 通过构造器注入 --> </bean> </beans>
测试代码
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("config/beans.xml","config/spring-idol.xml");//多配置文件 Performer performer = (Performer) ctx.getBean("duke"); Performer performer1 = (Performer) ctx.getBean("duke1"); performer.perform(); performer1.perform(); }
输入结果
JUGGLER 3 BEANSBAGS
JUGGLER 15 BEANSBAGS
通过构造器注入对象引用
public interface Poem { void recite(); }
public class Sonnet29 implements Poem{ private static String[] LINES = { "When,in disgrace with fortune and men‘s eyes", "I all alone beweep my outcast state" }; public Sonnet29(){ } @Override public void recite() { for(String str : LINES){ System.out.println(str); } } }
public class PoeticJuggler extends Juggler{ private Poem poem; public PoeticJuggler(Poem poem){ super(); this.poem = poem; } public PoeticJuggler(int beanBags,Poem poem){ super(beanBags); this.poem = poem; } public void perform(){ super.perform(); System.out.println("While reciting..."); poem.recite(); } }
配置文件
<bean id="sonnet29" class="org.springbaisc.Sonnet29"></bean> <bean id="poeticDuke" class="org.springbaisc.PoeticJuggler"> <constructor-arg value="15" /> <constructor-arg ref="sonnet29" /> </bean>
输出结果
JUGGLER 15 BEANSBAGS
While reciting...
When,in disgrace with fortune and men‘s eyes
I all alone beweep my outcast state
标签:
原文地址:http://www.cnblogs.com/vincentren/p/5638856.html