Spring自定义对象的注入配置

以上文章介绍的方法或方式,都是注入基本数据类型例如String,int等等,本例将介绍自定义对象的注入方式的配置。

【转载使用,请注明出处:http://blog.csdn.net/mahoking

操作步骤:

1、 创建Topic对象。

public class Topic {

	/**内容*/
	private String context;

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

	public Topic(String context) {
		this.context = context;
	}
}

2、 创建Speaker对象。

public class Speaker {

	private Topic topic;

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}

	/**
	 * 演讲
	 */
	public void speech() {
		System.out.println("[Speaker's Topic Context is ]"+topic.getContext()+".");
	}
}

3、 创建Spring配置文件middleLearn01.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">

	<!-- Learn middle 01    使用内部bean,但该bean不能被其他bean使用
	<bean id="speaker05" class="com.mahaochen.spring.middle.learn05.Speaker"
		scope="singleton">
		<property name="topic">
			<bean class="com.mahaochen.spring.middle.learn05.Topic">
				<constructor-arg index="0" value="Tour and Recollection" />
			</bean>
		</property>
	</bean> -->
	<!--  OR Learn middel 02 -->
	<bean id="middleSpeaker01" class="com.mahaochen.spring.middle.learn01.Speaker"
		scope="singleton">
		<property name="topic" ref="topic"/>
	</bean>
	<bean id="topic" class="com.mahaochen.spring.middle.learn01.Topic">
		<constructor-arg index="0" value="Tour and Recollection" />
	</bean>
</beans>

4、将Spring配置文件middleLearn01.xml引入到主配置文件beans.xml中。

<import resource="com/mahaochen/spring/middle/learn01/middleLearn01.xml"/>

5、编写测试类TestSpringMiddle01.java。

public class TestSpringMiddle01 {

	public static void main(String[] args) {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("beans.xml");
		Speaker speaker05 = beanFactory.getBean("middleSpeaker01", Speaker.class);
		speaker05.speech();
	}
}

【转载使用,请注明出处:http://blog.csdn.net/mahoking

时间: 2024-08-13 17:36:56

Spring自定义对象的注入配置的相关文章

servlet中如何实现通过Spring实现对象的注入

@WebServlet("/BaseServlet")public class BaseServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { super.init(); WebApplicationContextUtils.getWebApplicationContext(getServletC

Spring装配Bean---使用xml配置

声明Bean Spring配置文件的根元素是<beans>. 在<beans>元素内,你可以放所有的Spring配置信息,包括<bean>元素的声明. 除了Beans命名空间,Spring的核心框架总共自带了10个命名空间配置:  命名空间 用途  aop     为声明切面以及将@AspectJ注解的类代理为Spring切面提供了配置元素  beans     支持声明Bean和装配Bean,是Spring最核心也是最原始的命名空间  context 为配置Sprin

Spring 自定义标签配置

前景:经常使用一些依赖于Spring的组件时,发现可以通过自定义配置Spring的标签来实现插件的注入,例如数据库源的配置,Mybatis的配置等.那么这些Spring标签是如何自定义配置的?学习Spring标签的自定义配置为以后实现分布式服务框架做技术储备. 技术分析:Spring的标签配置是通过XML来实现的,通过XSD(xml Schema Definition)来定义元素,属性,数据类型等. Spring自定义标签解析 1.Spring启动时通过扫描根目录下的META-INF文件下的sp

Spring.Net学习笔记六(依赖对象的注入)

一.属性注入 上篇我们简单提到依赖注入的用途.回顾一下所讲内容,发现在object节点下使用了<property name="Tool" ref="computer"/>.而property 标签正是用来属性注入的.而ref是用来标识是关联到哪个object.而name属性是指属性名.如下: <object id="modernPerson" type="SpringNetIoC.ModernPerson, Sprin

【Spring实战】注入非Spring Bean对象

大家经常用到Spring IOC去管理对象之间的依赖关系,但一般情况下都有一个前提:这些Bean对象必须是通过Spring容器创建实例化的. 但实际上,项目中有可能会遇到这样的场景: 一个类不是通过Spring容器实例化的,而是直接new Object()这种方式创建,这个对象又和别的Spring容器中管理的对象存在依赖关系. 这时该怎么办呢,当然,我们可以手动的去调用setXxxx()方法去设置对象之间的依赖,但这样耦合性又太高.还好Spring也提供了注入非Spring Bean对象的功能.

Spring根据XML配置文件注入对象类型属性

这里有dao.service和Servlet三个地方 通过配过文件xml生成对象,并注入对象类型的属性,降低耦合 dao文件代码: package com.swift; public class DaoUser { public void fun() { System.out.println("I'm dao's fun()...................."); } } service文件代码:(提供setter方法,xml文件可通过这种方法配置) package com.sw

spring自定义类中@AutoWired标识的元素注入为null

最近在做项目的时候,发现程序运行的时候有一个nullpointer exception,一脸懵逼因为感觉程序没什么逻辑.后来发现是因为new出来的component不会自动注入它的元素. 现象:@Component修饰的自定义普通类中@Autowired属性为null 原因:如果是通过new实例化的对象,脱离了Spring的管理,所以获取不到Spring注解的属性值. 在新线程中也会存在注解获取不到Spring管理的Bean,也是因为new出来的线程,脱离了Spring容器 我在实际开发中遇到有

【初识Spring】对象(Bean)实例化及属性注入(xml方式)

title: [初识Spring]对象(Bean)实例化及属性注入(xml方式) date: 2018-08-29 17:35:15 tags: [Java,Web,Spring] --- ?#初识Spring之Bean实例化及属性注入 1.通过xml配置的方式进行实例化. 配置文件中bean标签的常用属性 通过无参构造(常用) 使用静态工厂进行创建 使用实例工厂进行创建 2.属性注入. 使用有参数构造方法注入属性 使用set方法注入属性(常用) 注入对象类型属性 注入复杂类型属性 xml配置的

Spring之对象依赖关系(依赖注入Dependency Injection)

承接上篇: Spring中,如何给对象的属性赋值: 1:通过构造函数,如下所示: <!-- 1:构造函数赋初始值 --><bean id="user1" class="com.bie.po.User"><constructor-arg value="10010" type="int"></constructor-arg>      <constructor-arg valu