Spring之创建bean(xml配置)
程序员文章站
2022-05-24 16:12:08
...
1.通过无参构造创建
Bean1.java
package zh.spring.entity;
public class Bean1 {
// 需要无参构造方法
public Bean1(){
}
public void fun() {
System.out.println("bean1...");
}
}
在src下创建bean1.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">
<!-- scope取值:singleton(默认)、prototype、request、session、globalSession -->
<bean id="bean1" class="zh.spring.entity.Bean1" scope="singleton"></bean>
</beans>
Bean1Test.java
package zh.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zh.spring.entity.Bean1;
public class Bean1Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
Bean1 bean11 = (Bean1) applicationContext.getBean("bean1");
Bean1 bean12 = (Bean1) applicationContext.getBean("bean1");
// scope="singleton",所以创建的对象是单例的
System.out.println(bean11);// [email protected]
System.out.println(bean12);// [email protected]
bean11.fun();// bean1...
bean12.fun();// bean1...
}
}
2.通过静态工厂创建
Bean1.java
package zh.spring.entity;
public class Bean1 {
public Bean1(){
}
public void fun() {
System.out.println("bean1...");
}
}
MyFactory1.java
package zh.spring.entity;
/**
* 静态工厂
* @author ZH
*/
public class MyFactory1 {
public static Bean1 getBean1(){
return new Bean1();
}
}
在src下创建bean1.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">
<!-- scope取值:singleton(默认)、prototype、request、session、globalSession -->
<bean id="bean1" class="zh.spring.entity.MyFactory1" factory-method="getBean1" scope="prototype"></bean>
</beans>
Bean1Test.java
package zh.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zh.spring.entity.Bean1;
public class Bean1Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
Bean1 bean11 = (Bean1) applicationContext.getBean("bean1");
Bean1 bean12 = (Bean1) applicationContext.getBean("bean1");
// scope="prototype",所以创建的对象是多例的
System.out.println(bean11);// [email protected]
System.out.println(bean12);// [email protected]
bean11.fun();// bean1...
bean12.fun();// bean1...
}
}
3.通过实例工厂创建
Bean1.xml
package zh.spring.entity;
public class Bean1 {
public Bean1(){
}
public void fun() {
System.out.println("bean1...");
}
}
MyFactory2.java
package zh.spring.entity;
/**
* 实例工厂
* @author ZH
*/
public class MyFactory2 {
public Bean1 getBean1(){
return new Bean1();
}
}
在src下创建bean1.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">
<!-- scope取值:singleton(默认)、prototype、request、session、globalSession -->
<!-- 首先创建实例工厂 -->
<bean id="myFactory2" class="zh.spring.entity.MyFactory2"></bean>
<!-- factory-bean的值写上面实例工厂的id值 -->
<bean id="bean1" factory-bean="myFactory2" factory-method="getBean1" scope="prototype"></bean>
</beans>
Bean1Test.java
package zh.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zh.spring.entity.Bean1;
public class Bean1Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
Bean1 bean11 = (Bean1) applicationContext.getBean("bean1");
Bean1 bean12 = (Bean1) applicationContext.getBean("bean1");
// scope="prototype",所以创建的对象是多例的
System.out.println(bean11);// [email protected]
System.out.println(bean12);// [email protected]
bean11.fun();// bean1...
bean12.fun();// bean1...
}
}
上一篇: 批量修改文件创建修改时间
下一篇: Python修改文件的“修改时间”
推荐阅读
-
spring Mvc配置xml使ResponseBody返回Json的方法示例
-
spring如何使用命名空间p简化bean的配置
-
JSP 开发之Spring Boot 动态创建Bean
-
spring5 源码深度解析----- 被面试官给虐懵了,竟然是因为我不懂@Configuration配置类及@Bean的原理
-
Spring学习之Bean的装配多种方法
-
Spring基于xml文件配置Bean过程详解
-
spring学习之创建项目 Hello Spring实例代码
-
02Spring基于xml的IOC配置--实例化Bean的三种方式
-
Spring 学习指南 第三章 bean的配置 (未完结)
-
Spring实战之XML与JavaConfig的混合配置详解