spring-bean命名方式-以及实例化--配置文件整合
来源:互联网 发布:程序员表情包 编辑:程序博客网 时间:2024/06/11 07:29
Spring 配置文件的一般结构如下:
•<beans>
• <import resource=“resource1.xml” />
• <import resource=“resource2.xml” />
• <bean id=“bean1” class=“***”></bean>
• <bean name=“bean2” class=“***”></bean>
• <alias alias=“bean3”name=“bean2”/>
•</beans>
其中import是导入其他配置。以上实例代码为:
1、命名
首先是接口类:
public interface HelloWorld {public void sayHello();}然后是实现类:
public class HelloWorldImpl implements HelloWorld {@Overridepublic void sayHello() {System.out.println("Hello World!");}}spring命名实例:
<?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-3.0.xsd"><bean class="com.jike.spring.chapter04.definition.HelloWorldImpl" /><bean id="helloWorld" class="com.jike.spring.chapter04.definition.HelloWorldImpl" /><bean name="helloWorldByName" class="com.jike.spring.chapter04.definition.HelloWorldImpl" /><bean id="helloWorldById" name="helloWorldByName01" class="com.jike.spring.chapter04.definition.HelloWorldImpl" /><bean name="bean1;alias11;alias12;alias13"class="com.jike.spring.chapter04.definition.HelloWorldImpl" /><bean id="bean2" name="alias21,alias22,alias23"class="com.jike.spring.chapter04.definition.HelloWorldImpl" /><bean name="bean3" class="com.jike.spring.chapter04.definition.HelloWorldImpl" /><alias alias="alias31" name="bean3" /><alias alias="alias32" name="bean3" /></beans>
2、实例化示例代码
首先是接口类:
public interface HelloWorld {public void sayHello();}实现类:
public class HelloWorldImpl implements HelloWorld { private String message; /** * 空构造器 */ public HelloWorldImpl() { this.message = "Hello World!"; } /** * 带参构造器 * * @param message */ public HelloWorldImpl(String message) { this.message = message; } public void sayHello() { System.out.println(message); }}构造器实例化不需要类。
下面是静态工厂实例化类:
public class HelloWorldStaticFactory { /** * 工厂方法 * * @param message * @return */ public static HelloWorld newInstance(String message) { // 返回需要的Bean实例 return new HelloWorldImpl(message); }}下面是实例工厂实例化类:
public class HelloWorldInstanceFactory { /** * 工厂方法 * * @param message * @return */ public HelloWorld newInstance(String message) { // 返回需要的Bean实例 return new HelloWorldImpl(message); }}最后是实例化的spring配置文件:
<?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-3.0.xsd"> <!--使用默认构造参数--> <bean id="helloWorldWithNoArgs" class="com.jike.spring.chapter04.instance.HelloWorldImpl" /> <!--使用有参数构造参数--> <bean id="helloWorldWithArgs" class="com.jike.spring.chapter04.instance.HelloWorldImpl" > <!-- 指定构造器参数 --> <constructor-arg index="0" value="Hello Args!"/> </bean> <!--静态工厂方式--> <bean id="helloWorldStaticFactory" class="com.jike.spring.chapter04.instance.HelloWorldStaticFactory" factory-method="newInstance" > <!-- 指定构造器参数 --> <constructor-arg index="0" value="Hello Static Factory!"/> </bean> <!-- 1、定义实例工厂Bean --> <bean id="helloWorldInstanceFactory" class="com.jike.spring.chapter04.instance.HelloWorldInstanceFactory" /> <!-- 2、使用实例工厂Bean创建Bean --> <bean id="helloWorldInstance" factory-bean="helloWorldInstanceFactory" factory-method="newInstance"> <constructor-arg index="0" value="Hello Instance Factory!"></constructor-arg> </bean> </beans>
最后是测试代码:
import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {helloWorldInstanceFactory();}//使用无参数构造器来实例化Beanpublic static void sayHelloWithNoArgs() {BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-instance.xml");HelloWorld helloWorld = beanFactory.getBean("helloWorldWithNoArgs", HelloWorld.class);helloWorld.sayHello();}//使用有参数构造器来实例化Beanpublic static void sayHelloWithArgs() {BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-instance.xml");HelloWorld helloWorld = beanFactory.getBean("helloWorldWithArgs", HelloWorld.class);helloWorld.sayHello();}//使用静态工厂方法来实例化Bean public static void helloWorldStaticFactory() { // 1、读取配置文件实例化一个IOC容器 BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-instance.xml"); // 2、从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现” HelloWorld helloWorld = beanFactory.getBean("helloWorldStaticFactory", HelloWorld.class); // 3、执行业务逻辑 helloWorld.sayHello(); } //使用实例化工厂方法来实例化Bean public static void helloWorldInstanceFactory() { // 1、读取配置文件实例化一个IOC容器 BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-instance.xml"); // 2、从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现编程” HelloWorld helloWorld = beanFactory.getBean("helloWorldInstance", HelloWorld.class); // 3、执行业务逻辑 helloWorld.sayHello(); }}
阅读全文
0 0
- spring-bean命名方式-以及实例化--配置文件整合
- Spring (一) 加载配置文件,实例化类, 三种实例化bean的方式
- spring学习(二)—spring 的bean的实例化方式(通过配置文件)
- spring实例化bean的方式
- spring中实例化bean的方式
- Spring中bean的实例化方式
- spring中有三中实例化bean的方式
- spring中三种实例化bean的方式
- 2.bean的命名和实例化、依赖注入方式
- spring装配Bean过程以及Bean实例化过程
- (3)Spring的三种实例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命周期
- (2)Spring的三种实例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命周期
- Spring加载配置文件和三种Bean实例化
- spring--xml配置方式,bean实例化的方式
- Spring中多配置文件以及寻觅引用其他bean的方式
- bean实例化方式
- Spring 实例化bean
- spring实例化bean
- Linux 命令行
- springboot项目的打包部署(gralde和maven)
- Could not create the view: An unexpected exception was thrown.问题解决
- vue使用resource传参数
- 题目100-1的个数
- spring-bean命名方式-以及实例化--配置文件整合
- loadrunner录制脚本常见问题
- Java使用RSA加密解密签名及校验
- centos 下github的详细教程,创建,提交等
- HTML下js使用方式
- 顺序存储结构的插入与删除
- miRNA的特征、功能及识别方法等详解
- 计算机网络 rdt2.1 rdt2.2含义 区别
- ubuntu配置jdk