在spring中可以通过service来把一个业务层的类交给spring窗口进行管理,而通过repository可以把持久层的类交给spring进行管理。今天只学习一下service交给spring进行管理。同时使用手功装配中的类型装配将字段值进行装配使用。
代码如下:
package cn.szzx.service; /** *@author whp *@Email wanghaipeng@szzxbj.com *@2013-5-30 * */ public interface Man { public void sayHello(); }
package cn.szzx.service.impl; import org.springframework.stereotype.Service; import cn.szzx.service.Man; /** *@author whp *@Email wanghaipeng@szzxbj.com *@2013-5-30 * */ @Service public class American implements Man { @Override public void sayHello() { System.out.println("american say hello"); } }
package cn.szzx.service.impl; import org.springframework.stereotype.Service; import cn.szzx.service.Man; /** *@author whp *@Email wanghaipeng@szzxbj.com *@2013-5-30 * */ @Service public class Chinese implements Man { @Override public void sayHello() { System.out.println("chinese say hello"); } }
package cn.szzx.test; 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.Component; import cn.szzx.service.Man; /** * @author whp * @Email wanghaipeng@szzxbj.com * @2013-5-30 * */ @Component public class Main { @Autowired @Qualifier("chinese") private Man man; /** * @param args */ public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Main main = (Main) ctx.getBean("main"); main.getMan().sayHello(); //System.out.println(main.getMan().sayHello()); } public Man getMan() { return man; } }
@Autowired @Qualifier("chinese") private Man man;
在进行装配的时候,如果接口Man的实现只有Chinese,而没有American的话,那么就可以不使用注解@Qualifier,直接使用@Autowired。因为Autowired是根据类型装配,如果在这个程序里没有使用Qualifier,在注入时spring就会扫描出两个相同类型的接口实例,一个是Chinese的实现,一个是American的实现。这样程序就会报错。
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="cn.szzx.*" /> </beans>
<context:annotation-config /> <context:component-scan base-package="cn.szzx.*" />
在beans.xml文件中要打开自动扫描功能。
相关推荐
Spring@Autowired注解与自动装配.rar
本篇文章主要介绍最重要的三个Spring注解,也就是@Autowired、@Resource和@Service,具有很好的参考价值。下面跟着小编一起来看下吧
NULL 博文链接:https://forestqqqq.iteye.com/blog/2235292
autowired spring autowired注解完整版 使用Spring2.5的新特性——Autowired可以实现快速的自动注入,而无需在xml文档里面添加bean的声明,大大减少了xml文档的维护。(偶喜欢这个功能,因为偶对xml不感冒)。
通过@Bean 和spring的factoryBean注入的bean. 以及对应@service注解注入的类 通过@Autowired 注入对象的时间是现根据类型在根据beanName获取的案例集合
Spring注解@Resource和@Autowired.doc
下面小编就为大家带来一篇基于Spring@Autowired注解与自动装配详谈。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
NULL 博文链接:https://cdxs2.iteye.com/blog/1941619
本资源是 我的博客spring注解概述的示例代码,只有源文件,不包含相关类库
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
Spring框架中 @Autowired 和 @Resource 注解的区别 在 spring 框架中,除了使用其特有的注解外,使用基于 JSR-250 的注解,它包括 @PostConstruct, @PreDestroy 和 @Resource 注释。 首先,咱们简单了解 @...
详解 Spring注解的(List&Map)特殊注入功能 最近接手一个新项目,已经没有原开发人员维护了。项目框架是基于spring boot进行开发。其中有两处Spring的注解花费了大量的时间才弄明白到底是怎么用的,这也涉及到...
主要介绍了Spring注解@Resource和@Autowired区别对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring注解开发@PreDestroy除了@Component外,Spring提供了3个功能基本和@Component等效的注解 @Repository 用于对DAO实现类进行标注 @Service 用于对Service实现类进行标注 @Controller 用于对Controller实现类进行...
NULL 博文链接:https://ysj5125094.iteye.com/blog/2054257
背景注解可以减少代码的开发量,Spring 提供了丰富的注解功能。我们可能会被问到,Spring 的注解到底是怎么触发的呢?今天以 Spring 最常使用的一个
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入。 1、共同点 两者都可以写在字段和setter方法上。两者...
使用Spring2.5的Autowired实现注释型的IOC , 使用Spring2.5的新特性——Autowired可以实现快速的自动注入,而无需在xml文档里面添加bean的声明,大大减少了xml文档的维护