spring入门程序

配置文件:

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="sequenceGenerator" class="com.wfb.beans.SequenceGenerator" dependency-check="default">
   <property name="prefix" value="30"></property>
   <property name="suffix" value="A"></property>
   <property name="initial" value="1000"></property>
   <property name="cb" ref="childBean"></property>
   <constructor-arg type="java.lang.String" index="0" value="10000"></constructor-arg>
   <constructor-arg index="1" value="A"></constructor-arg>
   <constructor-arg index="2" value="1000"></constructor-arg>
</bean>

<bean id="childBean" class="com.wfb.beans.ChildBean">

</bean>
</beans>

类:

package com.wfb.beans;


public class SequenceGenerator {

private String prefix;
private String suffix;
private int initial;
private int counter;
private ChildBean cb;
public void setCb(ChildBean cb) {
   this.cb = cb;
}
public SequenceGenerator() {
}
public SequenceGenerator(String p,String s,int i){
   prefix=p;
   suffix=s;
   initial=i;
}
public void setPrefix(String prefix) {
   this.prefix = prefix;
}
public void setSuffix(String suffix) {
   this.suffix = suffix;
}
public void setInitial(int initial) {
   this.initial = initial;
}
public void setCounter(int counter) {
   this.counter = counter;
}
public synchronized String getSequence(){
   StringBuilder sb=new StringBuilder();
   sb.append(prefix);
   sb.append(initial+counter++);
   sb.append(suffix);
   return sb.toString();
}
}

package com.wfb.beans;

public class ChildBean {

}

package com.wfb.beans.test;

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

import com.wfb.beans.SequenceGenerator;

public class test {
public static void main(String[] args) {
   ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
   SequenceGenerator sg=(SequenceGenerator) context.getBean("sequenceGenerator");
   System.out.println(sg.getSequence());
   System.out.println(sg.getSequence());
}

}

原文地址:https://www.cnblogs.com/macula7/p/1960448.html