Spring 配置文件的一般结构如下:
•
<beans>
•
<import resource
=
“
resource1.xml
” />
•
<import
resource
=“resource2.xml” />
•
<bean id=“bean1” class=“
***
”></bean>
•
<bean name=
“
bean2” class=
“
***
”></bean>
•
<alias alias=
“
bean3
”
name=
“
bean2
”
/>
•
</beans>
其中import是导入其他配置。
以上实例代码为:
1、命名
首先是接口类:
public interface HelloWorld {
public void sayHello();
}
然后是实现类:
public class HelloWorldImpl implements HelloWorld {
@Override
public void sayHello() {
System.out.println("Hello World!");
}
}
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
<bean id="helloWorld" class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
<bean name="helloWorldByName" class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
<bean id="helloWorldById" name="helloWorldByName01"
class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
<bean name="bean1;alias11;alias12;alias13"
class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
<bean id="bean2" name="alias21,alias22,alias23"
class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
<bean name="bean3"
class="com.jike.spring.chapter04.definition.HelloWorldImpl" />
<alias alias="alias31" name="bean3" />
<alias alias="alias32" name="bean3" />
</beans>
2、实例化示例代码
首先是接口类:
public interface HelloWorld {
public void sayHello();
}
实现类:
public class HelloWorldImpl implements HelloWorld {
private String message;
/**
* 空构造器
*/
public HelloWorldImpl() {
this.message = "Hello World!";
}
/**
* 带参构造器
*
* @param message
*/
public HelloWorldImpl(String message) {
this.message = message;
}
public void sayHello() {
System.out.println(message);
}
}
构造器实例化不需要类。
下面是静态工厂实例化类:
public class HelloWorldStaticFactory {
/**
* 工厂方法
*
* @param message
* @return
*/
public static HelloWorld newInstance(String message) {
// 返回需要的Bean实例
return new HelloWorldImpl(message);
}
}
下面是实例工厂实例化类:
public class HelloWorldInstanceFactory {
/**
* 工厂方法
*
* @param message
* @return
*/
public HelloWorld newInstance(String message) {
// 返回需要的Bean实例
return new HelloWorldImpl(message);
}
}
最后是实例化的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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--使用默认构造参数-->
<bean id="helloWorldWithNoArgs"
class="com.jike.spring.chapter04.instance.HelloWorldImpl" />
<!--使用有参数构造参数-->
<bean id="helloWorldWithArgs"
class="com.jike.spring.chapter04.instance.HelloWorldImpl" >
<!-- 指定构造器参数 -->
<constructor-arg index="0" value="Hello Args!"/>
</bean>
<!--静态工厂方式-->
<bean id="helloWorldStaticFactory"
class="com.jike.spring.chapter04.instance.HelloWorldStaticFactory"
factory-method="newInstance" >
<!-- 指定构造器参数 -->
<constructor-arg index="0" value="Hello Static Factory!"/>
</bean>
<!-- 1、定义实例工厂Bean -->
<bean id="helloWorldInstanceFactory"
class="com.jike.spring.chapter04.instance.HelloWorldInstanceFactory" />
<!-- 2、使用实例工厂Bean创建Bean -->
<bean id="helloWorldInstance" factory-bean="helloWorldInstanceFactory" factory-method="newInstance">
<constructor-arg index="0" value="Hello Instance Factory!"></constructor-arg>
</bean>
</beans>
最后是测试代码:
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
helloWorldInstanceFactory();
}
//使用无参数构造器来实例化Bean
public static void sayHelloWithNoArgs() {
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-instance.xml");
HelloWorld helloWorld = beanFactory.getBean("helloWorldWithNoArgs", HelloWorld.class);
helloWorld.sayHello();
}
//使用有参数构造器来实例化Bean
public static void sayHelloWithArgs() {
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-instance.xml");
HelloWorld helloWorld = beanFactory.getBean("helloWorldWithArgs", HelloWorld.class);
helloWorld.sayHello();
}
//使用静态工厂方法来实例化Bean
public static void helloWorldStaticFactory() {
// 1、读取配置文件实例化一个IOC容器
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-instance.xml");
// 2、从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现”
HelloWorld helloWorld
= beanFactory.getBean("helloWorldStaticFactory", HelloWorld.class);
// 3、执行业务逻辑
helloWorld.sayHello();
}
//使用实例化工厂方法来实例化Bean
public static void helloWorldInstanceFactory() {
// 1、读取配置文件实例化一个IOC容器
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-instance.xml");
// 2、从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现编程”
HelloWorld helloWorld
= beanFactory.getBean("helloWorldInstance", HelloWorld.class);
// 3、执行业务逻辑
helloWorld.sayHello();
}
}