<beans> <alias name="person" alias="p"/>//alias这里是别名,可以通过p,得到person这个bean. <bean name="person" class="cn.itcast.aliasspring.Person"/> </beans>
<?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 --> <!-- bean描述了一个类 id 唯一的标示 class 类的全名 --> <bean id="helloWorld" class="com.itheima10.spring.createobject.HelloWorld"></bean> <!-----------------------别名--------------------------------------> <!-- name的属性和bean的id匹配 alias 别名 --> <alias name="helloWorld" alias="狗蛋"/> <alias name="helloWorld" alias="习近平"/> <!-----------------------静态工厂方法--------------------------------------> <!-- 把helloWorldFactory放入到spring容器中 factory-method 工厂方法 --> <bean id="helloWorld2" factory-method="getInstance" class="com.itheima10.spring.createobject.method.HelloWorldFactory"></bean> <!-----------------------实例工厂方法--------------------------------------> <!-- 把helloWorldFactory2放入到spring容器中 factory-bean 指明了工厂bean factory-method 指明了该工厂bean中的方法 --> <bean id="helloWorldFactory" class="com.itheima10.spring.createobject.method.HelloWorldFactory2"></bean> <bean id="helloWorld3" factory-bean="helloWorldFactory" factory-method="getInstance"></bean> </beans>
package com.itheima10.spring.createobject;
public class HelloWorld {
public void hello(){
System.out.println("hello");
}
}
package com.itheima10.spring.createobject.method;
public class HelloWorldFactory {
public static HelloWorld getInstance(){
System.out.println("static method");
return new HelloWorld();
}
}
package com.itheima10.spring.createobject.method;
public class HelloWorldFactory2 {
/**
* 必须先创建工厂对象,才能调用该方法
* @return
*/
public HelloWorld getInstance(){
return new HelloWorld();
}
}
package com.itheima10.spring.createobject.method;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 第二种和第三种产生方式能看明白就OK了
* @author zd
*
*/
public class CreateObjectMethodTest {
/**
* 在默认情况下,spring容器调用的是一个类的默认的构造函数创建对象
*/
@Test
public void testCreateObject_Default(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
helloWorld.hello();
}
/**
* 利用静态工厂创建对象
* <bean id="helloWorld2"
factory-method="getInstance"
class="com.itheima10.spring.createobject.method.HelloWorldFactory"></bean>
spring容器做的事情:
利用HelloWorldFactory类调用了getInstance方法
*/
@Test
public void testCreateObject_StaticFactory(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld2");
helloWorld.hello();
}
/**
* 实例工厂方法创建对象
* <bean id="helloWorldFactory"
* class="com.itheima10.spring.createobject.method.HelloWorldFactory2"></bean>
* <bean id="helloWorld3"
factory-bean="helloWorldFactory"
factory-method="getInstance"></bean>
spring容器内部做的事情:
1、创建一个helloWorldFactory对象
2、由该对象调用getInstance产生helloWorld对象
*/
@Test
public void testCreateObject_InstanceFactory(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld3");
helloWorld.hello();
}
}
public class HelloWorld {
public List<String> lists = new ArrayList<String>();
public HelloWorld(){
System.out.println("new instance");
}
public void hello(){
System.out.println("hello");
}
}
package com.itheima10.spring.scope;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScopeTest {
/**
* 把一个bean放入到spring容器中,默认的是单例
* 如果一个类放入到spring容易中,而这个类是单例的,那么该类中的属性将会成为共享的
*/
@Test
public void testCreateObject_Scope_Default(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hello1 = (HelloWorld)context.getBean("helloWorld");
hello1.lists.add("aaaa");
hello2= (HelloWorld)context.getBean("helloWorld");
hello2.lists.add("bbbb");
System.out.println(helloWorld.lists.size());//2,并且只输出一次new instance
}
/**
* 如果spring的配置文件如下:
* <bean id="helloWorld"
class="com.itheima10.spring.scope.HelloWorld"
scope="prototype"></bean>
那么spring容器会为创建多个对象
*/
@Test
public void testCreateObject_Scope_Prototype(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hello1 = (HelloWorld)context.getBean("helloWorld");
hello1.lists.add("aaaa");
hello2= (HelloWorld)context.getBean("helloWorld");
hello2.lists.add("bbbb");
System.out.println(helloWorld.lists.size());//1,并且只输出两次new instance
}
}
<bean id="helloWorld" class="com.itheima10.spring.createobject.when.HelloWorld" scope="prototype"></bean> </beans>
package com.itheima10.spring.createobject.when;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CreateObjectWhenTest {
/**
* 默认情况下的顺序
* 1、启动spring容器
* 2、创建HelloWorld对象
* 3、对象调用方法
*
* <bean id="helloWorld" class="com.itheima10.spring.createobject.when.HelloWorld"></bean>
<bean id="helloWorld2" class="com.itheima10.spring.createobject.when.HelloWorld"></bean>
因为在spring容器中声明了两个bean,所以spring容器要创建两个对象
说明:
如果struts2,hibernate,spring容器整合,如果spring的配置文件中出现错误
当 tomcat容器启动的时候,就会报错,错误会特别早的显示出来
如果一个bean存放了大量的数据,这种方式不好,有可能会把数据过早的停留在内存中
如果一个bean不是单例,那么不管怎么样配置,都在是context.getBean时才要创建对象
*/
@Test
public void testCreateObject_When_Default(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
context.getBean("helloWorld");
}
/**
* <bean id="helloWorld"
class="com.itheima10.spring.createobject.when.HelloWorld"
lazy-init="true"></bean>
* 顺序
* 1、启动spring容器
* 2、context.getBean
* 3、调用构造器函数创建对象
* 说明:
如果struts2,hibernate,spring容器整合,如果spring的配置文件中出现错误
只有当用到该bean的时候才会报错。
如果一个bean存放了大量的数据,需要的时候才要加载数据
*/
@Test
public void testCreateObject_When_Lazy(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
context.getBean("helloWorld");
}
}
public class HelloWorld {
public HelloWorld(){
System.out.println("new instance");
}
public void init(){
System.out.println("init");
}
public void destroy(){
System.out.println("destroy");
}
public void hello(){
System.out.println("hello");
}
}
package com.itheima10.spring.ioc.initdestroy;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InitDestroyTest {
/**
* init-method="init"
* 执行顺序:
* 1、启动spring容器
* 2、创建helloWorld对象
* 3、执行init方法
* spring容器内部自动执行的
* 4、对象调用方法
* 5、只有当spring容器关闭掉的情况下才能执行destroy方法 前提条件:bean是单例的
* 该方法也是由spring容器内部调用的
* 说明:
* 如果一个bean不是单例的,则spring容器不负责对象的销毁。
* 在spring容器中,只有一个bean是单例的情况下,spring容器才要负责对象的创建、初始化、销毁工作
* 如果一个bean不是单例,spring容器只负责创建、初始化
*/
@Test
public void testInitDestroy(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
helloWorld.hello();
ClassPathXmlApplicationContext applicationContext = (ClassPathXmlApplicationContext)context;
applicationContext.close();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有