Spring的 注入和调用是很灵活的 手动配置和注解形式可以同时存在

来源:互联网 发布:游戏内存优化软件 编辑:程序博客网 时间:2024/05/20 16:00
1.先说action中调用spring服务,那么常见的方式
     private QueryGlobalConstantDao queryGlobalConstantDaoImpl;
     
     

     public void setQueryGlobalConstantDaoImpl(
                QueryGlobalConstantDao queryGlobalConstantDaoImpl) {
            this. queryGlobalConstantDaoImpl = queryGlobalConstantDaoImpl;
     }

其中这个声明的名称 queryGlobalConstantDaoImpl 是要和注入的service的名称一样的~~
当然 如果在spring的配置文件中加上
xmlns:context ="http://www.springframework.org/schema/context"
     xsi:schemaLocation="
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
那就是打开了content注解的方式注入和调用bean

然后 写上
     <context:annotation-config />
    <context:component-scan base-package= "com.smartshown.inner.service.impl" />
这两行其中的一个,我们就可以
     @Resource(name="queryGlobalConstantDaoImpl")
     private QueryGlobalConstantDao queryGlobalConstantDaoImpl;
来调用bean。。。。。。。。。spring支持两种调用方式同时存在,也支持下面说的注入方式两种 同时存在~
2.bean的注入,如上说的 不使用注解,普通的注入方式
     <bean name ="pmpUserServiceImpl" class= "com.smartshown.inner.service.impl.PmpUserServiceImpl" >
            <property name ="pmpUserDaoImpl" ref= "pmpUserDaoImpl"></property >
     </bean >

打开注解的方式就上上面说的 <context:component-scan>和<context:annotation-config />
<context:component-scan>除了具有<context:annotation-config />的功能之外,还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能 其实我们可以两句都写上,spring也会只注入一次的。

常用的就是 @Service("aaaa")
调用 @Resource(name ="aaaa")

0 0
原创粉丝点击