- 浏览: 258692 次
- 性别:
- 来自: 天津
文章分类
- 全部博客 (180)
- J2SE (15)
- J2EE (7)
- Struts (2)
- Hibernate (18)
- Spring (22)
- ssh (2)
- HTML (1)
- JS (10)
- CSS (3)
- jQuery (15)
- extJS (0)
- FLEX (14)
- Web service (7)
- Axis2 (3)
- lucene2.0 (9)
- maven (3)
- JBoss (0)
- WebLogic (0)
- Linux (8)
- Android (0)
- SiteMesh (1)
- JPA (0)
- JBPM (1)
- xFire (3)
- XML (4)
- apache (4)
- bingo专区 (0)
- 加密解密算法研究 (1)
- 数据库 (16)
- 资料 (1)
- 经验 (4)
- 健康 (1)
- 技术交流区 (1)
- 交友区 (0)
- jquery 插件区 (1)
最新评论
-
forrest_lv:
很有帮助
Axis2 XFire CXF 比较 -
kuloujianzun:
...
flex DataGrid -
zx.peng:
大哥,请教一下...
下面在别的地方引入:(在什么地方、怎么 ...
FLEX下拉多选框 -
elvislee030:
试试去,正好要找这个组件。。先谢过
FLEX下拉多选框 -
futrueboy:
如果你发到网上来,请翻译的好点
Apache Mina的学习应用(三)
以下是一个例子:
先编写接口Man:
public interface Man {
public String sayHello();
}
然后写Man的实现类Chinese和American:
@Service
public class Chinese implements Man{
public String sayHello() {
return "I am Chinese!";
}
}
@Service
public class American implements Man{
public String sayHello() {
return "I am American!";
}
}
@Service注释表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,例如Chinese实例化为chinese,American实例化为american,如果需要自己改名字则:@Service("你自己改的bean名")。
beans.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"
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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="testspring.main"/>
</beans>
在spring的配置文件里面只需要加上<context:annotation-config/>和<context:component-scan base-package="需要实现注入的类所在包"/>,可以使用base-package="*"表示全部的类。
编写主类测试:
@Service
public class Main {
@Autowired
@Qualifier("chinese")
private Man man;
public static void main(String[] args) {
// TODO code application logic here
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Main main = (Main) ctx.getBean("main");
System.out.println(main.getMan().sayHello());
}
public Man getMan() {
return man;
}
}
在Man接口前面标上@Autowired和@Qualifier注释使得Man接口可以被容器注入,当Man接口存在两个实现类的时候必须指定其中一个来注入,使用实现类首字母小写的字符串来注入。否则可以省略,只写@Qualifier
发表评论
-
Spring 企业级开发应用---------AXIS和spring的远程服务整合的应用
2010-08-16 14:36 1144Spring框架对远程访问 ... -
Spring 企业级开发应用---------httpinvoke和spring的远程服务整合的应用
2010-08-16 14:35 943和burlap和hessian一样的配置细细 服务端服务 ... -
Spring 企业级开发应用---------hessian和spring的远程服务整合的应用
2010-08-16 14:35 912hessian和burlap使用同一公司的产品实现机制不同 ... -
Spring 企业级开发应用---------burlap和spring的远程服务整合的应用
2010-08-16 14:34 837使用burlap和spring整合发布远程服务 开发步骤 ... -
Spring 企业级开发应用---------rmi和spring的整合的应用
2010-08-16 14:34 803使用spring和rmi开发远程服务的类的应用(此处可以一 ... -
spring2.5 和Hibernate的注解的应用
2010-08-16 14:30 769一、步骤 a. <!-- 数据源 --&g ... -
获取ApplicationContext的几种方式
2010-08-16 14:26 951第一种采用类路径的加载方式获取: Appli ... -
Spring 注解的配置的细节
2010-08-16 14:16 1677一. spring注解 1.准备工作(1)导入comm ... -
使用Spring 2.5 注释驱动的 IoC 功能
2010-08-16 14:10 839在项目中使用Spring的注解,关于sp ... -
Spring 和ActionMQ整合JMS开发应用
2010-08-16 13:51 1494Spring 和ActionMQ整合 ... -
Spring中添加 Log4j注解
2010-08-16 13:49 2482参照别人的代码做了个Demo如下: Spring的加载时 ... -
Spring 的优秀工具类盘点,特殊字符转义和方法入参检测工具类
2010-08-16 13:46 968Spring 不但提供了一个功能全面的应用开发框架,本身还 ... -
Spring Web 相关工具类
2010-08-16 13:38 991Web 相关工具类 您几乎总是使用 Spring 框架开 ... -
Spring 的优秀工具类盘点
2010-08-16 13:22 738Spring 不但提供了一个功能全面的 ... -
Spring ApplicationContext.xml配置的12个技巧
2010-08-16 13:08 1231Spring是一个强有力的ja ... -
Spring 中一个常用的反射类库ReflectionUtils
2010-08-16 13:05 4350在项目中部分方法采用反射,通过类访问类的属性,通过类反问方法, ... -
spring 的OpenSessionInViewFilter简介
2010-08-16 11:39 589假设在你的应用中Hibernate是通过spring 来管 ... -
使用hibernate3和Spring解决Oracle大字段(长文本)问题
2010-08-16 11:33 1445使用Oracle来存储长文本始终没Mysql ... -
Spring中Quartz的配置
2009-11-02 09:56 642Quartz是一个强大的企业级任务调度框架,Spring中继承 ... -
Spring声明式事务管理(下)
2009-11-01 20:58 1135三.用BeanNameAutoProxyCreator自动 ...
相关推荐
使用Spring2.5的Autowired实现注释型的IOC , 使用Spring2.5的新特性——Autowired可以实现快速的自动注入,而无需在xml文档里面添加bean的声明,大大减少了xml文档的维护
一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...
autowired spring autowired注解完整版 ... 使用Spring2.5的新特性——Autowired可以实现快速的自动注入,而无需在xml文档里面添加bean的声明,大大减少了xml文档的维护。(偶喜欢这个功能,因为偶对xml不感冒)。
网上对spring2.5吹的神了,个个都说要重新加入2.5,我就忍不住去看看这个宝贝,看过发现Autowired注解这个非常重要的功能还是不能脱离bean,而且发现自动装配就好像个鸡肋,2.5既然出来了不用又不爽,用了之后发现...
Spring@Autowired注解与自动装配.rar
3) 利用Spring的@Service、@Repository两个Annotation来实现Service类、DAO类在Spring容器中的自动注册,利用@Autowired来实现Service与其他对象的引用。 通过框架提供的以上组件,除了基本的配置,如数据源、事务等...
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.spring...
NULL 博文链接:https://huihai.iteye.com/blog/1880106
NULL 博文链接:https://forestqqqq.iteye.com/blog/2235292
java web开中spring在autowired自动注入极度精简版本,绝对可以运行。博客地址: http://blog.csdn.net/qq5132834/article/details/47870351
Spring Ioc源码分析系列--@Autowired注解的实现原理.doc
主要介绍了Spring使用@Autowired为抽象父类注入依赖代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了详解Spring Controller autowired Request变量的相关资料,通过此文希望能帮助到大家,需要的朋友可以参考下
@Autowired使用 构造函数注入 public Class Outer { private Inner inner; @Autowired public Outer(Inner inner) { this.inner = inner; } } 属性注入 public Class Outer { @Autowired private Inner ...
NULL 博文链接:https://cdxs2.iteye.com/blog/1941619
主要介绍了详解Spring @Autowired 注入小技巧,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring的annotation的autowired配置方法。