Spring中的Bean
程序员文章站
2022-03-03 11:50:15
...
项目准备
Spring常用的一些功能,为了方便使用,特意记录如下,不讲原理,只记录怎么使用
- 使用IntelliJ IDEA编辑器
- 使用Maven创建项目
- 准备pom.xml文件,里面放了几个常用的依赖
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qunar</groupId>
<artifactId>example1</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>example1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--Spring 上下文,核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!--Spring bean-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<!--Spring core-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.3.14.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Bean的实例化
构造器实例化
在resources目录下放入beans1.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="bean1" class="com.qunar.instance.constructor.Bean1"/>
</beans>
放入bean
package com.qunar.instance.constructor;
/**
* 构造器实例化的类
*
* @author
* @create 2018-03-25 19:50
**/
public class Bean1 {
}
放入测试类InstanceTest1
package com.qunar.instance.constructor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 构造器实例化的测试类
*
* @author
* @create 2018-03-25 19:50
**/
public class InstanceTest1 {
public static void main(String[] args) {
// 获取配置路径
String xmlPath = "classpath:beans1.xml";
// 获取配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
// 获取指定bean
Bean1 bean1 = (Bean1)applicationContext.getBean("bean1");
// 输出
System.out.println(bean1);
}
}
静态工厂方式实例化
首先创建一个bean2
package com.qunar.instance.static_factory;
/**
* 静态工厂方式使用的Bean
*
* @author
* @create 2018-03-25 20:04
**/
public class Bean2 {
}
然后创建bean2的工厂
package com.qunar.instance.static_factory;
/**
* 创建一个工厂创建Bean2实例
*
* @author
* @create 2018-03-25 20:02
**/
public class Mybean2Factory {
public static Bean2 createBean()
{
return new Bean2();
}
}
创建bean的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">
<!--创建一个工厂创建Bean2实例-->
<bean id="bean2" class="com.qunar.instance.static_factory.Mybean2Factory" factory-method="createBean"/>
</beans>
最后创建测试类InstanceTest2
package com.qunar.instance.static_factory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 创建一个工厂创建Bean2实例的测试类
*
* @author
* @create 2018-03-25 20:06
**/
public class InstanceTest2 {
public static void main(String[] args) {
// 创建配置路径
String xmlPath = "classpath:beans2.xml";
// 获取配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
// 打印类
System.out.println(applicationContext.getBean("bean2"));
}
}
实例工厂方式实例化
bean类
package com.qunar.instance.factory;
/**
* 实例工厂方式实例化的类
*
* @author
* @create 2018-03-25 20:13
**/
public class Bean3 {
}
工厂类
package com.qunar.instance.factory;
/**
* 实例工厂方式实例化的类
*
* @author
* @create 2018-03-25 20:11
**/
public class MyBean3Factory {
public MyBean3Factory() {
System.out.println("bean3正在实例化");
}
// 创建Bean3实例的方法
public Bean3 createBean()
{
return new Bean3();
}
}
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">
<!--配置工厂-->
<bean id="myBean3Factory" class="com.qunar.instance.factory.MyBean3Factory"/>
<!--使用factory-bean属性指向配置的实例工厂-->
<!--使用factory-method属性确定使用工厂中的哪个方法-->
<bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean"/>
</beans>
测试类
package com.qunar.instance.factory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 实例工厂方式实例化的测试类
*
* @author
* @create 2018-03-25 20:18
**/
public class InstanceTest3 {
public static void main(String[] args) {
String xmlPath="classpath:beans3.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
System.out.println(applicationContext.getBean("bean3"));
}
}