1.spring的helloworld
新建一个HelloWorld类,创建hello方法,输出hello world
public class HelloWorld {
public HelloWorld() {
System.out.println("============");
}
public void hello() {
System.out.println("hello World");
}
}
在src目录下创建spring配置文件-applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--
把一个类放入到spring容器中,该类就称为bean
-->
<!--
描述一个类
id 唯一标示
class 类名
-->
<bean id="helloWorld" class="com.my.spring.createobject.method.HelloWorld"></bean>
建立测试类
首先导入spring依赖的jar包,我用到的是spring2.5
public void textHelloWorld() {
//启动spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//根据id把spring容器中的bean取出来
HelloWorld helloWorld = (HelloWorld) applicationContext.getBean("helloWorld");
helloWorld.hello();
}
执行该方法,就能输出helloworld
2.利用静态工厂模式创建对象
applicationContext.xml
<!--
factory-method指的是静态工厂方法
-->
<bean id = "helloWorld2"
class="com.my.spring.createobject.method.factory.HelloWorldFactory"
factory-method="getInstance"></bean>
测试类:
/**
* 利用静态工厂模式创建对象
* <bean id="helloWorld2"
class="com.itheima12.spring.createobject.method.factory.HelloWorldFactory"
factory-method="getInstance"></bean>
spring容器内部调用了HelloWorldFactory中的getInstance方法创建对象
而具体的new对象的过程是由程序员来完成的。
*/
public void testCreateObject_StaticFactory(){
//启动spring容器
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
//根据id把spring容器中的bean提取出来了
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld2");
helloWorld.hello();
}
3.创建实例工厂方法.
applicationContext.xml
<bean id = "helloWorldFactory"
class = "com.my.spring.createobject.method.factory.HelloWorldFactory2"></bean>
<!--
factory-bean 是一个工厂bean spring容器会调用
factory-method是一个工厂方法
-->
<bean id = "helloWorld3" factory-bean="helloWorldFactory" factory-method="getInstance"></bean
测试类:
/**
* 实例工厂方法
*/
public void testCreateObject_InstaceFactory(){
//启动spring容器
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld3");
helloWorld.hello();
}
4.创建实例时运用别名
applictionContext.xml:
<!-- alias -->
<bean id = "helloWorld4" class="com.my.spring.alias.HelloWorld"></bean>
<!-- name的属性的值和bean的id对应 -->
<alias name="helloWorld4" alias="demo1"/>
<alias name="helloWorld4" alias="demo2"/>
测试类:
public void textAliasHelloWorld() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld) applicationContext.getBean("demo1");
helloWorld.hello();
}
6.创建实例时的单例和多例
在spring容器中的对象默认情况是单例的
<bean id = "helloWorld8" class="com.my.spring.createobject.scope.HelloWorld"></bean>
测试类:
public void textHelloWold() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld) applicationContext.getBean("helloWorld8");
HelloWorld helloWorld2 = (HelloWorld) applicationContext.getBean("helloWorld8");
HelloWorld helloWorld3 = (HelloWorld) applicationContext.getBean("helloWorld8");
}
测试后得出结论: 在spring容器中的对象默认情况是单例的
如何将单例变成多例?
因为对象是单例的,所以只要在类上声明一个属性,那么该属性将是全局的。对象就变成了多列。
applicastionContext.xml:
<bean id = "helloWorld9" class="com.my.spring.createobject.scope.HelloWorld" scope="prototype"></bean>
测试类:
public void textHelloWold_prototyoe() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld) applicationContext.getBean("helloWorld9");
HelloWorld helloWorld2 = (HelloWorld) applicationContext.getBean("helloWorld9");
System.out.println( helloWorld);
System.out.println(helloWorld2);
}
测试后发现,加上scope属性后产生的对象是多例。
7.对象创建的时间
<bean id = "helloWorld6" class="com.my.spring.createobject.time.HelloWorld "></bean>
helloWorld.java
public class HelloWorld {
public HelloWorld() {
System.out.println("===============");
}
public void hello() {
System.out.println("hello World");
}
}
测试类:
public void textHelloWorld() {
//启动spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//根据id把spring容器中的bean取出来
HelloWorld helloWorld = (HelloWorld) applicationContext.getBean("helloWorld6");
helloWorld.hello();
}
经过测试发现,默认情况下是在spring容器创建的时候创建对象。即在
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“applicationContext.xml”);时候进入类的构造函数。
这样,因为是在spring容器时候创建对象,所以只要配置文件错误,在web容器启动时候就能发现错误
<bean id = "helloWorld7" class="com.my.spring.createobject.time.HelloWorld " lazy-init="true"></bean>
配置bean时候加载lazy-init=”true”这一属性。
加上这一属性,容器创建对象时候是在applicationContext.getBean(“”)时候创建对象。
这种情况下,什么时候请求什么时候发现错误。