一、 项目介绍
1、IoC容器装配Bean_基于XML配置方式–实例化Bean的四种方式
2、项目开发工具:
(1) jdk1.8.0_92
(2) IDEA-2019.3.4
(3) apache-maven-3.5.2
3、项目目录结构

二、源码
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.suogu</groupId>
<artifactId>spring_day1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- 日志依赖-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
</dependencies>
</project>
2、resources中的配置文件
(1) log4j.properties(日志相关配置信息)
log4j.rootLogger=DEBUG,A1
log4j.logger.org.mybatis=DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
(2) beans.xml(Spring实体bean配置文件)
<?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">
<!-- 告诉spring创建对像
声明bean就是告诉spring要创建个类的对象
id:对象自定义名称,唯一值,spring通过这个名称找到对象。
class:类的全限定名称(不能是接口,因为spring是反射机制创建对象,必须使用类的全类名)。
spring会在底层完成:SomeService someService = new SomeService();
spring是把创建好的对象放到map集合中,spring框架有一个map存放对象的。
springMap.put(id的值,对象);
例如:springMap.put("someService",new SomeServiceImpl());
一个bean标签声明一个对象。
-->
<!--
spring默认创建对象的时间是:在创建spring的容器的时候,会创建配置文件中的所有的对象
spring创建对象:默认调用的是无参构造方法
-->
<bean id="someService" class="com.suogu.service.impl.SomeServiceImpl"/>
<!--spring能创建一个非自定义类的对象,即创建一个存在的,不是自己定义的某个类的对象-->
<bean id="mydate" class="java.util.Date"/>
<bean id="someDao" class="com.suogu.dao.impl.SomeDaoImpl"/>
<!-- 第一种,通过无参构造方法创建SomeServiceImpl,并且注入dao实现依赖-->
<bean id="someService1" class="com.suogu.service.impl.SomeServiceImpl">
<property name="someDao" ref="someDao"/>
</bean>
<!-- 第二种:通过静态工厂方式来实现-->
<bean id="someService2" class="com.suogu.service.impl.SomeServiceImplStaticFactory" factory-method="intisomeServiceImpl" />
<!--第三种:实例工厂的方式-->
<bean id="someServiceImplFactory" class="com.suogu.service.impl.SomeServiceImplFactory"/>
<bean id="someService3" factory-bean="someServiceImplFactory" factory-method="initBean"/>
<!--第四种:工厂bean-->
<bean id="someService4" class="com.suogu.service.impl.SomeServiceImplFactoryBean"/>
</beans>
3、service层
SomeService.java
package com.suogu.service;
public interface SomeService {
void doSome();
}
4、service实现层
SomeServiceImpl.java
package com.suogu.service.impl;
import com.suogu.dao.SomeDao;
import com.suogu.service.SomeService;
public class SomeServiceImpl implements SomeService {
SomeDao someDao = null;
public SomeDao getSomeDao() {
return someDao;
}
public void setSomeDao(SomeDao someDao) {
this.someDao = someDao;
}
@Override
public void doSome() {
System.out.println("实现了接口中的login方法");
//someDao.login();
}
}
SomeServiceImplFactory.java
package com.suogu.service.impl;
public class SomeServiceImplFactory {
//实例工厂的方式
public SomeServiceImpl initBean(){
return new SomeServiceImpl();
}
}
SomeServiceImplFactoryBean.java
package com.suogu.service.impl;
import org.springframework.beans.factory.FactoryBean;
public class SomeServiceImplFactoryBean implements FactoryBean<SomeServiceImpl> {
//工厂bean的方式
//泛型:传递要实例化对象的类型
@Override
//返回bean对象的方法,该方法spring会自动调用
public SomeServiceImpl getObject() throws Exception {
//初始化的对象
return new SomeServiceImpl();
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return false;
}
}
SomeServiceImplStaticFactory.java
package com.suogu.service.impl;
public class SomeServiceImplStaticFactory {
//静态工厂方式
public static SomeServiceImpl intisomeServiceImpl(){
return new SomeServiceImpl();
}
}
5、dao层(此中只写了简单输出语句,没与数据库交互)
SomeDao.java
package com.suogu.dao;
public interface SomeDao {
public void login();
}
6、dao实现层
SomeDaoImpl.java
package com.suogu.dao.impl;
import com.suogu.dao.SomeDao;
public class SomeDaoImpl implements SomeDao {
@Override
public void login() {
System.out.println("这是SomeDaoImpl中的方法,登录成功");
}
}
7、MyTest文件
MyTest.java
import com.suogu.service.SomeService;
import com.suogu.service.impl.SomeServiceImpl;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public ApplicationContext ac;
@Before
public void init(){
// 使用spring容器创建的对象
// 1.指定spring配置文件的名称
String config = "beans.xml";
//2.创建表示Spring容器的对象,ApplicationContext
// ApplicationContext就是表示Spring容器,通过容器获取对象了
// ClassPathXmlApplicationContext :表示从类路径中加载Spring的配置文件
//注意:后期这种方式不会再使用,会使用更加简单的方式
ac = new ClassPathXmlApplicationContext(config);;
}
//实例化Bean的四种方式
//第一种 无参数构造器
@Test
public void test01() {
System.out.println("第一种 无参数构造器");
//从容器中获取某个对象,你要 调用对象的方法
// getBean("配置文件中的bean的id值")
SomeService service = (SomeService) ac.getBean("someService1");
//使用Spring创建好的对象
service.doSome();
}
//第二种 静态工厂方法的方式
@Test
public void test02(){
System.out.println("第二种 静态工厂方法的方式");
SomeService service = (SomeService) ac.getBean("someService2");
//使用Spring创建好的对象
service.doSome();
}
//第三种 静态工厂方法的方式
@Test
public void test03(){
System.out.println("第三种 静态工厂方法的方式");
SomeService service = (SomeService) ac.getBean("someService3");
//使用Spring创建好的对象
service.doSome();
}
//第四种 静态工厂方法的方式
@Test
public void test04(){
SomeService service = (SomeService) ac.getBean("someService4");
//使用Spring创建好的对象
System.out.println("第四种工厂bean的方式");
service.doSome();
}
@Test
public void test05(){
String config = "beans.xml";
//创建spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
SomeService service = (SomeService) ac.getBean("someService");
//使用spring提供的方法,获取容器中定义的对像的数量
int nums = ac.getBeanDefinitionCount();
System.out.println("容器中定义的对象数量"+nums);
//获取每个定义的对象名称
String names[] = ac.getBeanDefinitionNames();
for(String name : names){
System.out.println(name);
}
}
}
5、程序运行结果截图
第一种 无参数构造器:

第二种 静态工厂方法的方式:

第三种 静态工厂方法的方式

第四种 静态工厂方法的方式

源码,以及导入项目到自己的idea中,可以地下评论留下QQ号,看到后会及时回复,也可以加群交流,谢谢

本文详细介绍了如何使用Spring IoC容器通过XML配置来实例化Bean,包括无参构造器、静态工厂方法、实例工厂方法以及工厂Bean的四种方式,并给出了对应的代码示例和测试用例。通过这些方式,可以理解Spring如何管理和组装Bean。

185

被折叠的 条评论
为什么被折叠?



