基于maven项目的 pom.xml配置文件
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.1.RELEASE</version>
<scope>test</scope>
</dependency>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.bean.Person" />
<bean id="person1" class="com.bean.Person">
<constructor-arg name="name" type="String" value="aa" /> <!-- 构造器参数设置 -->
</bean>
</beans>
Person实体类
package com.bean;
public class Person {
private String name;
public Person() {
System.out.println("Person.....");
}
public Person(String name) {
this.name = name;
System.out.println("Person...." + name);
}
}
测试Bean获取方式类
package com.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.bean.Person;
/**
* IoC(Inversion of Control):控制反转,简单的说就是让Spring帮我们创建对象。
*
* 测试Bean获取方式:
* 1、getBean(String name):通过bean名称来获取,得到是一个Object对象,在使用时需要进行强转
* 条件是名称必须存在,否则会报错。
* 2、getBean(Class<T> requiredType):根据指定类的字节码文件在配置文件中去查找,
* 如果找到就实例化对象,如果找不到就报错。如果找到多个,也会报错。
* 3、getBean(String name, Class<T> requiredType):通过名称和类的字节码来获取,
* 此方法是推荐使用的方法。
*/
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:beans.xml")//配置文件在src/main/resource下
public class SpringTest01 {
@Autowired
private BeanFactory factory;
//测试 getBean(String name);方法,找配置文件中的id
@Test
public void testGetBeanByName() {
Object obj = factory.getBean("person");
System.out.println(obj);
}
//测试 getBean(Class<T> requiredType)方法,找配置文件中的class
@Test
public void testGetBeanByType() {
Person person = factory.getBean(Person.class);
System.out.println(person);
}
//测试 getBean(String name, Class<T> requiredType)方法
@Test
public void test() {
Person person = factory.getBean("person", Person.class);
System.out.println(person);
}
@Test
public void test2() {
Object bean = factory.getBean("person1", "张三");
System.out.println(bean);
}
}
显示结果都是
Person.....
Person....aa
com.bean.Person@3c153a1
几种实体类
package com.bean;
import org.springframework.beans.factory.FactoryBean;
public class Man implements FactoryBean<Man> {
@Override
public Man getObject() throws Exception {
System.out.println("Man.....getObject().....");
return new Man();
}
@Override
public Class<?> getObjectType() {
return Man.class;
}
}
public class Women {
public static Women instance() {
System.out.println("Women......instance().....");
return new Women();
}
public Women build() {
System.out.println("Women......build().....");
return new Women();
}
}
public class People {
public People() {
System.out.println("People.....");
}
public void show() {
System.out.println("show().....");
}
}
spring配置文件
SpringTest02-context.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.xsd">
<!-- 构造方法实例化 -->
<bean id="people" class="com.bean.People" />
<!-- 静态工厂方法实例化 -->
<bean id="women" class="com.bean.Women" factory-method="instance" />
<!-- 实例化工厂方法 -->
<bean id="women1" class="com.bean.Women" />
<bean factory-bean="women1" factory-method="build" />
<!-- 实例化工厂方法变化方式,需要实现FactoryBean接口 -->
<bean id="man" class="com.bean.Man" />
</beans>
Spring实例化对象的几种方式的测试
package com.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.bean.Man;
import com.bean.People;
import com.bean.Women;
/**
* Spring实例化对象的几种方式:
* 1、使用构造器来创建对象
* 需要在配置文件中做如下配置:
* <bean id="people" class="com.bean.People" />
*
* 2、静态工厂方法实例化
* 首先编写一个静态工厂方法:
* public class Women {
public static Women instance() {
System.out.println("Women......instance().....");
return new Women();
}
}
* 然后在配置文件中进行如下配置:
* <bean id="women" class="com.bean.Women" factory-method="instance" />
*
* 3、实例化(非静态)工厂方法
* 首先编写一个非静态方法:
* public class Women {
* public Women build() {
* System.out.println("Women......build().....");
* return new Women();
* }
* }
* 然后在配置文件中作发如下配置:
* <bean id="women1" class="com.bean.Women" />
* <bean factory-bean="women1" factory-method="build" />
*/
@RunWith(SpringRunner.class)
@ContextConfiguration
// 如果在此注解中没有指定名称,则默认配置文件的名称为测试类的名称-context.xml,并且在同级目录下。
public class SpringTest02 {
//@Autowired
//private People people;
@Autowired
//private Women women;
private BeanFactory factory;
@Test
public void test01() {
People people = factory.getBean("people", People.class);
people.show();
}
@Test
public void test02() {
factory.getBean("women", Women.class);
}
@Test
public void test03() {
//new Women().build();
factory.getBean("women1", Women.class);
}
@Test
public void test04() {
factory.getBean("man", Man.class);
}
}
其结果是:
People.....
Women......instance().....
Women......build().....
show().....
配置文件
SpringTest03 -context.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.xsd">
<!-- Bean的作用域 -->
<bean id="people" class="com.bean.People" scope="singleton" />
</beans>
Bean的作用域的测试类
package com.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.bean.People;
/**
* Bean的作用域:
* 需要在配置文件中的bean标签中指定scope属性:
* 1、singleton:单例,也就是无论创建多少个对象都是同一个对象,默认值。
* 2、prototype:多例,每创建一个对象都是新的对象。
* 3、request:请求域有效,一般用web中,一般不使用。
* 4、session:一个会话有效,一般不使用。
* 5、application:整个项目有效,一般不使用。
*
* 只需要关注singleton和prototype就可以了,绝大部分都使用singleton,只有
* 控制层使用prototype。
*/
@RunWith(SpringRunner.class)
@ContextConfiguration
public class SpringTest03 {
@Autowired
private BeanFactory factory;
@Test
public void test01() {
People people = factory.getBean("people", People.class);
People people1 = factory.getBean("people", People.class);
System.out.println(people);
System.out.println(people1);
System.out.println(people == people1);
}
}
其结果如下:
People.....
com.bean.People@548a102f
com.bean.People@548a102f
true