package com.test.spring.inject;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
/**
* Spring注入
* 1.注入String(不通过Bean注入) <BR>
* 2.注入Map <BR>
* 3.注入List 使用private List<String> listInstance;会报错 <BR>
*
* @author Alex
*
*/
@Service
public class Property {
@Autowired
private String propertyName;
// @Autowired
// @Qualifier("otherName")
// private String propertyOtherName;
@Autowired
private Bean bean;
@Autowired(required = true)
@Qualifier("mapInstance")
// 不指定mapInstance 存入所有的<String, Object>;Spring默认byType装配.
private Map<String, Object> mapInstance;
@Autowired
@Qualifier("listInstance")
private List<Object> listInstance;
@Autowired
@Qualifier("mapBean")
private Map<String, Object> mapBean;
public static void main(String[] args) {
// ApplicationContext context = new FileSystemXmlApplicationContext(
// "applicationContext.xml");
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
String[] beans = context.getBeanDefinitionNames();
for (String bean : beans) {
System.out.println(bean);
if ("property".equals(bean)) {
Property property = (Property) context.getBean(bean);
System.out.println("***************************************");
System.out.println("name:" + property.propertyName);
// System.out.println("OtherName:" +
// property.propertyOtherName);
System.out.println("mapInstance:" + property.mapInstance);
System.out.println("listInstance:" + property.listInstance);
System.out.println("mapBean:" + property.mapBean);
System.out.println("bean.name:" + property.bean.getName());
System.out.println("***************************************");
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="false">
<description>Spring配置文件</description>
<!-- 自动注册bean,多个路径用逗号分开 -->
<context:component-scan base-package="com.test.spring.inject" />
<bean class="java.lang.String">
<constructor-arg type="java.lang.String" value="string_1234" />
</bean>
<util:map id="mapInstance">
<entry key="map_key1" value="map_Value1" />
<entry key="map_key2" value="map_Value2" />
</util:map>
<util:list id="listInstance" list-class="java.util.ArrayList">
<value type="java.lang.String">list_Value2</value>
<value type="java.lang.String">list_Value2</value>
</util:list>
<util:map id="mapBean" map-class="java.util.HashMap">
<entry key="mapKeyMap" value-ref="mapInstance" />
<entry key="mapKeyList" value-ref="listInstance" />
</util:map>
<!--
<util:constant id="name" static-field="java.lang.Integer.MAX_VALUE" />
<bean id="otherName" class="java.lang.String">
<constructor-arg type="java.lang.String" value="anotherString_1234" />
</bean>
-->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName"
value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>file:config.properties</value>
</list>
</property>
</bean>
</beans>
分享到:
相关推荐
Spring 2.5依赖注入(DI)是Java应用程序开发中的一个重要概念,它允许开发者将对象之间的依赖关系解耦,使得代码更加灵活、可维护。在这个名为"spring2.5依赖注入示例"的小demo中,我们将探讨Spring框架如何实现DI...
总之,Spring 2.5是一个功能强大且灵活的框架,它提供了Bean管理、依赖注入和面向切面编程等多种工具,帮助开发者构建高质量的Java应用。通过深入学习和实践,我们可以掌握Spring的核心理念,提高开发效率,同时也能...
**Spring2.5** 是一个全面的后端应用程序框架,它支持依赖注入(DI)和面向切面编程(AOP)。在这个例子中,Spring负责管理所有对象的生命周期,包括Hibernate的SessionFactory和DAO层的对象。Spring的事务管理功能...
5. **Spring2.5**:Spring框架是企业级Java应用的核心框架,它提供依赖注入(DI)、面向切面编程(AOP)、数据访问/集成、Web应用等多种功能,Spring2.5版本是Spring发展的一个重要里程碑,引入了许多改进和新特性。...
根据提供的信息来看,这份文档是关于Spring 2.5版本的参考手册。Spring框架是一个开源的应用框架,主要用于Java平台的企业级应用开发。Spring框架的主要优势在于它的轻量级、灵活性以及对其他技术的良好支持。接下来...
在 Spring 2.5 引入了 @Autowired 注释,我们平常直接引用的时候很少注意这些,只是自己写好了一个方法或者 springboot 自动配置好的一个方法我们要在另一个类中去调用,这个时候,我们就会采用该注释。 2. @...
jbpm4.3-spring2.5整合是一个关键的话题,涉及到企业级工作流管理系统Jbpm与Spring框架的集成。Jbpm是一个开源的工作流程管理(BPM)和业务规则管理系统,它允许开发者设计、执行、管理和监控业务流程。而Spring是...
这个系统是采用经典的Java Web开发技术栈构建的,主要包括四个核心组件:Struts2作为MVC框架、Spring作为应用上下文和依赖注入框架、Hibernate作为对象关系映射工具,以及MySql作为数据库管理系统。以下是对这些技术...
在"Spring学习详细讲述了Sping的学习过程,非常经典的学习Sping"这个资源中,我们可以期待深入理解以下关键知识点: 1. **依赖注入(DI)与控制反转(IoC)**:Spring的核心机制,通过DI,我们可以避免硬编码依赖,使...
- **spring2.5.txt** 可能包含Spring 2.5版本的配置示例或指南,该版本引入了基于注解的配置和AOP增强。 - **struts.txt** 可能涉及Struts框架的基础知识、配置或者与Spring集成的具体步骤。 综上所述,Struts和...
#### 2.5 用户界面 - **填充 JSP 文件**:通过 JSP 文件将数据呈现给用户。 - **功能验证**:通过浏览器验证 JSP 文件是否能正确显示数据并支持 CRUD 操作。 #### 2.6 验证机制 - **添加验证**:利用 Commons ...
- 自 Spring 2.5 开始,注解成为配置 bean 的主流方式。例如,`@Component`、`@Service`、`@Repository` 和 `@Controller` 注解用于标记组件,`@Autowired` 用于自动装配依赖,`@Qualifier` 用于指定注入的具体 ...
- 在`struts.xml`中,将Action的Class属性修改为Spring的Bean名称,利用Spring的IOC特性实现Action的实例化和依赖注入。 ##### 第三部分:Hibernate与Spring进行整合 **Step11:引入Hibernate框架** - 添加...
Spring框架则是一个全面的企业级应用开发框架,涵盖了依赖注入(DI)、面向切面编程(AOP)、数据访问/集成、事务管理等多个方面。在S2SH整合中,Spring主要负责管理Bean和事务,以及与Hibernate的集成。Spring2.5...
这里,`@Component`声明ExampleClass为一个Spring Bean,`@Autowired`会自动将名为`anotherBean`的Bean注入到当前Bean的成员变量中。 3. **Java配置方式** Spring 3.0引入了Java配置,允许开发者在Java代码中定义...
- **1.2.2.5 Web**:提供了构建Web应用程序的所有功能,包括MVC控制器、视图解析器、表单标签库等。 - **1.2.2.6 Test**:提供支持测试Spring组件的JUnit和TestNG集成。 - **1.2.3 Usage Scenarios 使用场景** ...
Spring MVC以其模块化、松耦合的设计,以及对依赖注入(DI)和面向切面编程(AOP)的支持,深受开发者喜爱。以下是一些从提供的文档标题中提炼出的关键知识点: 1. **基于Annotation的MVC(Spring 2.5)** - ...
1. **Sping配置数据源**:Spring支持多种数据源配置方式,如使用`BasicDataSource`、`DataSource`等。 2. **注入使用**:通过Spring的配置文件或注解来注入数据源。 3. **dbcp.BasicDataSource**:一种轻量级的...
其中,@PostConstruct 和 @PreDestroy 注解是 Spring 2.5 中引入的注解,用于指定 Bean 的初始化和销毁方法。InitializingBean 和 DisposableBean 接口则需要手动实现初始化和销毁方法。 四、 ApplicationContext ...